[英]ElasticSearch query not returns exact match of an array
我有一个关于 Elasticsearch 数组查询的问题。 就我而言,自定义属性的结构是一个对象数组,每个对象包含inner_name
和value
, value
的类型是混合的(可以是字符串、数字、数组、日期等),其中一种类型是多-checkbox,它应该将数组作为输入。 映射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 术语查询会尝试匹配 any ,将运算符视为OR
而不是AND
这是您想要的。 对此有两种解决方案
term
查询,这将提供所需的AND
功能{
"query": {
"bool": {
"must": [
{
"match": {
"custom_attributes.attributes.inner_name": "attr2"
}
},
{
"term": {
"custom_attributes.attributes.value": "val1"
}
},
{
"term": {
"custom_attributes.attributes.value": "val2"
}
},
{
"term": {
"custom_attributes.attributes.value": "val3"
}
},
{
"term": {
"custom_attributes.attributes.value": "val4"
}
}
]
}
}
}
AND
和whitespace
分析器的匹配查询。 如果您的条款包含空格,这将不起作用{
"query": {
"bool": {
"must": [
{
"match": {
"custom_attributes.attributes.inner_name": "attr2"
}
},
{
"match": {
"custom_attributes.attributes.value": {
"query": "val1 val2 val3 val4",
"operator": "and",
"analyzer": "whitespace"
}
}
}
]
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.