0
votes

I want to search text in the following order and give relevence score accordingly:

  1. match for exact phrase
  2. All words (exact word) should appear in search result
  3. some or atleast one word should appear in result

Here is my query:

{
  "_source": [
    "title"
  ],
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "title": {
              "query": "introduction to java",
              "fuzziness": 0,
              "operator": "and",
              "boost": 1
            }
          }
        },
        {
          "match_phrase": {
            "title": {
              "query": "introduction to java",
              "boost": 5
            }
          }
        }
      ],
      "minimum_should_match": 1
    }
  },
  "size": 20
}

But I am getting Javascript or jQuery first instead of java. Result:

{
"_score": 594.7316,
"_source": {
"title": "Introduction to jQuery Web Development"
}
}
,
{
"_score": 592.86993,
"_source": {
"title": "Introduction to JavaScript Development"
}
}
,
{
"_score": 592.8397,
"_source": {
"title": "A Comprehensive Introduction to Java Virtual Machine (JVM)"
}
}
,
{
"_score": 591.7474,
"_source": {
"title": "Introduction to Java for Programmers"
}
} 

What should I do to make this happen ? Thanks in advance.

1
Your query works fine for me - except that using "operator": "and" will break your third rule of including results that contain at least one of the terms in it (change it to "or" or just remove it). Maybe try using the Explain option to see how your results are being scored.tchambers
explain option is nice. I got something: "description": "weight(title:"(i in int intr intro introd introdu introduc introduct introducti introductio introduction) (t to) (j ja jav java)" in 5759) [PerFieldSimilarity], result of:" It is searching for any one of edge ngram here. but I'm not using any ngram analyzer hereRaj Kumar

1 Answers

0
votes

Okay so this one is my mistake. I applied edge_ngram analyzer on title field using mapping api so thats why it was searching for j/ja/jav/java.

I deleted the index and created a new one without this mapping and its working fine for me.