在短语建议中建议下一个单词

时间:2018-02-13 13:09:32

标签: elasticsearch

我如何建议短语建议者中的下一个单词? 我尝试了一个来自https://www.elastic.co/guide/en/elasticsearch/reference/current/search-suggesters-phrase.html的简单示例,但它似乎没有建议

如果我输入"红色"我没有得到任何东西,当我希望得到红狗"时,木瓦过滤器确实产生了我期望的二元组。

这是一个例子

PUT test
{
  "settings": {
    "index": {
      "number_of_shards": 1,
      "analysis": {
        "analyzer": {
          "trigram": {
            "type": "custom",
            "tokenizer": "standard",
            "filter": ["standard", "shingle"]
          }
        },
        "filter": {
          "shingle": {
            "type": "shingle",
            "min_shingle_size": 2,
            "max_shingle_size": 3
          }
        }
      }
    }
  },
  "mappings": {
    "test": {
      "properties": {
        "summary": {
          "type": "text",
          "fields": {
            "trigram": {
              "type": "text",
              "analyzer": "trigram"
            }
          }
        }
      }
    }
  }
}
POST test/test?refresh=true
{"summary": "Red dog"}
POST test/test?refresh=true
{"summary": "Blue word"}


GET test/_analyze
{
  "field":"summary.trigram",
  "text":"red dog"
}

POST test/_search
{
  "suggest": {
    "text": "red",
    "simple_phrase": {
      "phrase": {
        "field": "summary.trigram",
        "size": 1,
        "gram_size": 3,
        "direct_generator": [ {
          "field": "summary.trigram",
          "suggest_mode": "always",
          "min_word_length":2
        } ],
        "highlight": {
          "pre_tag": "<em>",
          "post_tag": "</em>"
        }
      }
    }
  }
}

1 个答案:

答案 0 :(得分:1)

为什么短语建议不起作用?

Phrase suggester没有为字符串"red"提出任何建议的原因是因为它拼写正确,而短语建议器实际上是拼写建议者,而不是completion suggester

如果你尝试"reddog"(没有空格),你会得到建议:

  "suggest": {
    "simple_phrase": [
      {
        "text": "reddog",
        "offset": 0,
        "length": 6,
        "options": [
          {
            "text": "red dog",
            "highlighted": "<em>red dog</em>",
            "score": 0.44063255
          }
        ]
      }
    ]
  }

如果您添加此文档:

POST test/test?refresh=true
{"title": "reddish dog"}

并查询输入"reddi"的建议,您还将获得拼写更正:

  "suggest": {
    "simple_phrase": [
      {
        "text": "reddi",
        "offset": 0,
        "length": 5,
        "options": [
          {
            "text": "reddish",
            "highlighted": "<em>reddish</em>",
            "score": 0.3440574
          }
        ]
      }
    ]
  }

如何在短语中建议下一个单词?

您似乎在寻找不同类型的建议 - Completion Suggest

如果您尝试使用此映射:

PUT test
{
  "mappings": {
    "test": {
      "properties": {
        "summary": {
          "type": "text",
          "fields": {
            "suggest": {
              "type" : "completion"
            }
          }
        }
      }
    }
  }
}

这个查询:

POST test/_search
{

  "suggest" : {
    "my-suggestion" : {
      "text" : "red",
      "completion" : {
        "field" : "summary.suggest"
      }
    }
  }
}

你会得到你想要的东西:

{
  // ...
  "suggest": {
    "my-suggestion": [
      {
        "text": "red",
        "offset": 0,
        "length": 3,
        "options": [
          {
            "text": "Red dog",
            "_index": "test",
            "_type": "test",
            "_id": "AWGanwweVn0PQ9k--kkE",
            "_score": 1,
            "_source": {
              "summary": "Red dog"
            }
          }
        ]
      }
    ]
  }
}

希望有所帮助!