简体   繁体   English

ElasticSearch按数组项过滤

[英]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以查找所有记录,其中:

  1. "uIDExtensionFields.key" = "IsAccountCreation" “ uIDExtensionFields.key” =“ IsAccountCreation”
  2. AND "uIDExtensionFields.value" = "true" AND“ uIDExtensionFields.value” =“ true”

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: 更新:通过下面的帮助,我可以解决我的问题:

  1. lowercase the value that I was searching for. 小写我要搜索的值。 (changed "IsAccoutCreation" to "isaccountcreation") (将“ IsAccoutCreation”更改为“ isaccountcreation”)
  2. Updated the mapping so that "uIDExtensionFields" is a nested type 更新了映射,以便“ uIDExtensionFields”是嵌套类型
  3. Updated my filter to the following: 将我的过滤器更新为以下内容:

_ _

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: 这里是您要检查的一些资源:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM