0
votes

I try to create an type-ahead functionality based on Elasticsearch. But I cannot create documents as said in documentation https://www.elastic.co/guide/en/elasticsearch/reference/5.6/search-suggesters-completion.html#completion-suggester-mapping

Here is my dump:

This is my template

PUT /_template/infinity-index
{
    "template": "infinity-index-*",
    "settings": {},
    "mappings": {
      "*": {
        "properties": {
          "id": {
            "type": "integer"
          },
          "status": {
            "type": "keyword",
            "index": true
          },
          "manufacturer": {
            "type": "text",
            "index": true
          },
          "suggest": {
            "type": "completion"
          }
        }
      }
    }
}

Then I send bulk request (here is only one document):

POST _bulk
{"create":{"_id":"60003","_index":"infinity-index-2018-01-13","_type":"default"}}
{"id":60003,"status":"active","manufacturer":"WMV","suggest":[{"input": "WVM", "weight": 0}]}

and I get this error:

{
  "took": 64,
  "errors": true,
  "items": [
    {
      "create": {
        "_index": "infinity-index-2018-01-13",
        "_type": "default",
        "_id": "60003",
        "status": 400,
        "error": {
          "type": "illegal_argument_exception",
          "reason": "[suggest] is defined as an object in mapping [default] but this name is already used for a field in other types"
        }
      }
    }
  ]
}

Can anybody give me a hint, what I did wrong? Thanks

1
Found intereseting behavior: when I create the very first document in the index, then I should use an array value ["WVM"], after that all other documents and the first document can have object value: [{"input": "WVM", "weight": 0}] - Serghei Luchianenco

1 Answers

0
votes

As the error says 'suggest' field already has different types on existing records and ES can't put in the new record because of this. If you have relevant data in this index try testing in a new index, if it's not relevant data just delete and recreate the index and continue testing.
I had the same problem when I tried to store numbers and strings on the same field and the records that didn't match just got lost.

Edit:

Okay, I did more research and I don't know why it says this error, but I checked this page:
https://www.elastic.co/guide/en/elasticsearch/reference/current/search-suggesters-completion.html
and I tried to do what you do without your bulk command and instead I did this:

PUT /_template/infinity-index
{
    "template": "infinity-index-*",
    "settings": {},
    "mappings": {
      "*": {
        "properties": {
          "id": {
            "type": "integer"
          },
          "status": {
            "type": "keyword",
            "index": true
          },
          "manufacturer": {
            "type": "text",
            "index": true
          },
          "suggest": {
            "type": "completion"
          }
        }
      }
    }
}

PUT infinity-index-2018-01-13/type1/1?refresh
{
  "suggest" : [ "WMV", "notWMV"]
}

POST infinity-index-2018-01-13/_search?pretty
{
    "suggest": {
        "manufacture-suggest" : {
            "prefix" : "w", 
            "completion" : { 
                "field" : "suggest" 
            }
        }
    }
}

And the result is this:

{
  ...
  "suggest": {
    "manufacture-suggest": [
      {
        "text": "w",
        "offset": 0,
        "length": 1,
        "options": [
          {
            "text": "WMV",
            "_index": "infinity-index-2018-01-13",
            "_type": "type1",
            "_id": "1",
            "_score": 1,
            "_source": {
              "suggest": [
                "WMV",
                "notWMV"
              ]
            }
          }
        ]
      }
    ]
  }
}

Is this what you tried to achieve?