在弹性搜索中使用模糊查询时查找实际匹配的单词

时间:2014-11-15 11:16:50

标签: elasticsearch lucene fuzzy-search

我是elasticsearch的新手,正在寻找模糊查询搜索 我已经制作了一个具有像这样的对象/记录值的新索引产品

{
            "_index": "products",
            "_type": "product",
            "_id": "10",
            "_score": 1,
            "_source": {
                "value": [
                    "Ipad",
                    "Apple",
                    "Air",
                    "32 GB"
                ]
            }
        }

现在,当我在elasticsearch中执行模糊查询搜索时,如

{
   query: {
       fuzzy: {
          value: "tpad"
       }
   }
}

它会返回正确的记录(上面刚刚制作的产品) 我知道术语tpadipad匹配,所以记录是返回 但从技术上讲,我怎么知道它已匹配ipad。弹性搜索只返回像这样的完整记录(或记录)

{
"took": 4,
"timed_out": false,
"_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
},
"hits": {
    "total": 1,
    "max_score": 0.61489093,
    "hits": [
        {
            "_index": "products",
            "_type": "product",
            "_id": "10",
            "_score": 0.61489093,
            "_source": {
                "value": [
                    "Ipad",
                    "Apple",
                    "Air",
                    "32 GB"
                ]
            }
        }
    ]
}
}

弹性搜索中是否有任何方法可以让我知道它是否与tpad匹配ipad

3 个答案:

答案 0 :(得分:3)

如果您使用highlighting,Elasticsearch将显示匹配的字词:

curl -XGET http://localhost:9200/products/product/_search?pretty -d '{
  "query" : {
    "fuzzy" : {
        "value" : "tpad"
      }
  },
  "highlight": {
    "fields" : {
        "value" : {}
    }
  }
}'

Elasticsearch将返回匹配的文档,并突出显示片段:

{
  "took" : 31,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 1,
    "max_score" : 0.13424811,
    "hits" : [ {
      "_index" : "products",
      "_type" : "product",
      "_id" : "10",
      "_score" : 0.13424811,
      "_source":{
 "value" : ["Ipad",
                "Apple",
                "Air",
                "32 GB"
                ]
           },
      "highlight" : {
        "value" : [ "<em>Ipad</em>" ]
      }
    } ]
  }
}

答案 1 :(得分:1)

如果您只想分析结果,可以使用Inquisitor插件。

如果您需要以编程方式执行此操作,我认为突出显示功能可以帮助您:

Determining which words were matched in a fuzzy search

答案 2 :(得分:1)

我知道这个问题比较老,但我刚刚碰到它。我这样做的方法是在构建查询时填充查询名称字段。这样它就会回到&#34; matchingQuery&#34;作为回应的领域。希望这会有所帮助:)