Elasticsearch 我可以在query_string短语模式下使用 "OR" 或 "AND"吗?



例如,我有两个文档,如"美味的苹果是好的"以及";美味的香蕉很好吃;。

现在我想使用query_string来匹配两个短语"美味的苹果;以及";美味的香蕉";。一般来说,我可以使用类似的查询

""美味的苹果;或者";美味的香蕉">

以匹配这些文档。

但这里我想要像一样的查询

""美味的(苹果或香蕉)">

以匹配。在短语模式中,es似乎不支持括号和bool。

我之所以需要上面的这个,是因为随着搜索词数量的增加,有效的query_string变得更加复杂。

例如,当我想搜索时

""(美味的、讨厌的或好的)(苹果、香蕉或葡萄)">

我不想像一样划分这个query_string

""美味的苹果;或者";美味的香蕉;或者";美味的葡萄;或者";讨厌的苹果;或者">

Ingest文档

POST test_david_zhao/_doc
{
"text": "tasty apple is good"
}
POST test_david_zhao/_doc
{
"text": "tasty grape is good"
}
POST test_david_zhao/_doc
{
"text": "tasty banana is good"
}
POST test_david_zhao/_doc
{
"text": "nasty banana is bad"
}
POST test_david_zhao/_doc
{
"text": "dirty grape is bad"
}

查询

POST test_david_zhao/_search
{
"query": {
"query_string": {
"default_field": "text",
"query": "+(text:apple text:banana text: grape) +(text:tasty text:nasty)"
}
}
}

响应

{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 4,
"relation" : "eq"
},
"max_score" : 2.261763,
"hits" : [
{
"_index" : "test_david_zhao",
"_type" : "_doc",
"_id" : "d6t3_HcBDMyXCx985YKJ",
"_score" : 2.261763,
"_source" : {
"text" : "nasty banana is bad"
}
},
{
"_index" : "test_david_zhao",
"_type" : "_doc",
"_id" : "dKt3_HcBDMyXCx9804Le",
"_score" : 1.9252907,
"_source" : {
"text" : "tasty apple is good"
}
},
{
"_index" : "test_david_zhao",
"_type" : "_doc",
"_id" : "dat3_HcBDMyXCx982oIq",
"_score" : 1.4144652,
"_source" : {
"text" : "tasty grape is good"
}
},
{
"_index" : "test_david_zhao",
"_type" : "_doc",
"_id" : "dqt3_HcBDMyXCx984IJD",
"_score" : 1.4144652,
"_source" : {
"text" : "tasty banana is good"
}
}
]
}
}

最新更新