elasticsearch 来自多个字段集合的随机自由文本搜索弹性搜索,elasticsearch,mongoose,mongoosastic,elasticsearch,Mongoose,Mongoosastic" /> elasticsearch 来自多个字段集合的随机自由文本搜索弹性搜索,elasticsearch,mongoose,mongoosastic,elasticsearch,Mongoose,Mongoosastic" />

elasticsearch 来自多个字段集合的随机自由文本搜索弹性搜索

elasticsearch 来自多个字段集合的随机自由文本搜索弹性搜索,elasticsearch,mongoose,mongoosastic,elasticsearch,Mongoose,Mongoosastic,我们正在使用弹性搜索、MongoDB、Mongoostic 假设 User:{ username:String, city : String, country:String etc } 这种类型的文档存储在弹性搜索中,现在如果用户搜索abhay sikandrabad,则首先尝试同时查找abhay和sikandrabad。abhay,sikandrabad可能出现在用户名、城市、国家的任何地方。因此,基本上它从每个字段搜索,如果不匹配,则尝试匹配abhay,如果未找到ab

我们正在使用弹性搜索、MongoDB、Mongoostic

假设

User:{
  username:String,
  city : String,
  country:String 
   etc 
}
这种类型的文档存储在弹性搜索中,现在如果用户搜索abhay sikandrabad,则首先尝试同时查找abhay和sikandrabad。abhay,sikandrabad可能出现在用户名、城市、国家的任何地方。因此,基本上它从每个字段搜索,如果不匹配,则尝试匹配abhay,如果未找到abhay的数据,则尝试查找sikandrabad


这种类型的东西已经在弹性搜索中实现了,还是我必须为此编写代码?

我认为最接近您描述的查询是。即使有与所有单词匹配的记录,它仍将返回仅与单个单词匹配的记录,但与所有单词匹配的记录将显示在列表的顶部

如果可以重新创建索引,则使用自定义的所有字段。索引时间优化将比搜索时间优化提供更好的性能。因此,您可以创建如下映射:

PUT /my_index/_mapping/my_mapping
{
    "_all": {"enabled": false},
    "properties": {
        "custom_all": {
            "type": "string"
        },
        "username": {
            "copy_to": "custom_all",
            "type": "string"
        },
        "city": {
            "copy_to": "custom_all",
            "type": "string"
        },
        "country": {
            "copy_to": "custom_all",
            "type": "string"
        }
}
无论您希望搜索哪些字段,请将它们包含在带有copy_to param的custom_all字段中。现在,您可以在custom_all字段上执行搜索

GET /my_index/my_mapping/_search
{
    "query": {
        "match": {
            "custom_all": "text to match"
        }
    }
}
如果要为用户名匹配的记录赋予更高的优先级,可以使用如下bool查询:

GET /my_index/my_mapping/_search
{
    "query": {
        "bool": {
            "must": {
                "match": {"custom_all": "text to match"}
            },
            "should": [
                { "match": { "username": "text to match" } }
            ]
        }
    }
}

must子句确保查询与自定义_all字段匹配。should子句确定文档的分数。如果should子句匹配,分数将更高。类似地,在数组中添加更多的should子句将包括不同的评分标准。您还可以将boost参数添加到should子句中,以确定哪个字段对总分的贡献程度。希望这对您有所帮助。

如果您想在多个字段中搜索一个值,请增加
should
方法的数量,并传递更多字段键。若要优先考虑字段,则将
替换为
必须

.setQuery(QueryBuilders.boolQuery()
.should(QueryBuilders.matchQuery(field1\u键,值))
.should(QueryBuilders.matchQuery(字段2_键,值)))