1

I am trying to implement autocomplete feature using elasticsearch. The structure of the index which I am working on is this

"title": "blog post 1",
"body": "body of the blog post",
"category": "programming",
"locations": [
    {"name": "united states"}, 
    {"name": "new york"}, 
    {"name": "venice"}
]

I am trying to use nested aggregations. The mapping which I am using is this

{                            
  blog : {                   
    mappings : {             
      tag : {                
        properties : {       
          body : {           
            type : string    
          },                 
          category : {       
            type : string    
          },                 
          locations : {      
            properties : {   
              name : {       
                type : string
              }              
            }                
          },                 
          title : {          
            type : string    
          }                  
        }                    
      }                      
    }                        
  }                          
}

The query which is supposed to aggregate the results on the basis of locations.name is

GET /blog/tag/_search
{
    "size": 0, 
    "query": {
        "match": {
            "locations.name": "montreal"
        }
    },
    "aggs": {
        "aggregated_locations": {
            "nested": {
                "path": "locations"
            },
            "aggs": {
                "filtered_locations": {
                   "terms": {
                      "fields": "locations.name"
                   }
                }
            }
        }
    }
}

Currently I am executing the above request using the chrome plugin for elasticsearch. The above query gives out an error, which is very long but I can post it too if someone prompts. I guess I might be wrong in interpreting the meaning of nested aggregations, which means my mapping is wrong. But I am not able to figure out where the problem is.

1 Answer 1

2

There are some missing parts in your mapping, you need to set the locations field to nested type and not analyzing the inner field to be able to aggregate with regards of your expectations:

PUT blog
{
  "mappings": {
    "tag": {
      "properties": {
        "body": {
          "type": "string"
        },
        "category": {
          "type": "string"
        },
        "locations": {
          "type": "nested",
          "properties": {
            "name": {
              "type": "string",
              "index": "not_analyzed"
            }
          }
        },
        "title": {
          "type": "string"
        }
      }
    }
  }
}

Then run following query to filter on nested field, and aggregate on nested field as well:

GET blog/_search
{
  "size": 0, 
  "query": {
    "nested": {
      "path": "locations",
      "query": {
        "match": {
          "locations.name": "montreal"
        }
      }
    }
  },
  "aggs": {
    "aggregated_locations": {
      "nested": {
        "path": "locations"
      },
      "aggs": {
        "filtered": {
          "terms": {
            "field": "locations.name"
          }
        }
      }
    }
  }
}

Those query can be copied / pasted in sense (https://www.elastic.co/guide/en/sense/current/installing.html)

5
  • I used --> curl -XGET 'localhost:9200/blog/_mapping/tag?pretty' >! test.json to get the new mapping. But test.json is a huge file. It has many redundancies. Is this normal behaviour? Feb 2, 2016 at 23:41
  • So what I exactly did was deleted my index, ran the mapping, and then created the new index....is something wrong with my workflow? I am new to elasticsearch. Feb 2, 2016 at 23:42
  • 1
    You can't update a mapping, the workflow is correct, you need to delete your index first. There is a blog post that explains it here (elastic.co/blog/changing-mapping-with-zero-downtime) and also guide to do it with zero downtime. Feb 3, 2016 at 6:29
  • The exact same mapping and query are not working in my chrome extension for sense. Is elasticsearch 2.0 required for using above specified queries and mappings, I am using elasticsearch 1.7 currently Feb 10, 2016 at 13:59
  • The query returns all the elements in the locations list if it finds the particular location defined in the query. I just want the buckets to have only the single location which was queried Feb 10, 2016 at 14:03

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.