[英]ElasticSearch filter by array item
I have the following record in ES: 我在ES中有以下记录:
"authInput" : {
"uID" : "foo",
"userName" : "asdfasdfasdfasdf",
"userType" : "External",
"clientType" : "Unknown",
"authType" : "Redemption_regular",
"uIDExtensionFields" :
[
{
"key" : "IsAccountCreation",
"value" : "true"
}
],
"externalReferences" : []
}
"uIDExtensionFields" is an array of key/value pairs. “ uIDExtensionFields”是键/值对的数组。 I want to query ES to find all records where:
我想查询ES以查找所有记录,其中:
This is the filter that I think I should be using but it never returns any data. 我认为这是我应该使用的过滤器,但它从不返回任何数据。
GET devdev/authEvent/_search
{
"size": 10,
"filter": {
"and": {
"filters": [
{
"term": {
"authInput.uIDExtensionFields.key" : "IsAccountCreation"
}
},
{
"term": {
"authInput.uIDExtensionFields.value": "true"
}
}
]
}
}
}
Any help you guys could give me would be much appreciated. 大家能给我的任何帮助将不胜感激。
Cheers! 干杯!
UPDATE: WITH THE HELP OF THE RESPONSES BELOW HERE IS HOW I SOLVED MY PROBLEM: 更新:通过下面的帮助,我可以解决我的问题:
_ _
GET devhilden/authEvent/_search
{
"size": 10,
"filter": {
"nested": {
"path": "authInput.uIDExtensionFields",
"query": {
"bool": {
"must": [
{
"term": {
"authInput.uIDExtensionFields.key": "isaccountcreation"
}
},
{
"term": {
"authInput.uIDExtensionFields.value": "true"
}
}
]
}
}
}
}
}
There are a few things probably going wrong here. 这里可能有些错误。
First, as mconlin points out, you probably have a mapping with the standard analyzer for your key field. 首先,正如mconlin所指出的,您可能在关键字段上使用了标准分析仪进行了映射。 It'll lowercase the key.
它将小写密钥。 You probably want to specify
"index": "not_analyzed"
for the field. 您可能要为该字段指定
"index": "not_analyzed"
。
Secondly, you'll have to use nested mappings for this document structure and specify the key and the value in a nested filter. 其次,您必须为此文档结构使用嵌套映射,并在嵌套过滤器中指定键和值。 That's because otherwise, you'll get a match for the following document:
这是因为否则,您将获得以下文档的匹配项:
"uIDExtensionFields" : [
{
"key" : "IsAccountCreation",
"value" : "false"
},
{
"key" : "SomeOtherField",
"value" : "true"
}
]
Thirdly, you'll want to be using the bool
-filter's must
and not and
to ensure proper cachability. 第三,您将要使用
bool
-filter的must
而不是), and
确保适当的可处理性。
Lastly, you'll want to put your filter in the filtered
-query. 最后,您需要将过滤器放入
filtered
-query中。 The top-level filter is for when you want hits to be filtered, but facets/aggregations to not be. 顶级过滤器适用于您希望对匹配进行过滤而对构面/聚合不进行过滤的情况。 That's why it's renamed to
post_filter
in 1.0. 这就是为什么在1.0
post_filter
其重命名为post_filter
的原因。
Here's a few resources you'll want to check out: 这里是您要检查的一些资源:
Troubleshooting Elasticsearch searches, for Beginners covers the first two issues. 对Elasticsearch搜索进行故障排除(针对初学者)涵盖了前两个问题。
Managing Relations in ElasticSearch covers nested docs (and parent/child) 在ElasticSearch中管理关系涵盖了嵌套文档(以及父/子)
all about elasticsearch filter bitsets covers and
vs. bool
. 所有关于elasticsearch过滤器位集盖
and
与bool
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.