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

A Coder Gamer
  • 840
  • 1
  • 10
  • 31

1 Answers1

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

pkhlop
  • 1,824
  • 3
  • 18
  • 23
  • 1
    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? – A Coder Gamer Apr 28 '16 at 17:59
  • 2
    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](https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-update-by-query.html), you can use [scripting](http://stackoverflow.com/a/36312926/604530), you can [scroll](https://www.elastic.co/guide/en/elasticsearch/reference/2.3/search-request-scroll.html) trough results and update them using [batch](https://www.elastic.co/guide/en/elasticsearch/reference/2.3/_batch_processing.html) processing. – pkhlop Apr 28 '16 at 18:11
  • 2
    @pkhlop- Thank you for your time. I found this https://github.com/yakaz/elasticsearch-action-updatebyquery. Update by query api is still in experimental phase and unfortunately it does not support elasticsearch v2.x. – A Coder Gamer Apr 29 '16 at 22:07
  • 1
    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? – A Coder Gamer Apr 29 '16 at 22:24