弹性搜索词建议

时间:2019-01-22 11:13:40

标签: elasticsearch autosuggest search-suggestion

我们希望弹性搜索建议在索引中找到的单词。我们正在使用版本:5.6.4。

如果我键入“ head”,它应该建议“总部”,这是索引中的一个单词。最好按出现的顺序对建议进行排序。

我尝试了一些教程,但是它们都返回了整个内容项,而不是单词本身。我想到的使用文档最接近的是:

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

但是在我键入:“ headquarte”之前,它没有任何结果。

当我键入“ lead”时,我期望“ leadership”,但结果是:“ land”。错字校正可能很好,但不是我目前想要的。我首先要获得稳定的工作单词建议功能,以后可能要包括错字更正。

我也尝试过使用edgeNGram过滤器,但是没有得到任何合适的结果。 whitespace过滤器现在应该可以了。前缀搜索对我来说很好。

配置:

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"
        }
      }
    }
  }
}

内容:

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." }

1 个答案:

答案 0 :(得分:0)

我最终使用聚合来解决此问题:

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.*"
               }
            }
         }
      }
    }
}
相关问题