jilesh
jilesh

Reputation: 436

Elasticsearch: Trying to find out appropriate searching query algorithm

I want a result from elastic search in a way where entered search string match with multiple fields of documents and list out the result from indexes is max number of field matching record shows first and least number of field matching record show last in the list.

As e.g: If i am searching keyword "test" and i have more than 12 fields in one record of index.

Now if test is match in 10 fields in 1 record then match in 6 fields in another records and then 2 fields in another records.

I want to show first record in listing with match of search string with maximum number of field match to least number of field match.

As per this example first record show with 10 fields match with search string, second with 6 fields match and 3rd with 2 fields match and go on...

It's good if able to get some good suggestion or example for same.

Upvotes: 1

Views: 169

Answers (1)

jaspreet chahal
jaspreet chahal

Reputation: 9099

This is default behavior of elasticsearch. Documents with more number of matches are scored higher

Query:

{
  "query": {
    "query_string": {
      "default_field": "*", -->search in all fields
      "query": "test"
    }
  }
}

Result:

{
        "_index" : "index18",
        "_type" : "_doc",
        "_id" : "iSCe6nEB8J88APx3YBGn",
        "_score" : 0.9808291,  --> scored higher as two fields match
        "_source" : {
          "field1" : "test",
          "field2" : "test"
        }
      },
      {
        "_index" : "index18",
        "_type" : "_doc",
        "_id" : "iiCe6nEB8J88APx3ghF-",
        "_score" : 0.4700036,
        "_source" : {
          "field1" : "test",
          "field2" : "abc"
        }
      }

Upvotes: 1

Related Questions