4

I have the following record in 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. I want to query ES to find all records where:

  1. "uIDExtensionFields.key" = "IsAccountCreation"
  2. 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")
  2. Updated the mapping so that "uIDExtensionFields" is a nested type
  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"
                     }
                  }                  
               ]
            }
         }
      }
   }
}
2
  • 1
    try lower case key value of "isaccountcreation", depending on your mapping es might not care about case. Commented Feb 27, 2014 at 20:59
  • changing "IsAccountCreation" to "isaccountcreation" did indeed fix that part of my query, thank you very much! Commented Feb 27, 2014 at 21:57

1 Answer 1

5

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. It'll lowercase the key. You probably want to specify "index": "not_analyzed" for the field.

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.

Lastly, you'll want to put your filter in the 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.

Here's a few resources you'll want to check out:

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you mconlin, you were correct that if I lower cased it then it worked when just searching on the key. @Alex, thank you for the suggestion about nesting, you are quite right. My question back to you: is that supported in ES 0.9x or do you need the 1.0 version to get that nesting? My updated query is working, but still has the nesting issue that needs to be fixed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.