我正在努力进行 flex 搜索查询。
这些是示例文档,我想查询这些文档。这些是具有通用属性的文档
[
{
"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"
}
}
]
}
}
}
我们使用 flex 搜索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
限制返回的响应。
关于elasticsearch - 查询 Elasticsearch 文档,其中嵌套对象中的属性值为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61079978/