ElasticSearch 按数组项过滤

jhi*_*den 4 elasticsearch

我在ES中有以下记录:

"authInput" : {
    "uID" : "foo",
    "userName" : "asdfasdfasdfasdf",
    "userType" : "External",
    "clientType" : "Unknown",
    "authType" : "Redemption_regular",
    "uIDExtensionFields" : 
    [
        {
            "key" : "IsAccountCreation",
            "value" : "true"
        }
    ],
    "externalReferences" : []
}
Run Code Online (Sandbox Code Playgroud)

“uIDExtensionFields”是键/值对的数组。我想查询 ES 以查找以下位置的所有记录:

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

这是我认为我应该使用的过滤器,但它永远不会返回任何数据。

GET devdev/authEvent/_search
{
   "size": 10,
    "filter": {
        "and": {
           "filters": [
              {
                  "term": {
                     "authInput.uIDExtensionFields.key" : "IsAccountCreation"
                  }
              },
              {
               "term": {
                  "authInput.uIDExtensionFields.value": "true"
               }   
              }
           ]
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

你们能给我的任何帮助将不胜感激。

干杯!

更新:在以下回复的帮助下,我是如何解决我的问题的:

  1. 小写我正在搜索的值。(将“IsAccoutCreation”更改为“isaccountcreation”)
  2. 更新了映射,使“uIDExtensionFields”成为嵌套类型
  3. 将我的过滤器更新为以下内容:

_

GET devhilden/authEvent/_search
{
   "size": 10,
   "filter": {
      "nested": {
         "path": "authInput.uIDExtensionFields",
         "query": {
            "bool": {
               "must": [
                  {
                     "term": {
                        "authInput.uIDExtensionFields.key": "isaccountcreation"
                     }
                  },
                  {
                     "term": {
                        "authInput.uIDExtensionFields.value": "true"
                     }
                  }                  
               ]
            }
         }
      }
   }
}
Run Code Online (Sandbox Code Playgroud)

Ale*_*vik 5

这里可能有一些问题。

首先,正如 mconlin 指出的那样,您可能有一个与关键字段的标准分析器的映射。它会将密钥小写。您可能想要指定"index": "not_analyzed"该字段。

其次,您必须为此文档结构使用嵌套映射,并在嵌套过滤器中指定键和值。这是因为否则,您将获得以下文档的匹配项:

"uIDExtensionFields" : [
    {
        "key" : "IsAccountCreation",
        "value" : "false"
    },
    {
        "key" : "SomeOtherField",
        "value" : "true"
    }
]
Run Code Online (Sandbox Code Playgroud)

第三,您需要使用 -filter boolmust而不是and确保适当的可缓存性。

最后,您需要将过滤器放入 -query 中filtered。顶级过滤器适用于当您希望过滤命中但不过滤分面/聚合时。这就是为什么它post_filter在 1.0 中被重命名为。

以下是您需要查看的一些资源: