Search code examples
elasticsearchbulkcompletion

bulk insert elasticSearch for autocomplete suggestion


How to bulk insert into suggestions

here is mapping

PUT /myindex/autocomplete/_mapping
{
   "autocomplete": {
      "properties": {
         "name": {
            "type": "string"
         },
         "suggest": {
            "type": "completion",
            "index_analyzer": "simple",
            "search_analyzer": "simple",
            "payloads": true
         }
      }
   }
}

here is few data that works if I inert then one by one

POST /myindex/autocomplete/
{
   "name": "Nevermind",
   "suggest": {
      "input": [
         "Nevermind",
         "Nirvana"
      ],
      "output": "Nirvana - Nevermind",
      "payload": {
         "tip": 1,
         "oid": 1
      },
      "weight": 34
   }
}

how to do a BULK insert of suggestions?

PUT /myindex/autocomplete/_bulk
{
   "name": "Nevermind",
   "suggest": {
      "input": [
         "Nevermind"
      ],
      "output": "Nirvana - Nevermind",
      "payload": {
         "tip": 1,
         "oid": 1
      },
      "weight": 34
   }
}
{
   "name": "Bleach",
   "suggest": {
      "input": [
         "Bleach"
      ],
      "output": "Nirvana - Bleach",
      "payload": {
         "tip": 2,
         "oid": 3
      },
      "weight": 20
   }
}

Solution

  • You need to format your data according to what the bulk operation expects, i.e. one line for the "index" action and another line with the JSON source all separated by new line characters (including on the last line):

    curl -XPOST localhost:9200/myindex/autocomplete/_bulk -d '
    { "index":{"_id": 1} }
    { "name": "Nevermind",  "suggest": { "input": [ "Nevermind" ], "output": "Nirvana - Nevermind", "payload": {  "tip": 1, "oid": 1 }, "weight": 34 } }
    { "index":{"_id": 2} }
    { "name": "Bleach",  "suggest": { "input": [ "Bleach" ], "output": "Nirvana - Bleach", "payload": {  "tip": 2, "oid": 3 }, "weight": 20 } }
    '