首页 文章

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

提问于
浏览
4

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

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

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

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

它会返回正确的记录(上面刚刚制作的产品) .
我知道 tpad 这个词匹配 ipad 所以记录是返回的 .
但从技术上讲,我怎么知道它已匹配 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 回答

  • 1

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

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

    Determining which words were matched in a fuzzy search

  • 3

    我知道这个问题比较老,但我刚刚碰到它 . 我这样做的方法是在构建查询时填充查询名称字段 . 这样它就会回到“matchedQuery”字段中作为响应 . 希望这可以帮助 :)

  • 1

    如果您使用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>" ]
          }
        } ]
      }
    }
    

相关问题