我正在努力进行弹性搜索查询。
这些是示例文档,我想查询这些文档。这些是具有通用属性的文档
[
{
"field1": "value",
"properties": [
{
"propertyBooleanValue": null,
"propertyName": "Product name",
"propertyDateValue": null,
"propertyType": "TEXT",
"propertyStringValue": "SUPER Cool Extreme",
"propertyNumericValue": null
},
{
"propertyBooleanValue": null,
"propertyName": "Product expiration date",
"propertyDateValue": null,
"propertyType": "DATE",
"languageCode": null,
"propertyNumericValue": null
}
]
},
{
"field1": "blah blah",
"properties": [
{
"propertyBooleanValue": null,
"propertyName": "Product name",
"propertyDateValue": null,
"propertyType": "TEXT",
"propertyStringValue": "So boring",
"propertyNumericValue": null
},
{
"propertyBooleanValue": null,
"propertyName": "Product expiration date",
"propertyDateValue": "2020-04-02",
"propertyType": "DATE",
"languageCode": null,
"propertyNumericValue": null
}
]
},
{
"field1": "wow2",
"properties": [
{
"propertyBooleanValue": null,
"propertyName": "Product name",
"propertyDateValue": null,
"propertyType": "TEXT",
"propertyStringValue": "iPear",
"propertyNumericValue": null
},
{
"propertyBooleanValue": null,
"propertyName": "Product expiration date",
"propertyDateValue": null,
"propertyType": "DATE",
"languageCode": null,
"propertyNumericValue": null
}
]
}
]
我只想查询带有嵌套对象的文档,该文档的属性为“ propertyName” =“产品有效期”和“ propertyDateValue” = null
我使用查询,但它返回所有文档:
{
"query": {
"bool": {
"must": [
{
"nested": {
"query": {
"bool": {
"must": [
{
"bool": {
"must_not": [
{
"exists": {
"field": "properties.propertyDateValue"
}
}
]
}
},
{
"term": {
"properties.propertyName": {
"value": "Product expiration date"
}
}
}
]
}
},
"path": "properties"
}
}
]
}
}
}
我们使用弹性搜索7.7
如@jaspreet所述,预期结果。为了进一步详细说明,您可以使用inner_hits
参数仅检索实际上与两个查询都匹配的那些properties
嵌套子文档,即:]]
{ "_source": "inner_hits", <---- hiding the default response "query": { "bool": { "must": [ { "nested": { "query": { "bool": { "must": [ { "bool": { "must_not": [ { "exists": { "field": "properties.propertyDateValue" } } ] } }, { "term": { "properties.propertyName": { "value": "Product expiration date" } } } ] } }, "path": "properties", "inner_hits": {} <----- needs to be here } } ] } } }
屈服
[ { "_index" : "mich", "_type" : "_doc", "_id" : "6iLSVHEBZbobBB0NSl9x", "_score" : 0.6931472, "_source" : { }, "inner_hits" : { "properties" : { "hits" : { "total" : { "value" : 1, "relation" : "eq" }, "max_score" : 0.6931472, "hits" : [ { "_index" : "mich", "_type" : "_doc", "_id" : "6iLSVHEBZbobBB0NSl9x", "_nested" : { "field" : "properties", "offset" : 1 }, "_score" : 0.6931472, "_source" : { "propertyBooleanValue" : null, "propertyName" : "Product expiration date", "propertyDateValue" : null, "propertyType" : "DATE", "languageCode" : null, "propertyNumericValue" : null } } ] } } } }, ... ]
这可能是您想要的。
请注意,上面的查询与以下查询不同,在以下查询中,您有两个单独的bool-must子句,与第一个查询相比,它们忽略了AND连接。在这种情况下,inner_hits
将需要具有唯一的名称。
{ "_source": "inner_hits", "query": { "bool": { "must": [ { "nested": { "path": "properties", "query": { "bool": { "must": [ { "bool": { "must_not": [ { "exists": { "field": "properties.propertyDateValue" } } ] } } ] } }, "inner_hits": { "name": "NULL_propertyDateValue" } } }, { "nested": { "path": "properties", "query": { "bool": { "must": [ { "term": { "properties.propertyName": { "value": "Product expiration date" } } } ] } }, "inner_hits": { "name": "MATCH_IN_propertyName" } } } ] } } }
长话短说,请第一个查询,并随时使用
inner_hits
来限制返回的响应。