0
votes

In elastic search I'm storing records in the trends namespace and trend topic. These are simple objects with only a name (string) and id.

I want to search by name via autocomplete, and for that I'm trying to use the indexed search.

Here's my code to add the index to the name column. Here I'm using a name_suggest column into which I insert the same data as the name column, and is used in the query as well.

  def self.index_column_mappings
    elasticsearch.index({
      index: "trends",
      type: "trend",
      body: {
        mappings: {
          trend: {
            properties: {
              name: {
                type: "string"
              },
              name_suggest: {
                type: "completion"
              }
            }
          }
        }
      }
    })
  end

To start out with I DELETE *. Then I add this column mapping and add some records. Next I run this to search:

  def suggest(term)
    @client.search({
      index: "trends",
      type: "trend",
      body: {
        suggest: {
          name_suggest: {
            prefix: term,
            completion: {
              field: "name_suggest"
            }
          }
        }
      }  
    })
  end

and I get the following error:

Elasticsearch::Transport::Transport::Errors::BadRequest: [400] {"error":{"root_cause":[{"type":"illegal_argument_exception","reason":"Field [name_suggest] is not a completion suggest field"}],"type":"search_phase_execution_exception","reason":"all shards failed","phase":"query","grouped":true,"failed_shards":[{"shard":0,"index":"trends","node":"YVcINtPlSU2u8R2lT9VxPQ","reason":{"type":"illegal_argument_exception","reason":"Field [name_suggest] is not a completion suggest field"}}],"caused_by":{"type":"illegal_argument_exception","reason":"Field [name_suggest] is not a completion suggest field"}},"status":400} from /home/max/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/elasticsearch-transport-5.0.3/lib/elasticsearch/transport/transport/base.rb:201:in `__raise_transport_error'

It seems the important part is this: Field [name_suggest] is not a completion suggest field. I don't understand how I'm failing to set the field up.

btw i'm using Elastic search 5.x


In response to comments here is the result for GET /trends/_mapping:

{
   "trends": {
      "mappings": {
         "trend": {
            "properties": {
               "id": {
                  "type": "long"
               },
               "mappings": {
                  "properties": {
                     "trend": {
                        "properties": {
                           "properties": {
                              "properties": {
                                 "name": {
                                    "properties": {
                                       "type": {
                                          "type": "text",
                                          "fields": {
                                             "keyword": {
                                                "type": "keyword",
                                                "ignore_above": 256
                                             }
                                          }
                                       }
                                    }
                                 },
                                 "name_suggest": {
                                    "properties": {
                                       "type": {
                                          "type": "text",
                                          "fields": {
                                             "keyword": {
                                                "type": "keyword",
                                                "ignore_above": 256
                                             }
                                          }
                                       }
                                    }
                                 }
                              }
                           }
                        }
                     }
                  }
               },
               "name": {
                  "type": "text",
                  "fields": {
                     "keyword": {
                        "type": "keyword",
                        "ignore_above": 256
                     }
                  }
               },
               "name_suggest": {
                  "type": "text",
                  "fields": {
                     "keyword": {
                        "type": "keyword",
                        "ignore_above": 256
                     }
                  }
               }
            }
         }
      }
   }
}
1
Can you please post mappin you have right now ?Vova Bilyachat
N you are not, its code which puts mapping. Open in browser SERVER:9200/trends/_mapping?prettyVova Bilyachat
@VolodymyrBilyachat see updatemax pleaner
do you see that mapping is wrong? As i said in my answer delete INDEX, then make sure index is created, then start indexing your dataVova Bilyachat
I am not sure how ruby works maybe its async so you could sent two requests at the same time.Vova Bilyachat

1 Answers

1
votes

Final answer. You are using wrong method to index data. Look for example here

 def self.index_column_mappings
     client.indices.create({
      index: "trends",
      body: {
        mappings: {
          trend: {
            properties: {
                name:  { type: 'string' },
                name_suggest:  { type: 'completion' }
            }
          }
        }
      }
    })
  end