【发布时间】:2019-12-10 16:19:43
【问题描述】:
我正在尝试在 Elasticsearch 中构建以下查询:
(query1) AND (query2 OR query2 OR TRUE)
'OR true' 部分是否可以使用 elasticsearch,或者是否有另一种结构化查询的方式以给出相同的结果?
我有一组文档,比如说 10 个,所有匹配 tag1,这 10 个文档中的一些也将匹配 tag2 和 tag3,如果是这样,我使用命名查询来告诉我哪些文档匹配 tag2 和 tag3 (匹配 tag2 和 tag3 的文档是匹配 tag1 的文档的子集)。
但是,即使没有匹配 tag2 或 tag3,我仍然应该从匹配 tag1 的初始查询中获得结果。
GET /test/_search
{
"query": {
"nested": {
"path": "TAGS",
"query": {
"bool": {
"must": [
{
"match": {
"TAGS.ID": {
"query": "tag1",
"_name": "tag1-query"
}
}
},
{
"bool": {
"should": [
{
"match": {
"TAGS.ID": {
"query": "tag2",
"_name": "tag2-query"
}
}
},
{
"match": {
"TAGS.ID": {
"query": "tag3",
"_name": "tag3-query"
}
}
},
// OR true here?
]
}
}
]
}
},
"inner_hits": {}
}
}
}
更新:基于@Val 的评论。这是我的完整测试:
PUT /test
PUT /test/_mapping/_doc
{
"properties": {
"name": {
"type": "text"
},
"TAGS": {
"type": "nested"
}
}
}
POST /test/_doc
{
"name" : "doc1",
"TAGS" : [
{
"ID" : "tag1",
"TYPE" : "BASIC"
},
{
"ID" : "tag2",
"TYPE" : "BASIC"
}
]
}
# (tag1) and (tag2 or tag3 or true)
GET /test/_search
{
"query": {
"nested": {
"path": "TAGS",
"query": {
"bool": {
"must": [
{
"match": {
"TAGS.ID": {
"query": "tag1",
"_name": "tag1-query"
}
}
}
],
"should": [
{
"match": {
"TAGS.ID": {
"query": "tag2",
"_name": "tag2-query"
}
}
},
{
"match": {
"TAGS.ID": {
"query": "tag3",
"_name": "tag3-query"
}
}
}
]
}
},
"inner_hits": {}
}
}
}
运行上述查询只会得到以下结果:
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 0.6931472,
"hits" : [
{
"_index" : "test",
"_type" : "_doc",
"_id" : "SaOs8G4BbvPS27u-IouS",
"_score" : 0.6931472,
"_source" : {
"name" : "doc1",
"TAGS" : [
{
"ID" : "tag1",
"TYPE" : "BASIC"
},
{
"ID" : "tag2",
"TYPE" : "BASIC"
}
]
},
"inner_hits" : {
"TAGS" : {
"hits" : {
"total" : 1,
"max_score" : 0.6931472,
"hits" : [
{
"_index" : "test",
"_type" : "_doc",
"_id" : "SaOs8G4BbvPS27u-IouS",
"_nested" : {
"field" : "TAGS",
"offset" : 0
},
"_score" : 0.6931472,
"_source" : {
"ID" : "tag1",
"TYPE" : "BASIC"
},
"matched_queries" : [
"tag1-query"
]
}
]
}
}
}
}
]
}
}
即matched_queries 数组只报告了 tag1-query 的匹配项,而我原本希望它包含 tag1-query 和 tag2-query?
【问题讨论】:
-
在这种情况下,您不需要
OR TRUE,因为无论如何您都会得到您所期望的;-) 您只需将bool/should移出must就可以了去。当使用must时,should子句仅对提升匹配的文档有用。试试看! -
感谢@Val,但如果我将匹配查询移出 bool/should,这不意味着它们在 bool/must 中,因此会与 tag1 查询进行 AND 运算吗?跨度>
-
没有
should数组应该是must数组的兄弟 -
谢谢@Val,试过了,但仍然有问题,用完整的复制步骤更新了我的答案,如果你有时间看看? :-) 谢谢
标签: elasticsearch