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

Bru*_*tos 6 lucene elasticsearch

用例: 我收藏了companies。每个公司都有city和的信息country。我希望能够进行文本搜索来查找例如曼谷-泰国的公司。所有信息都必须使用不同的语言进行搜索。示例:在巴西,大多数人用英语指曼谷,而不是巴西人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" ]
}
Run Code Online (Sandbox Code Playgroud)

每个分析器使用不同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"
        }
    }
}]
Run Code Online (Sandbox Code Playgroud)

这是我正在使用的查询:

{
   "query": {
      "multi_match" : {
        "query" : "bangkok tailandia",
        "type"  : "cross_fields",
        "operator":   "and",
        "fields" : [ "city_txt_en", "country_txt_pt" ],
        "tie_breaker": 0.0
      }
   },
   "profile": true
}
Run Code Online (Sandbox Code Playgroud)

分析查询后,结果为:

(+city_txt_en:bangkok +city_txt_en:tailandia) 
(+country_txt_pt:bangkok +country_txt_pt:tailandia)
Run Code Online (Sandbox Code Playgroud)

由于Elasticsearch试图同时匹配citycountry字段中的术语,因此无法正常工作。问题在于,曼谷一词是英文,而tailandia是葡萄牙语。

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

+(city_txt_en:bangkok | country_txt_pt:bangkok) 
+(city_txt_en:tailandia | country_txt_pt:tailandia)
Run Code Online (Sandbox Code Playgroud)

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

kee*_*ety 4

您应该能够使用 来实现这一点[query_string][1]。查询字符串会分解术语,然后根据分析器将它们应用到每个字段。例子:

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

      }
   },
   "profile": true
}
Run Code Online (Sandbox Code Playgroud)