Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elasticsearch more like this query using like_text

I'm having some frustration using the more like this query.

Here is my index creation:

curl -XPUT 'http://127.0.0.1:9200/food/' -d 
'{
  "mappings": {
    "fruit": {
      "properties": {
        "term": {
          "type": "string",
          "term_vector": "yes"
        }
      }
    }
  }
}'

And here is a sample piece of data in that index:

{
  "_index": "food",
  "_type": "fruit",
  "_id": "2",
  "_score": 1,
  "_source": {
    "term": "yellow",
    "property_ids": [
      1
    ],
    "id": 2
  }
}

Lastly here is the more like this query I use against it to attempt to return data:

curl -XGET 'http://127.0.0.1:9200/food/_search' -d '{
  "query": {
    "more_like_this": {
      "fields": [
        "term"
      ],
      "like_text": "yellow",
      "min_term_freq": 1,
      "max_query_terms": 12
    }
  }
}
'

The issue is when I do this search I get no results back:

{"took":2,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":0,"max_score":null,"hits":[]}}%

If I do a standard wildcard query for just "yellow" I get that result back. What am I missing?

like image 687
Dave Holland Avatar asked Mar 16 '23 09:03

Dave Holland


1 Answers

By default min_doc_freq is 5 (check doc) so, your query is not working because your index doesn't contain at least 5 documents whose term property holds yellow. So, set min_doc_freq to 1 in your query and it should work. Example:

{
    "query": {
        "more_like_this": {
            "fields": [
                "term"
             ],
             "like_text": "yellow",
             "min_term_freq": 1,
             "min_doc_freq": 1,
             "max_query_terms": 12
         }
    }
}
like image 165
moliware Avatar answered Mar 25 '23 05:03

moliware