Saturday 14 November 2015

Elasticsearch: copy to field

Adding copy_to parameter to any field mapping will cause all values of this field to be copied to fields specified in the parameter.

For example, let’s create mapping for type person in index organization.
PUT /organization/_mapping/person
{
  "properties": {
    "firstName": {
      "type":     "string",
      "copy_to":  "fullName" 
    },
    "lastName": {
      "type":     "string",
      "copy_to":  "fullName" 
    },
    "fullName": {
      "type":     "string"
    }
  }
}


Insert following data into type person.
PUT /_bulk
{"create" : {"_index" : "organization", "_type":"person", "_id" : "1"}}
{"firstName" : "Hari Krishna", "lastName" : "Gurram"}
{"create" : {"_index" : "organization", "_type":"person", "_id" : "2"}}
{"firstName" : "Shanmugam", "lastName" : "Chinnappaiayan"}
{"create" : {"_index" : "organization", "_type":"person", "_id" : "3"}}
{"firstName" : "Murugesh", "lastName" : "kulakarni"}


You can query on field fullName like below.

GET /organization/person/_search
{
  "query" :{
    "match" :{
      "fullName" : "krishna murugesh"
    }
  }
}

You will get following response.

{
   "took": 1,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 2,
      "max_score": 0.19551794,
      "hits": [
         {
            "_index": "organization",
            "_type": "person",
            "_id": "1",
            "_score": 0.19551794,
            "_source": {
               "firstName": "Hari Krishna",
               "lastName": "Gurram"
            }
         },
         {
            "_index": "organization",
            "_type": "person",
            "_id": "3",
            "_score": 0.15891947,
            "_source": {
               "firstName": "Murugesh",
               "lastName": "kulakarni"
            }
         }
      ]
   }
}



    
Prevoius                                                 Next                                                 Home

No comments:

Post a Comment