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

标签 elasticsearch lucene

用例: 我有一组公司。每个公司都有城市国家的信息。我希望能够进行文本搜索以查找例如泰国曼谷的公司。所有信息必须可以不同语言搜索。 例子: 在巴西,大多数人指的是英语版的曼谷,而不是巴西版的 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/

相关文章:

elasticsearch - 是否有可能提取 Google Analytics 数据并将其发布到 Elastic Search?

java - Lucene Android NoClassDefFoundError

lucene - 如何从 Solr 索引中删除逻辑删除的文档?

garbage-collection - Solr 缓存与 EHCache/BigMemory

solr - 将TermVector插入Lucene

python - 使用Python API有条件地更新ElasticSearch文档

php - 如何使用多个实体绑定(bind)设置 ElasticSearch 索引结构

ruby-on-rails - searchkick - 具有多个属性的自动完成

elasticsearch - Elasticsearch 2.0 GreaterOrEquals NEST 2.0

java - 同时进行 Lucene 索引和搜索