gpt4 book ai didi

使用不同查询分析器的 Elasticsearch 多匹配跨字段查询

转载 作者:行者123 更新时间:2023-11-29 02:48:19 24 4
gpt4 key购买 nike

用例:我有一组公司。每个公司都有城市国家的信息。我希望能够进行文本搜索以查找例如泰国曼谷的公司。所有信息必须可以不同语言搜索。例子:在巴西,大多数人指的是英语版的曼谷,而不是巴西版的 Banguecoque。在这种情况下,如果有人想搜索位于泰国曼谷的公司,搜索语句将为 bangkok tailandia。由于此要求,我必须能够跨不同的语言字段进行搜索以检索结果。

问题:在不指定分析器的情况下发送查询时,Elasticsearch 使用在每个字段配置上指定的 search_analyzer。问题是它打破了跨字段查询的目的。这是分析器配置:

"query_analyzer_en": {
"type": "custom",
"tokenizer": "standard",
"filter": [ "lowercase", "asciifolding", "stopwords_en" ]
},
"query_analyzer_pt": {
"type": "custom",
"tokenizer": "standard",
"filter": [ "lowercase", "asciifolding", "stopwords_pt" ]
}

每个分析器按语言使用不同的stop过滤器。

这是字段配置:

"dynamic_templates": [{
"english": {
"match": "*_txt_en",
"match_mapping_type": "string",
"mapping": {
"type": "string",
"analyzer": "index_analyzer_en",
"search_analyzer": "query_analyzer_en"
}
}
}, {
"portuguese": {
"match": "*_txt_pt",
"match_mapping_type": "string",
"mapping": {
"type": "string",
"analyzer": "index_analyzer_pt",
"search_analyzer": "query_analyzer_pt"
}
}
}]

这是我正在使用的查询:

{
"query": {
"multi_match" : {
"query" : "bangkok tailandia",
"type" : "cross_fields",
"operator": "and",
"fields" : [ "city_txt_en", "country_txt_pt" ],
"tie_breaker": 0.0
}
},
"profile": true
}

分析查询后的结果是:

(+city_txt_en:bangkok +city_txt_en:tailandia) 
(+country_txt_pt:bangkok +country_txt_pt:tailandia)

它无法正常工作,因为 Elasticsearch 正在尝试匹配 citycountry 字段中的两个术语。问题是 bangkok 是英语,而 tailandia 是葡萄牙语。

如果我在查询上设置分析器,lucene 查询就是我期望的方式:

+(city_txt_en:bangkok | country_txt_pt:bangkok) 
+(city_txt_en:tailandia | country_txt_pt:tailandia)

但现在的问题是我必须对两种语言使用相同的查询分析器。我需要一种方法来按语言使用不同的查询分析器来生成上面的 lucene 查询。

最佳答案

您应该能够使用 [query_string][1] 来实现它。查询字符串打破术语,然后根据分析器将它们应用于每个字段。示例:

{
"query": {
"query_string" : {
"query" : "bangkok tailandia",
"default_operator": "AND",
"fields" : [ "city_txt_en", "country_txt_pt" ]

}
},
"profile": true
}

关于使用不同查询分析器的 Elasticsearch 多匹配跨字段查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36051750/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com