Search code examples
ruby-on-railselasticsearchsearchkick

Searchkick highlights unnecessary word


I'm highlighting unnecessary word for example I'm search for "Document CASE No. 2015-331" here the list that searchkick will highlight

  1. "Document CASE No. 2015-331"
  2. "Not"
  3. "no"
  4. "on"
  5. "case is"<----- this is very weird i dont know why this is
  6. highlighted lol
  7. "2015"
  8. "2017"
  9. "2018"
  10. "2016"
  11. "to"
  12. "Not to"

here's what's my search looks like

    search = ::Document.search params[:q], fields: [:content], where: {id:
     params[:id]}, highlight: { tag: 'span class=match-matcher',
     fragment_size: @document.content.length}

    search.with_highlights.each do |document, highlights|

       document.content = highlights[:content]

    end

the goal here to highlight the "Document CASE No. 2015-331" only


Solution

  • Looks like your field has been analyzed when indexed. If you want to achieve the exact match and tokens should be searchable the mapping should be "not_analyzed" and the data needs to be re-indexed.

    Here you are looking for exact match.

    Modify the mapping for you field by adding something like below.

    "mappings": {
        "properties": {
           "city": {
            "type": "text",
             "fields": {
               "raw": { 
                 "type":  "keyword"
               }
              }
            }
          }
        }