1

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.

jilesh
  • 436
  • 1
  • 3
  • 13

1 Answers1

1

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"
        }
      }
jaspreet chahal
  • 8,817
  • 2
  • 11
  • 29
  • There is no need to specify default_field if we want to search all fields as "*" is the default value. (unless it is changed in index.query.default_field) – Kumar V May 06 '20 at 19:59
  • what if we want to search in some fields instead of all fields? add that fields comma saperated instead of *? – jilesh May 07 '20 at 04:41
  • @jilesh , yes you can pass as "default_field": ["field1","field2"] – jaspreet chahal May 07 '20 at 04:50
  • @jaspreetchahal, Okay one more thing if we pass it as mention in your comment in that case it will set priority by default i mean first check from field1 and then for field2? – jilesh May 07 '20 at 05:21
  • @jilesh it will be "fields": ["field1","field2"] instead of "default_field". All the fields are checked. Documents with more matches are scored higher – jaspreet chahal May 07 '20 at 05:26
  • @jaspreetchahal, Okay sounds good and is this good way to go with this way i mean i want to find in more than 10 fields of one record or any other optimistic solution you suggest? – jilesh May 07 '20 at 05:28
  • @jilesh it is fine , If you are searching over entire index, you can leave out fields. If a subset of fields you will have to mention them – jaspreet chahal May 07 '20 at 05:33
  • @jaspreetchahal ,It's not working with the fields only in query string i think i have to use multi_match on query_string. correct? – jilesh May 07 '20 at 10:24
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/213320/discussion-between-jaspreet-chahal-and-jilesh). – jaspreet chahal May 07 '20 at 10:33