k16
k16

Reputation: 481

How to use the Phrase Suggester in the Elasticsearch?

I want to use the Phrase Suggester in the Elasticsearch 1.7.

https://www.elastic.co/guide/en/elasticsearch/reference/current/search-suggesters-phrase.html

So, I created below query like domcument pages sample, but I have gotten the error.

nested: ElasticsearchIllegalArgumentException[No mapping found for field [itemname]];

$ curl -XPOST 'localhost:9200/_search?pretty' -d '{
  "query": {
    "function_score": {
      "query": {
        "filtered": {
          "query": {
            "query_string": {
              "fields": [
                "itemname"
              ],
              "query": "cola"
            }
          }
        }
      }
    }
  },
  "suggest": {
    "text": "cola",
    "simple_phrase": {
      "phrase": {
        "field": "itemname",
        "size": 5,
        "real_word_error_likelihood": 0.95,
        "max_errors": 0.5,
        "gram_size": 2
      }
    }
  }
}'

But the field [itemname]] is definitely defined.
In fact, I can search from the itemname field with this query.

$ curl -XPOST 'localhost:9200/_search?pretty' -d '{
  "query": {
    "function_score": {
      "query": {
        "filtered": {
          "query": {
            "query_string": {
              "fields": [
                "itemname"
              ],
              "query": "cola"
            }
          }
        }
      }
    }
  }
}'
{
  "took" : 9,
  "timed_out" : false,
  "_shards" : {
    "total" : 15,
    "successful" : 15,
    "failed" : 0
  },
  "hits" : {
    "total" : 97,
    "max_score" : 11.625176,
    "hits" : [ {
      "_index" : "my_index",
      "_type" : "my_type",
      "_id" : "20615",
      "_score" : 11.625176,
      "_source":{"itemid":"20615","itemname":"cola 500ml"}
    }, {

In this case what's wrong with me ?
Does anyone advise me how to use the Phrase Suggester properly ?

Thanks.

Add my settings

# curl -XGET 'http://localhost:9200/my_index?pretty'
{
  "my_index" : {
    "aliases" : { },
    "mappings" : {
      "my_type" : {
        "_all" : {
          "enabled" : true,
          "analyzer" : "kuromoji_analyzer"
        },
        "properties" : {
          "itemid" : {
            "type" : "string",
            "index" : "not_analyzed",
            "store" : true
          },
          "catname" : {
            "type" : "string",
            "store" : true,
            "analyzer" : "kuromoji_analyzer"
          },
          "itemname" : {
            "type" : "string",
            "store" : true,
            "analyzer" : "kuromoji_analyzer"
          },
          "myscore" : {
            "type" : "double",
            "store" : true
          },
          "subcatname" : {
            "type" : "string",
            "store" : true,
            "analyzer" : "kuromoji_analyzer"
          }
        }
      }
    },

Upvotes: 1

Views: 1761

Answers (1)

Val
Val

Reputation: 217254

I think since you're running your suggester query on the root endpoint /, your search hits another index which doesn't have any mapping type defining the itemname field.

Try running your query directly on the index which has the mapping type that defines the itemname field.

According to the results of your second query, you should try running your suggester on /my_index/my_type instead of the root endpoint /

                        add the index and the type
                                |      |
                                v      v
curl -XPOST 'localhost:9200/my_index/my_type/_search?pretty' -d '{
  "query": {
    "function_score": {
      "query": {
        "filtered": {
          "query": {
            "query_string": {
              "fields": [
                "itemname"
              ],
              "query": "cola"
            }
          }
        }
      }
    }
  },
  "suggest": {
    "text": "cola",
    "simple_phrase": {
      "phrase": {
        "field": "itemname",
        "size": 5,
        "real_word_error_likelihood": 0.95,
        "max_errors": 0.5,
        "gram_size": 2
      }
    }
  }
}'

Upvotes: 1

Related Questions