Elasticsearch Multi field search with high preference to exact matches

Viewed 19

I have two Fields, Content and Page Title on which i am running my search. Basically what i am trying to achieve is , if the query is present exactly in the document content or page title, then that document should get a higher score and a higher position in the search response.

I have tried the below queries, however , we are getting results where the exact phrases are overpowered by terms being present many times in the document. what am i doing wrong ? I have gone through elastic documentation and have tried quite a few permutations. But i think logically i am making a mistake which is of setting them from intended behaviour.

The mappings are all standard (tokenizer n gram{1-18} and all lowercased) and the Content and Page Title fields are text fields indexed for search.

The first query i am trying is as follows.

{
   "_source":"false",
   "fields":[
      "Page Title",
      "Url"
   ],
   "query":{
      "bool":{
         "should":{
            "bool":{
               "should":{
                  "match":{
                     "Content":"user_query"
                  }
               },
               "should":{
                  "match":{
                     "Page Title":"user_query"
                  }
               }
            }
         },
         "must":{
            "bool":{
               "must":{
                  "match_phrase":{
                     "Content":"user_query"
                  }
               },
               "should":{
                  "match_phrase":{
                     "Page Title":"user_query"
                  }
               }
            }
         }
      }
   },
   "highlight":{
      "order":"score",
      "force_source":"true",
      "require_field_match":"false",
      "pre_tags":[
         "<em>"
      ],
      "post_tags":[
         "</em>"
      ],
      "fields":{
         "Content":{
            "highlight_query":{
               "match":{
                  "Content":{
                     "query":"user_query",
                     "operator":"AND",
                     "analyzer":"standard",
                     "fuzziness":0,
                     "lenient":"false",
                     "auto_generate_synonyms_phrase_query":"false",
                     "fuzzy_transpositions":"false"
                  }
               }
            }
         }
      }
   }
}

The other query i used is as follows

search_dict={
   "_source":"false",
   "fields":[
      "Page Title",
      "Url",
   ],
   "query":{
      
      "bool":{
         "must":[
            {
               "simple_query_string":{
                  "query":"<USER_QUERY>~<SLOP>",
                  "fields":[
                     "Page Title^3",
                     "Content"
                  ],
                  "default_operator":"and",
                  "analyze_wildcard":"true",
                  "analyzer":"standard",
                  "boost":5,
                  "fuzzy_max_expansions":1,
                  "auto_generate_synonyms_phrase_query":"false",
                  "fuzzy_transpositions":"false",
                  "minimum_should_match":"75%"
               }
            },
            {
               "simple_query_string":{
                  "query":"<USER_QUERY>~<SLOP>",
                  "fields":[
                     "Page Title^3",
                     "Content"
                  ],
                  "default_operator":"OR",
                  "analyze_wildcard":"true",
                  "analyzer":"standard",
                  "boost":3,
                  "fuzzy_max_expansions":1,
                  "auto_generate_synonyms_phrase_query":"false",
                  "fuzzy_transpositions":"false",
                  "minimum_should_match":"75%"
               }
            }
         ]
      }
   },
   "from":"0",
   "size":"10",
   "aggs":{
      "Field1":{
         "terms":{
            "field":"field 1",
            "size":30
         }
      },
      "Field2":{
         "terms":{
            "field":"field 2",
            "size":30,
         }
      }
   },
   "highlight":{
      "order":"score",
      "force_source":"true",
      "require_field_match":"false",
      "pre_tags":[
         "<em>"
      ],
      "post_tags":[
         "</em>"
      ],
      "fields":{
         "Content":{
            "highlight_query":{
               "match":{
                  "Content":{
                     "query":"<USER_QUERY>",
                     "operator":"AND",
                     "analyzer":"standard",
                     "fuzziness":0,
                     "lenient":"false",
                     "auto_generate_synonyms_phrase_query":"false",
                     "fuzzy_transpositions":"false"
                  }
               }
            }
         }
      }
   }
}

For mapping please note

{
  "settings":{
    "analysis":{
          "filter":{
                "autocomplete_filter":{
                          "type":"edge_ngram",
                          "min_gram":1,
                          "max_gram":18
                          }
              },
          "analyzer":{
                "autocomplete":{
                        "type":"custom",
                        "tokenizer":"standard",
                        "filter":[
                              "lowercase",
                              "autocomplete_filter"
                            ]
                        }
                }
        }
    },
   "mappings": {
    "dynamic": "true",
    "_source": {
      "enabled": "true"
    },
    "properties": {
      "user": {
        "type": "keyword"
      },
      "creationDate": {
        "type": "date"
      },
      "tags": {
        "type": "keyword"
      },
      "body": {
        "type": "text"
      },
      "did":{
        "type": "text"
      },
      "Field1":{
        "type": "keyword",
               "fields": {
                  "raw": {
                     "type": "keyword"
                  }
               }
      },
      "Field2":{
        "type": "keyword",
               "fields": {
                  "raw": {
                     "type": "keyword"
                  }
               }
      },
    "doc": {"type": "binary"}
    }
  }
}
0 Answers
Related