elena
elena

Reputation: 4178

Higher score for first word in ElasticSearch

Right now my search gives me unwanted results when I search, say for "egg". I get following:

_score: 2.7645843
_source:
  django_id: "18003"
  text: "Bagels, egg"
  content_auto: "Bagels, egg"
  django_ct: "web.fooddes"
  allergies: []
  outdated: false
  id: "web.fooddes.18003"
_explanation:
  value: 2.7645843
  description: "weight(_all:egg in 516) [PerFieldSimilarity], result of:"
  details:
  - value: 2.7645843
    description: "fieldWeight in 516, product of:"
    details:
    - value: 1.4142135
      description: "tf(freq=2.0), with freq of:"
      details:
      - value: 2.0
        description: "termFreq=2.0"
    - value: 5.21295
      description: "idf(docFreq=26, maxDocs=1824)"
    - value: 0.375
      description: "fieldNorm(doc=516)"

as the first result.

And only as fifth or even further the wanted one:

_score: 2.380459
_source:
  django_id: "01124"
  text: "Egg, white, raw, fresh"
  content_auto: "Egg, white, raw, fresh"
  django_ct: "web.fooddes"
  allergies: []
  outdated: false
  id: "web.fooddes.01124"
_explanation:
  value: 2.3804593
  description: "weight(_all:egg in 1489) [PerFieldSimilarity], result of:"
  details:
  - value: 2.3804593
    description: "score(doc=1489,freq=2.0), product of:"
    details:
    - value: 0.99999994
      description: "queryWeight, product of:"
      details:
      - value: 5.386365
        description: "idf(docFreq=22, maxDocs=1848)"
      - value: 0.18565395
        description: "queryNorm"
    - value: 2.3804595
      description: "fieldWeight in 1489, product of:"
      details:
      - value: 1.4142135
        description: "tf(freq=2.0), with freq of:"
        details:
        - value: 2.0
          description: "termFreq=2.0"
      - value: 5.386365
        description: "idf(docFreq=22, maxDocs=1848)"
      - value: 0.3125
        description: "fieldNorm(doc=1489)"

That is because the first result has less words in it and consequently the result has higher score, because "egg" is more relevant in this case.

However, I want the first word encountered in the result to be the most important. So if I search for word "egg", it should first and foremost show those results that start with this word. Any ideas how to implement this?

Upvotes: 3

Views: 1057

Answers (1)

elena
elena

Reputation: 4178

Figured it out thanks to people at https://discuss.elastic.co.

  "query": {
      "bool": {
        "must": { "match": { "_all": request_text }},
        "should":
        {
          "span_first" : {
            "match" : {
                "span_term" : { "_all" : request_text }
            },
            "end" : 1
          }
        }
      }
    }

Upvotes: 3

Related Questions