Return 文档的数组字段包含 Elasticsearch 中用户数组中的所有元素 6.x
Return documents with an array field that contains ALL elements from a user array in Elasticsearch 6.x
我的所有文档都有一个 tags
类型 Array
的字段。我想搜索 return 所有具有 tags
的 交叉点 的文档,并带有用户输入数组。元素的数量是可变的,不是固定大小。
例子:
tags:["python", "flask", "gunicorn"]
input:["python"]
- 这会 return
true
因为 所有 input
中的元素都在 tags
. 中
tags:["nginx", "pm2"]
input:["nodejs", "nginx", "pm2", "microservice"]
- 这会 return
false
因为 "nodejs"
和 "microservice"
不在 tags
. 中
我查看了 terms
查询,但我认为它不适用于数组。
我也找到了这个,,但是这个解决方案是针对旧版本的 Elasticsearch 的,语法已经改变了。
我相信您正在寻找 terms_set
- 参考:https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-terms-set-query.html
PUT tags
POST tags/_doc
{
"tags": ["python", "flask", "gunicorn"]
}
POST tags/_doc
{
"tags": ["nginx", "pm2"]
}
GET tags/_search
{
"query": {
"terms_set": {
"tags": {
"terms": ["nginx", "pm2"],
"minimum_should_match_script": {
"source": "params.num_terms"
}
}
}
}
}
返回:
"hits" : {
"total" : 1,
"max_score" : 0.5753642,
"hits" : [
{
"_index" : "tags",
"_type" : "_doc",
"_id" : "XZqN_mkB94Kxh8PwtQs_",
"_score" : 0.5753642,
"_source" : {
"tags" : [
"nginx",
"pm2"
]
}
}
]
}
查询您示例中的完整列表:
GET tags/_search
{
"query": {
"terms_set": {
"tags": {
"terms": ["nodejs", "nginx", "pm2", "microservice"],
"minimum_should_match_script": {
"source": "params.num_terms"
}
}
}
}
}
如预期的那样没有结果:
"hits" : {
"total" : 0,
"max_score" : null,
"hits" : [ ]
}
我的所有文档都有一个 tags
类型 Array
的字段。我想搜索 return 所有具有 tags
的 交叉点 的文档,并带有用户输入数组。元素的数量是可变的,不是固定大小。
例子:
tags:["python", "flask", "gunicorn"]
input:["python"]
- 这会 return
true
因为 所有input
中的元素都在tags
. 中
tags:["nginx", "pm2"]
input:["nodejs", "nginx", "pm2", "microservice"]
- 这会 return
false
因为"nodejs"
和"microservice"
不在tags
. 中
我查看了 terms
查询,但我认为它不适用于数组。
我也找到了这个,
我相信您正在寻找 terms_set
- 参考:https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-terms-set-query.html
PUT tags
POST tags/_doc
{
"tags": ["python", "flask", "gunicorn"]
}
POST tags/_doc
{
"tags": ["nginx", "pm2"]
}
GET tags/_search
{
"query": {
"terms_set": {
"tags": {
"terms": ["nginx", "pm2"],
"minimum_should_match_script": {
"source": "params.num_terms"
}
}
}
}
}
返回:
"hits" : {
"total" : 1,
"max_score" : 0.5753642,
"hits" : [
{
"_index" : "tags",
"_type" : "_doc",
"_id" : "XZqN_mkB94Kxh8PwtQs_",
"_score" : 0.5753642,
"_source" : {
"tags" : [
"nginx",
"pm2"
]
}
}
]
}
查询您示例中的完整列表:
GET tags/_search
{
"query": {
"terms_set": {
"tags": {
"terms": ["nodejs", "nginx", "pm2", "microservice"],
"minimum_should_match_script": {
"source": "params.num_terms"
}
}
}
}
}
如预期的那样没有结果:
"hits" : {
"total" : 0,
"max_score" : null,
"hits" : [ ]
}