1
votes

If we search for "Padma Priya" and is there any exact match then we need to show that result first but what happening is It's showing "Padma" posts first only because of keyword density and weightage.

If there is no "Padma Priya" then we want to show "Padma" results and "Priya" Results as per keyword density and weightage.

If we found both "Padma Priya" Keyword in Text and the URL then we need to give high priority to URL then Title after that page content.

This is my query:

{
            searchBody = {
                "from" : 0,
                "size" : size,
                "query": {
                    "bool": {
                        "should" : [
                            {
                              "match": {
                                "location": {
                                  "query": q,
                                  // "boost": 5
                                }
                              }
                            },
                            {
                              "match": {
                                "title": {
                                  "query": q,
                                  "boost": 5
                                }
                              }
                            }
                            ,
                            {
                              "match": {
                                "description": {
                                  "query": q
                                }
                              }
                            }
                          ]
                    }
                }
            };

}

1
@can you please add your index mapping, sample docs, your search query so that I can use that to provide the correct query?user156327

1 Answers

1
votes

As shown in ES boost doc first query, you can see how to give more boost/priority to your title fields than the page content which is very common use-case.

{
  "mappings": {
    "properties": {
      "title": {
        "type": "text",
        "boost": 2 ---> note this same text will get twice priority/boost if found in title field.
      },
      "content": {
        "type": "text"
      }
    }
  }
}

for your first use-case of If we search for "Padma Priya" and is there any exact match, you need to combine the phrase query with your existing query to get the result at the top.

Concept will be clear by below example:

Index sample doc, index will be created automatically.

{
   "title" : "Padma is the author of post",
   "content" : "If we search for Padma and is there any exact match then we need to show that result first but what happening is It's showing Padma posts first only because of keyword density and weightage."
}

Index another doc which has padam priya as a phrase:

{
   "title" : "Padma Priya",
   "content" : "If we search for Padma and is there any exact match then we need to show that result first but what happening is It's showing Padma posts first only because of keyword density and weightage."
}

Search query

{
    "query": {
        "bool": {
            "should": [
                {
                    "multi_match": {
                        "query": "Padma Priya",
                        "fields": [
                            "title^3", --> this will give 3X priority of title field
                            "content"
                        ]
                    }
                },
                {
                    "match_phrase": {
                        "title": "Padma Priya"
                    }
                }
            ]
        }
    }
}

And search result

 "hits": [
            {
                "_index": "indexboost",
                "_type": "_doc",
                "_id": "1",
                "_score": 1.8336343, // note high score for exact match
                "_source": {
                    "title": "Padma Priya",
                    "content": "If we search for Padma and is there any exact match then we need to show that result first but what happening is It's showing Padma posts first only because of keyword density and weightage."
                }
            },
            {
                "_index": "indexboost",
                "_type": "_doc",
                "_id": "2",
                "_score": 0.9081677,
                "_source": {
                    "title": "Padma is the author of post",
                    "content": "If we search for Padma and is there any exact match then we need to show that result first but what happening is It's showing Padma posts first only because of keyword density and weightage."
                }
            }
        ]