短语建议弹性搜索

时间:2016-04-21 09:59:13

标签: elasticsearch search-suggestion

我有一个问题是对错误拼写的Ngrams提出一个好的建议。 让我详细解释一下。

在'title'字段上考虑使用shingle过滤器的以下映射:

PUT _template/test_news_flo
{
"template": "test_articles_flo",
"settings": {
    "number_of_shards": 1,
    "number_of_replicas": 2,
    "analysis": {
        "filter": {
            "filter_shingle":{ 
                "type":"shingle", 
                "max_shingle_size":5, 
                "min_shingle_size":2, 
                "output_unigrams":"true" 
            }

        },
        "analyzer": {
            "analyzer_shingle":{ 
                "tokenizer":"standard", 
                "filter":["standard", "lowercase", "filter_shingle"] 
            } 
        }
    }
},
"mappings": {
    "article": {
        "_all": {  "enabled": false },
        "properties": {
            "title": {
                "norms": {
                    "enabled": false
                },
                "type": "string",
               "fields": {
                    "shingle": {
                        "search_analyzer":"analyzer_shingle", 
                        "index_analyzer":"analyzer_shingle", 
                        "type":"string" 
                      }
                  }
              }
          }
      }
   }
}

将一个包含'carla bruni'的文档添加到索引test_articles_flo

POST /test_articles_flo/article
{
  "title": "Carla Bruni scintillante pour le Sidaction" 
}

然后运行以下建议短语查询:

 POST /test_articles_flo/_search/?size=0
 {
 "suggest": {
  "suggest-phrase-title": {
     "text": "Carla bruno",
     "phrase": {     
        "field": "title.shingle",
        "confidence": 1,
        "size": 5,
        "gram_size": 2,
        "max_errors": 2
       }
    }
  }
}

返回以下结果,这正是我需要的:

      {
        "text": "Carla bruno",
        "offset": 0,
        "length": 11,
        "options": [
           {
              "text": "carla bruni",
              "score": 0.24166171
           }
        ]
     }

现在添加另一篇文章:

POST /test_articles_flo/article
{
    "title": "Le réveil de Massimo Bruno" 
}

然后再次使用建议进行搜索:没有给出建议,因为在索引中找到'bruno'并且elasticsearch认为它是有效的。

你知道我怎么能把建议归还'carla bruni'作为建议?

1 个答案:

答案 0 :(得分:0)

在添加direct_generator时似乎按预期工作:

这是查询

POST /test_articles_flo/_search/?size=0
{
   "suggest": {
      "suggest-phrase-title": {
         "text": "jonnhy dep",
         "phrase": {     
            "field": "title.shingle",
            "confidence": 1,
            "size": 5,
            "gram_size": 2,
            "max_errors": 5,
            "direct_generator" : [ {
              "field" : "title.shingle",
              "suggest_mode" : "always",
              "min_word_length" : 1
            }],
            "collate": {
               "query": {
                  "match_phrase": {
                     "{{field_name}}": "{{suggestion}}"
                  }
               },
               "params": {
                  "field_name": "title.shingle"
               },
               "prune": false
            }
         }
      }
   }
}