1

I am new to Elasticsearch, trying to do some search. I have names of objects like :

Homework
work
jobroles
jobs

I am using wildcard query, but its returning score of 1.0 for each docs.

I want score based on how well it matched. Ex

Ex. If I type

work

score of work > homework
Airn5475
  • 2,452
  • 29
  • 51

1 Answers1

1

Its a good question and directly you can't get the exact match on top, what you need is ngram analyzer which provides the partial matches and another field which stores the exact tokens in lowercase(text field with standard analyzer will solve it).

I've reproduced your issue and solved it using above mentioned approach, Please refer my blog on autocomplete and my this SO answer for in-depth read of various autocomplete/partial searches and why/what/how part of it.

Working example

Create index mapping

{
  "settings": {
    "analysis": {
      "filter": {
        "autocomplete_filter": {
          "type": "ngram",
          "min_gram": 1,
          "max_gram": 10
        }
      },
      "analyzer": {
        "autocomplete": { 
          "type": "custom",
          "tokenizer": "standard",
          "filter": [
            "lowercase",
            "autocomplete_filter"
          ]
        }
      }
    },
    "index.max_ngram_diff" : 10
  },
  "mappings": {
    "properties": {
      "title": {
        "type": "text",
        "analyzer": "autocomplete", 
        "search_analyzer": "standard" 
      },
      "title_lowercase" :{
        "type" : "text"
      }
    }
  }
}

Index your sample docs

{
  "title" : "Homework",
  "title_lowercase" : "Homework"
}   

{
  "title" : "work",
  "title_lowercase" : "work"
}

Search query

{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "title": {
              "query": "work"
            }
          }
        },
        {
          "match": {
            "title_lowercase": {
              "query": "work"
            }
          }
        }
      ]
    }
  }
}

And expected result

 "hits": [
      {
        "_index": "internaledge",
        "_type": "_doc",
        "_id": "1",
      "_score": 0.9926754, /note score of `work` is much higher than`homework`
        "_source": {
          "title": "work",
          "title_lowercase": "work"
        }
      },
      {
        "_index": "internaledge",
        "_type": "_doc",
        "_id": "2",
        "_score": 0.2995283,
        "_source": {
          "title": "Homework",
          "title_lowercase": "Homework"
        }
      }
    ]
Amit
  • 30,756
  • 6
  • 57
  • 88
  • salam.I am getting this error while trying to create index : https://justpaste.it/8do6n –  Aug 07 '20 at 07:42
  • @Imran, are you creating index from fresh ? – Amit Aug 07 '20 at 09:05
  • yes, i fixed that issue. "doc" was missing. its working good, thanks. for examle i have names like work,homework,homeworkinformation searching for information gives me no result,where as info did. Any idea what should be the good size of ngram ? –  Aug 07 '20 at 09:11
  • sure, One last thing, is it possible to apply these new settings to existing index ? –  Aug 07 '20 at 09:41
  • yes, but you have to first close and than open the index :) use the API mentioned in https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-open-close.html – Amit Aug 07 '20 at 10:00
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/219399/discussion-between-opster-elasticsearch-ninja-and-imran). – Amit Aug 07 '20 at 10:18
  • is it possible to create the ngrams like this : homework -> ho,hom,home,homew,homewo,homewor,homework only ? which is only in forward direction ? And thanks for the help last time, I got good grades in the assignment :-) @Opster –  Aug 11 '20 at 17:30
  • 1
    @Imran thats great to hear to that got good marks and yeah its totally possible, would advise you to ask a new question and I shall post the answer there, please comment here as soon as you ask a question :) – Amit Aug 12 '20 at 02:03
  • here you go : https://stackoverflow.com/questions/63423787/how-to-create-ngrams-in-only-forward-direction-in-elasticsearch –  Aug 15 '20 at 07:48