2

I’m using Elasticsearch v2.1.1. I have indexed a data set in it with some fields like keywords, Collection etc.

My sample indexed dataset is as follows:

{
  "took": 2,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 103,
    "max_score": 1,
    "hits": [
      {
        "_index": "pibtest1",
        "_type": "SearchTech",
        "_id": "http://www.searchtechnologies.com/images/solutions/candidate-search-match-dashboard.PNG",
        "_score": 1,
        "_source": {
          "Collection": "default_collection",
          "keywords": "keywords-NOT-PROVIDED"
          }
      }
    }
}

Now I want to append comma separated values to the Collection field.

For eg: “Collection”:[ “default_collection”,”wiki_collection”]

Right now, the Collection field is of type “string”. I believe the Collection field needs to be of type array for this. So shall I create an array type mapping before indexing the data in ES? If yes, how shall I do it? I have tried to create the mapping (before indexing the data) like below but it didn’t work and gave me an error.

PUT pibtest1
{
  "mappings":{
    "SearchTech": {
      "properties": {
        "Collection" :{
          "type": "array",
          "index": "analyzed"
        }
      }
    }
  }
}

Error:

{
   "error": {
      "root_cause": [
         {
            "type": "mapper_parsing_exception",
            "reason": "No handler for type [array] declared on field [Collection]"
         }
      ],
      "type": "mapper_parsing_exception",
      "reason": "Failed to parse mapping [SearchTech]: No handler for type [array] declared on field [Collection]",
      "caused_by": {
         "type": "mapper_parsing_exception",
         "reason": "No handler for type [array] declared on field [Collection]"
      }
   },
   "status": 400
}

How do I use the _update api for this? I read that update api will replace the existing value with the new value but I need to append the value to the array. I want to search for a query and update the Collection fields of the results. Thank you.

I found this similar post: Append to an existing elasticsearch array field using python

1 Answer 1

3

You don't need to change anything. In Elasticsearch each field could handle one value or array of values:

      POST index/type/id
      {
      "Collection": "default_collection",
      "keywords": "keywords-NOT-PROVIDED"
      }

and

      POST index/type/id
      {
      "Collection": ["default_collection", "extra value"],
      "keywords": "keywords-NOT-PROVIDED"
      }

both work with same mapping

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

4 Comments

Thanks a lot for your reply. I can update the fields now. I have one more question: How do I search for query and then update the results for all the returned result?
elasticsearch is a document oriented storage with free structure, it is not that easy as in MySQL, for example, but you have options: you can use update by query api, you can use scripting, you can scroll trough results and update them using batch processing.
@pkhlop- Thank you for your time. I found this github.com/yakaz/elasticsearch-action-updatebyquery. Update by query api is still in experimental phase and unfortunately it does not support elasticsearch v2.x.
I tried the update by query plugin for Elasticsearch V1.6.1, it worked perfectly. Is there any similar plugin for this purpose for V2.x? or is there any other way to search and update multiple documents at the same time?

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.