Search code examples
elasticsearchautosuggestsearch-suggestion

Elastic Search word suggestion


We want elastic search to suggest words found within the index. We are using version: 5.6.4.

if I type "head" it should suggest "headquarters", a word found in the index. Preferably it should sort the suggestions by occurrence.

I have tried several tutorials, but they all return the entire content item instead of the word itself. The closest I came up with using the documentation is the following:

GET test_content/_search?pretty
{
  "suggest": { 
      "text": "head", 
      "my-suggestion": { 
         "term": { 
            "field":"autocomplete"
         }
      }
   }
}

But it gives no results, until I type: "headquarte".

When I type "lead" I expect "leadership", but get as result: "land". Typo correction can be nice, but not what I am looking for at the moment. I first want to get a stable working word suggestion functionality and might include typo correction later.

I have also tried playing around with the edgeNGram filter, but didn't get any proper results. The whitespace filter should be fine for now. Prefix search is fine by me.

Configuration:

PUT test_content
{
  "settings": {
      "analysis": {
         "analyzer": {
            "whitespace_analyzer": {
               "type": "custom",
               "tokenizer": "whitespace",
               "filter": [
                  "lowercase",
                  "asciifolding"
               ]
            }
         }
      }
   },
  "mappings": {
    "mydocument": {
      "properties": {
        "autocomplete": {
          "type": "text",
          "fields": {
            "raw": {
              "type": "keyword"
            },
            "completion": {
              "type": "text",
              "analyzer": "whitespace_analyzer",
              "search_analyzer": "standard"
            }
          }
        },
        "title": {
          "type": "text"
        },
        "content": {
          "type": "text"
        }
      }
    }
  }
}

Content:

POST test_content/mydocument
{ "title": "CubeSats to Space", "content": "With the VCLS effort, NASA has successfully advanced the commercial launch service choices for smaller payloads, providing viable dedicated small launch options as an alternative to the rideshare approach, said Jim Norman, director of Launch Services at NASA Headquarters in Washington. This first mission is opening the door for future launch options.", "autocomplete": "CubeSats to Space With the VCLS effort, NASA has successfully advanced the commercial launch service choices for smaller payloads, providing viable dedicated small launch options as an alternative to the rideshare approach, said Jim Norman, director of Launch Services at NASA Headquarters in Washington. This first mission is opening the door for future launch options." }

POST test_content/mydocument
{ "title": "Passing of Rona Ramon", "content": "NASA is deeply saddened by the passing of Rona Ramon, and we send our heartfelt condolences to her family and the people of Israel. Rona’s courage and inspiration in the face of tragedy have helped inspire a new generation to build on the legacy of her husband, space shuttle astronaut Ilan Ramon.", "autocomplete": "Passing of Rona Ramon NASA is deeply saddened by the passing of Rona Ramon, and we send our heartfelt condolences to her family and the people of Israel. Rona’s courage and inspiration in the face of tragedy have helped inspire a new generation to build on the legacy of her husband, space shuttle astronaut Ilan Ramon."}

POST test_content/mydocument
{ "title": "New Moon to Mars Exploration Approach in 2018", "content": "This year, we landed on Mars for the seventh time, and America remains the only country to have landed on Mars successfully. We created new U.S. commercial partnerships to land back on the Moon. We made breakthroughs in our quest to send humans farther into space than ever before. And, we contributed to remarkable advancements in aviation. I want to thank the entire NASA team for a fantastic year of American leadership in space, and I am confident we will build on our 2018 successes in 2019.", "autocomplete": "New Moon to Mars Exploration Approach in 2018 This year, we landed on Mars for the seventh time, and America remains the only country to have landed on Mars successfully. We created new U.S. commercial partnerships to land back on the Moon. We made breakthroughs in our quest to send humans farther into space than ever before. And, we contributed to remarkable advancements in aviation. I want to thank the entire NASA team for a fantastic year of American leadership in space, and I am confident we will build on our 2018 successes in 2019." }

Solution

  • I ended up by using aggregations to solve this issue:

    PUT test_content
    {
      "mappings": {
        "mydocument": {
          "properties": {
            "autocomplete": {
              "type": "keyword"
            }
            ,
            "title": {
              "type": "text",
              "analyzer": "standard"
            },
            "content": {
              "type": "text"
            }
          }
        }
      }
    }
    
    POST test_content/mydocument
    { "title": "Passing of Rona Ramon", "content": "NASA is deeply saddened by the passing of Rona Ramon, and we send our heartfelt condolences to her family and the people of Israel. Rona’s courage and inspiration in the face of tragedy have helped inspire a new generation to build on the legacy of her husband, space shuttle astronaut Ilan Ramon.", "autocomplete": ["Passing","of","Rona","Ramon","NASA","is","deeply","saddened","by","the","passing","of","Rona","Ramon,","and","we","send","our","heartfelt","condolences","to","her","family","and","the","people","of","Israel.","Rona’s","courage","and","inspiration","in","the","face","of","tragedy","have","helped","inspire","a","new","generation","to","build","on","the","legacy","of","her","husband,","space","shuttle","astronaut","Ilan","Ramon."]}
    
    POST test_content/mydocument
    { "title": "New Moon to Mars Exploration Approach in 2018", "content": "This year, we landed on Mars for the seventh time, and America remains the only country to have landed on Mars successfully. We created new U.S. commercial partnerships to land back on the Moon. We made breakthroughs in our quest to send humans farther into space than ever before. And, we contributed to remarkable advancements in aviation. I want to thank the entire NASA team for a fantastic year of American leadership in space, and I am confident we will build on our 2018 successes in 2019.", "autocomplete": [ "New","Moon","to","Mars","Exploration","Approach","in","2018","This","year,","we","landed","on","Mars","for","the","seventh","time,","and","America","remains","the","only","country","to","have","landed","on","Mars","successfully.","We","created","new","U.S.","commercial","partnerships","to","land","back","on","the","Moon.","We","made","breakthroughs","in","our","quest","to","send","humans","farther","into","space","than","ever","before.","And,","we","contributed","to","remarkable","advancements","in","aviation.","I","want","to","thank","the","entire","NASA","team","for","a","fantastic","year","of","American","leadership","in","space,","and","I","am","confident","we","will","build","on","our","2018","successes","in","2019." ] }
    
    GET test_content/_search?pretty
    {
      "size": 0,
        "aggregations": {
          "autocomplete": {
             "filter": {
                "prefix": {
                   "autocomplete": "hum"
                }
             },
             "aggregations": {
                "autocomplete": {
                   "terms": {
                      "field": "autocomplete",
                      "include": "hum.*"
                   }
                }
             }
          }
        }
    }