我正在尝试使用n-gram过滤器创建优先搜索结果。优先级应如下:
我的想法是将一个属性拆分为多个字段,并增强每个字段。
我的分析:
'ngram_index_analyzer': {
'type': 'custom',
'tokenizer': 'keyword',
'filter': [
'lowercase',
'multiplex',
'ngram_filter',
]
},
'edge_ngram_index_analyzer': {
'type': 'custom',
'tokenizer': 'keyword',
'filter': [
'lowercase',
'edge_ngram_filter',
],
},
'back_edge_ngram_index_analyzer': {
'type': 'custom',
'tokenizer': 'keyword',
'filter': [
'lowercase',
'back_edge_ngram_filter',
],
},
和我的过滤器: 'ngram_filter': {
'type': 'ngram',
'min_gram': 2,
'max_gram': 20,
},
'edge_ngram_filter': {
'type': 'edge_ngram',
'min_gram': 2,
'max_gram': 20,
},
'back_edge_ngram_filter': {
'type': 'edge_ngram',
'min_gram': 2,
'max_gram': 20,
'side': 'back',
},
然后,我将一个属性拆分为多个字段,并为每个字段分配相应的过滤器: 'substring': {
'type': 'text',
'analyzer': 'ngram_index_analyzer',
'search_analyzer': 'search_analyzer',
'boost': 0.5,
},
'prefix': {
'type': 'text',
'analyzer': 'edge_ngram_index_analyzer',
'search_analyzer': 'search_analyzer',
'boost': 0.7
},
'ending': {
'type': 'text',
'analyzer': 'back_edge_ngram_index_analyzer',
'search_analyzer': 'search_analyzer',
'boost': 0.2
}
我希望查询的结果如下:但是,它只是对提升值求和,所有结果都是随机的。例如,“关于市场的某些营销内容”可能会比“市场讨论”高,因为它有2个匹配项(中间和结尾)。
有没有一种方法可以重建查询,以便每个匹配项都是唯一的,并且结果为OR而不是AND?
最佳答案
我不确定是否完全了解您的用例。
但是,关于您的映射,具有best_field策略的多重匹配是否可以满足您的需求?
https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-multi-match-query.html。
这是一个dis_max,且tie_breaker为0的情况。
https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-dis-max-query.html
您可以在2个字段中查询,并且取决于提升,仅使用最佳字段的分数。
官方文件:
best_fields The best_fields type is most useful when you are searching for multiple words best found in the same field. For instance “brown fox” in a single field is more meaningful than “brown” in one field and “fox” in the other.
The best_fields type generates a match query for each field and wraps them in a dis_max query, to find the single best matching field. For instance, this query:
GET /_search { "query": { "multi_match" : { "query": "brown fox", "type": "best_fields", "fields": [ "subject", "message" ] } } }
Normally the best_fields type uses the score of the single best matching field, but if tie_breaker is specified, then it calculates the score as follows:
the score from the best matching field plus tie_breaker * _score for all other matching fields
关于elasticsearch - Elasticsearch使用OR查询代替AND查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64344422/