过滤掉嵌套 objects ElasticSearch
Filter out nested objects ElasticSearch
美好的一天:
是否可以过滤掉不相关的嵌套object?用例是您有一个 parent,但是 parent 有多个 children。例如,对于特定的 parent...有 100 children;我需要一种方法来过滤掉特定的 children,以便在 100 个中,我得到该结果的一个子集。
已更新
运行下面查询
GET /dev/doc/_search?typed_keys=true
{"_source":{"includes":["reviews"]},"query":{"nested":{"query":{"bool":{"filter":[{"term":{"reviews.userId":{"value":"339c8add-4278-4acd-905e-64b9acabc71a"}}}]}},"path":"reviews"}}}
但是,我得到了以下结果:
"reviews": [
{
"score": 0,
"friendlinessOfStaff": 1,
"amenities": 2,
"grounds": 2,
"reviewDate": "2018-07-03T02:00:34.8735726-07:00",
"qualityOfCare": 4,
"activities": 2,
"facilityReviewReplies": [],
"id": "56a4bac2-85d0-4ccf-aba2-fd9ff74fb3a5",
"message": "blah blah blah",
"userId": "339c8add-4278-4acd-905e-64b9acabc71a",
"cleanliness": 4
},
{
"score": 0,
"friendlinessOfStaff": 1,
"amenities": 2,
"grounds": 2,
"reviewDate": "2018-07-04T12:01:22.228658-07:00",
"qualityOfCare": 4,
"activities": 2,
"facilityReviewReplies": [],
"id": "f2f1b84e-bc1d-4e9c-b6d5-bdc578cb1b5f",
"message": "blah blah blah",
"userId": "7f1d389d-1316-4058-9857-ca51ecd9f5f8",
"cleanliness": 4
}
]
如您所见,此列表中的最后一个 object 有 "userId": "7f1d389d-1316-4058-9857-ca51ecd9f5f8" 但不匹配,过滤器仍在返回值.知道为什么会这样吗?
你用的是什么客户端?
您可能想要这样的东西 (java):
不然就是这个:https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-source-filtering.html
您可以像这样指定要包含和排除的字段:
GET /_search
{
"_source": {
"includes": [ "obj1.*", "obj2.*" ],
"excludes": [ "*.description" ]
},
"query" : {
"term" : { "user" : "kimchy" }
}
}
听起来您是在问如何过滤 return 编辑了哪些内部嵌套对象。如果您询问如何在不过滤这些列中的数据的情况下选择要 return 的列,Andreas 的回答会有所帮助。
至 return 只有与您的查询匹配的嵌套对象才可以使用 inner_hits
。基本上只需将它添加到您的查询子句中,您就应该获得匹配的内部对象。您可以选择添加更多用于分页等的配置选项。
GET index/_search
{
"query": {
"nested": {
"path": "field_name",
"query": { ... },
"inner_hits": {}
}
}
}
美好的一天:
是否可以过滤掉不相关的嵌套object?用例是您有一个 parent,但是 parent 有多个 children。例如,对于特定的 parent...有 100 children;我需要一种方法来过滤掉特定的 children,以便在 100 个中,我得到该结果的一个子集。
已更新
运行下面查询
GET /dev/doc/_search?typed_keys=true
{"_source":{"includes":["reviews"]},"query":{"nested":{"query":{"bool":{"filter":[{"term":{"reviews.userId":{"value":"339c8add-4278-4acd-905e-64b9acabc71a"}}}]}},"path":"reviews"}}}
但是,我得到了以下结果:
"reviews": [
{
"score": 0,
"friendlinessOfStaff": 1,
"amenities": 2,
"grounds": 2,
"reviewDate": "2018-07-03T02:00:34.8735726-07:00",
"qualityOfCare": 4,
"activities": 2,
"facilityReviewReplies": [],
"id": "56a4bac2-85d0-4ccf-aba2-fd9ff74fb3a5",
"message": "blah blah blah",
"userId": "339c8add-4278-4acd-905e-64b9acabc71a",
"cleanliness": 4
},
{
"score": 0,
"friendlinessOfStaff": 1,
"amenities": 2,
"grounds": 2,
"reviewDate": "2018-07-04T12:01:22.228658-07:00",
"qualityOfCare": 4,
"activities": 2,
"facilityReviewReplies": [],
"id": "f2f1b84e-bc1d-4e9c-b6d5-bdc578cb1b5f",
"message": "blah blah blah",
"userId": "7f1d389d-1316-4058-9857-ca51ecd9f5f8",
"cleanliness": 4
}
]
如您所见,此列表中的最后一个 object 有 "userId": "7f1d389d-1316-4058-9857-ca51ecd9f5f8" 但不匹配,过滤器仍在返回值.知道为什么会这样吗?
你用的是什么客户端?
您可能想要这样的东西 (java):
不然就是这个:https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-source-filtering.html
您可以像这样指定要包含和排除的字段:
GET /_search
{
"_source": {
"includes": [ "obj1.*", "obj2.*" ],
"excludes": [ "*.description" ]
},
"query" : {
"term" : { "user" : "kimchy" }
}
}
听起来您是在问如何过滤 return 编辑了哪些内部嵌套对象。如果您询问如何在不过滤这些列中的数据的情况下选择要 return 的列,Andreas 的回答会有所帮助。
至 return 只有与您的查询匹配的嵌套对象才可以使用 inner_hits
。基本上只需将它添加到您的查询子句中,您就应该获得匹配的内部对象。您可以选择添加更多用于分页等的配置选项。
GET index/_search
{
"query": {
"nested": {
"path": "field_name",
"query": { ... },
"inner_hits": {}
}
}
}