[英]Query elastic search documents, where there is null value for property in nested object
我在彈性搜索查詢中苦苦掙扎。
這些是示例文檔,我將對其進行查詢。 這些是具有通用屬性的文檔
[
{
"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
}
]
}
]
我只想查詢帶有嵌套 object 的文檔,該文檔具有“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"
}
}
}
]
}
}
}
長話短說,帶有第一個查詢的 go 並隨意使用inner_hits
限制返回的響應。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.