Elasticsearch 5 Determining if nested field exists

Consider an index with documents such as these, where the team location may or may not be populated:

{
  "status" : "active",
  "name" : "Jimmy Chitwood",
  "team" : {
    "name" : "Hickory Huskers",
    "location" : "Hickory, Indiana"
  } 
}

Note how the document has a nested object “team”.

With Elastic 5.0, I wanted to run a query against this index that would only return documents that have a team.location.  I needed to wrap the “exists” query in a “nested” query.

This is the query I used:

{
  "from": 0,
  "size": 100,
  "query": {
    "bool": {
      "must": [
      {
        "nested": {
          "path": "team",
          "query": {
            "exists": {
              "field": "team.location"
            }
          }
        }
      }
      ]
    }
  }
}

 

Advertisements

About stevewall123

I am a Lead Software Engineer in Minneapolis working for Thomson Reuters. I am currently working on projects using Java, JavaScript, Spring, Drools, Hazelcast, Liquibase and Tomcat. Previously, I used C#, GWT, Grails, Groovy, JMS and JBoss Drools Guvnor. In the past I have worked on projects using J2EE, Swing, Webwork, Hibernate, Spring, Spring-WS, JMS, JUnit and Ant.
This entry was posted in Elasticsearch and tagged . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s