Search code examples
elasticsearch

How to let Elasticsearch exclude the result which matched low score?


Elasticsearch return low scroe result in tail ,it do well. But my customer's requirment is not seem them.

insert the test data

curl -X POST "http://127.0.0.1:9200/test/_create/1?pretty" -H 'Content-Type: application/json' -d'{"body": "I love my cat"}'
curl -X POST "http://127.0.0.1:9200/test/_create/2?pretty" -H 'Content-Type: application/json' -d'{"body": "my cat"}'
curl -X POST "http://127.0.0.1:9200/test/_create/3?pretty" -H 'Content-Type: application/json' -d'{"body": "little cat"}'

get the result

curl -X GET "http://127.0.0.1:9200/test/_search?pretty" -H 'Content-Type: application/json'  -d'{  "query": { "match": { "body": "I love my cat" } } }'

It will return

{
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : 2.1295943,
    "hits" : [
      {
        "_index" : "test",
        "_id" : "1",
        "_score" : 2.1295943,
        "_source" : {
          "body" : "I love my cat"
        }
      },
      {
        "_index" : "test",
        "_id" : "2",
        "_score" : 0.6722922,
        "_source" : {
          "body" : "my cat"
        }
      },
      {
        "_index" : "test",
        "_id" : "3",
        "_score" : 0.14874382,
        "_source" : {
          "body" : "little cat"
        }
      }
    ]
  }
}

But I only want the "I love my cat", no the other two data.

I have try function_score but it still keep returning the result


Solution

  • By default, full text search feature is enabled. So when you index a string Elasticsearch will analyze the text.

    The string will be saved as both text and keyword.

    If you want exact match search use term query with keyword field type

    { "query": { "term": { "body.keyword": "I love my cat" } } }
    

    If you want a score threshold you can use min_score parameter like @val mentioned.

    {"query":{"match":{"body":"I love my cat"}},"min_score":1}