【发布时间】:2020-03-25 20:08:42
【问题描述】:
我有一个关于 Elasticsearch 数组查询的问题。在我的例子中,自定义属性的结构是一个对象数组,每个对象包含inner_name 和value,值的类型是混合的(可以是字符串、数字、数组、日期等),其中一个类型是多复选框,它应该将数组作为输入。如下映射custom_attributes:
"attributes" : {
"properties" : {
"_id" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"inner_name" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"value" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
}
},
我使用mongoosastic 将我的 MongoDB 索引到 ES,所以自定义属性的结构是这样的:
[
{
customer_name: "customerX",
"custom_attributes" : [
{
"group" : "xyz",
"attributes" : [
{
"inner_name" : "attr1",
"value" : 123,
},
{
"inner_name" : "attr2",
"value" : [
"Val1",
"Val2",
"Val3",
"Val4"
]
}
]
}
]
},
{
customer_name: "customerY",
"custom_attributes" : [
{
"group" : "xyz",
"attributes" : [
{
"inner_name" : "attr2",
"value" : [
"Val1",
"Val2"
]
}
]
}
]
}
]
我想执行一个查询,其中所有值都必须在数组中。但是,以下查询的问题在于,只要它包含数组中的任何值,它就会返回文档。这是查询:
{
"query": {
"bool": {
"must": [
{
"match": {
"custom_attributes.attributes.inner_name": "attr2"
}
},
{
"terms": {
"custom_attributes.attributes.value": [
"val1",
"val2",
"val3",
"val4"
]
}
}
]
}
}
}
例如,它返回两个文档,它应该只返回第一个!我的查询有什么问题?有没有另一种方法来编写查询?
【问题讨论】:
-
有人可以帮忙吗?!
标签: elasticsearch elastic-stack mongoosastic