elasticsearch Elasticsearch使用不同的查询分析器进行多匹配跨字段查询,elasticsearch,lucene,elasticsearch,Lucene" /> elasticsearch Elasticsearch使用不同的查询分析器进行多匹配跨字段查询,elasticsearch,lucene,elasticsearch,Lucene" />

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

elasticsearch Elasticsearch使用不同的查询分析器进行多匹配跨字段查询,elasticsearch,lucene,elasticsearch,Lucene,用例: 我有一批公司。每家公司都有城市和国家的信息。我希望能够进行文本搜索,以找到例如泰国曼谷的公司。所有信息都必须可以用不同的语言进行搜索。 例子: 在巴西,大多数人在英文版中提到曼谷,而不是巴西版。在这种情况下,如果一个人想要搜索曼谷-泰国的公司,搜索语句将是曼谷泰兰迪亚。 由于这个要求,我必须能够搜索不同的语言领域检索结果 问题: 如果发送查询时未指定analyzer Elasticsearch,请使用在每个字段配置上指定的search_analyzer。问题是它破坏了跨字段查询的目的。

用例: 我有一批
公司
。每家公司都有
城市
国家
的信息。我希望能够进行文本搜索,以找到例如泰国曼谷的公司。所有信息都必须可以用不同的语言进行搜索。 例子: 在巴西,大多数人在英文版中提到曼谷,而不是巴西版。在这种情况下,如果一个人想要搜索曼谷-泰国的公司,搜索语句将是
曼谷泰兰迪亚
。 由于这个要求,我必须能够搜索不同的语言领域检索结果

问题: 如果发送查询时未指定analyzer 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" ]
}
"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"
        }
    }
}]
每个分析仪使用不同的
stop
语言过滤器

这是字段配置:

"query_analyzer_en": {
    "type": "custom",
    "tokenizer": "standard",
    "filter": [ "lowercase", "asciifolding", "stopwords_en" ]
},
"query_analyzer_pt": {
    "type": "custom",
    "tokenizer": "standard",
    "filter": [ "lowercase", "asciifolding", "stopwords_pt" ]
}
"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正在尝试匹配
城市
国家
字段中的两个术语。问题是曼谷一词是英语,而泰兰迪亚一词是葡萄牙语

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

+(city_txt_en:bangkok | country_txt_pt:bangkok) 
+(city_txt_en:tailandia | country_txt_pt:tailandia)
但现在的问题是,我必须对这两种语言使用相同的查询分析器。我需要一种通过语言使用不同的查询分析器生成上面的lucene查询的方法。

根据文档

然而,您可以做的是将查询分成两部分,就像这样,每个部分都有相同的匹配机会。这里您可以使用
匹配
,因为每个
多匹配
都有一个字段,但是您也可以在每个子查询中添加具有相同分析器的其他字段

{
    "bool": {
        "should": [
            {
              "multi_match" : {
                "query" : "bangkok tailandia",
                "type":       "cross_fields",
                "operator":   "and",
                "fields" : [ "city_txt_en" ],
                "minimum_should_match": "50%" 
              }
            },
            {
              "multi_match" : {
                "query" : "bangkok tailandia",
                "type":       "cross_fields",
                "operator":   "and",
                "fields" : [ "country_txt_pt" ]
              }
            }
        ]
    }
}

您应该能够使用
[query\u string][1]
实现这一点。 查询字符串打断术语,然后根据分析器在每个字段中应用它们。 例如:

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

      }
   },
   "profile": true
}

如果我使用
“minimum\u should\u match”:“50%”
这意味着对于每个查询,它必须至少匹配两个术语中的一个。问题是,如果两个查询匹配
曼谷
,但不匹配
tailandia
,则将检索文档。但是我只需要检索与
曼谷
tailandia
匹配的文档。如果我使用
bool/must
将查询分为两个查询(每个术语一个查询),它就会工作。但我不想在我的API上创建这种逻辑。我只想把这句话发送给Elasticsearch,有没有任何选项可以在不打断查询的情况下按术语进行搜索?也许你能解释一下你的用例是什么,即为什么你的用户在同一个查询中使用两种不同的语言进行搜索?这可能也会引起其他人的兴趣。葡萄牙语中有没有停止词不是英语中的停止词,反之亦然?也许英语中有很多停止词在葡萄牙语中不存在,反之亦然。但stopwords只是一个例子。例如,我们可以对每种语言使用同义词和词干。另一点是,我想索引许多语言。因此,在这种情况下,如果不破坏不同领域的信息,我可能有更大的机会制造问题。