我认为最好是描述自己的意图并尝试将其分解为代码。
query_string
提供的功能,我希望用户能够进行复杂的查询。例如“AND”,“OR”和“〜”等。query_string
单独搜索它们,而不是短语。例如,“谁愿意”应该把这三个词按顺序排列给我,这是我的最佳选择,然后再给我任何东西。 当前查询:
{
"indices_boost": {},
"aggregations": {
"by_ayah_key": {
"terms": {
"field": "ayah.ayah_key",
"size": 6236,
"order": {
"average_score": "desc"
}
},
"aggregations": {
"match": {
"top_hits": {
"highlight": {
"fields": {
"text": {
"type": "fvh",
"matched_fields": [
"text.root",
"text.stem_clean",
"text.lemma_clean",
"text.stemmed",
"text"
],
"number_of_fragments": 0
}
},
"tags_schema": "styled"
},
"sort": [
{
"_score": {
"order": "desc"
}
}
],
"_source": {
"include": [
"text",
"resource.*",
"language.*"
]
},
"size": 5
}
},
"average_score": {
"avg": {
"script": "_score"
}
}
}
}
},
"from": 0,
"size": 0,
"_source": [
"text",
"resource.*",
"language.*"
],
"query": {
"bool": {
"must": [
{
"query_string": {
"query": "inna alatheena",
"fuzziness": 1,
"fields": [
"text^1.6",
"text.stemmed"
],
"minimum_should_match": "85%"
}
}
],
"should": [
{
"match": {
"text": {
"query": "inna alatheena",
"type": "phrase"
}
}
}
]
}
}
}
注意:尽管我在索引中有
alatheena
,但是没有~
进行搜索的allatheena
不会返回任何内容。因此,我必须进行模糊搜索。有什么想法吗?
最佳答案
您应该使用Dis Max Query来实现。
快速示例如何使用它:
POST /_search
{
"query": {
"dis_max": {
"tie_breaker": 0.7,
"boost": 1.2,
"queries": [
{
"match": {
"text": {
"query": "inna alatheena",
"type": "phrase",
"boost": 5
}
}
},
{
"match": {
"text": {
"query": "inna alatheena",
"type": "phrase",
"fuzziness": "AUTO",
"boost": 3
}
}
},
{
"query_string": {
"default_field": "text",
"query": "inna alatheena"
}
}
]
}
}
}
它将运行您的所有查询,并且将获得得分最高的查询。因此,只需使用它定义规则即可。您应该实现您想要的。
关于search - Elasticsearch query_string与match_phrase结合,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33580698/