如何在elasticsearch中进行精确匹配?

Mur*_*raj 2 indexing mongodb node.js exact-match elasticsearch

在这里我给出了我更新的映射

curl -X PUT localhost:9200/testing/listings/_mapping -d '{
  "listings" : {
    "properties" : {
        "address" : {
           "properties": {
              "location": { "type" : "string",
                            "index" : "not_analyzed"
               }
            }
        },
        "suggest" : { "type" : "completion",
                      "index_analyzer" : "simple",
                      "search_analyzer" : "simple",
                      "payloads" : true
        }
      }
   }
}'
Run Code Online (Sandbox Code Playgroud)

我的映射创建索引如下

{
  "testing": {
    "mappings": {
      "listings": {
        "properties": {
          "address": {
            "properties": {
              "city": {
                "type": "string"
              },
              "line1": {
                "type": "string"
              },
              "line2": {
                "type": "string"
              },
              "line3": {
                "type": "string"
              },
              "location": {
                "type": "string",
                "index": "not_analyzed"
              },
              "pincode": {
                "type": "string"
              }
            }
          },
          "title": {
            "type": "string"
          }
        }
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

但我的数据仍然不匹配。

我的样本数据是

{
  "listings": {
    "title": "testing 3",
    "address": {
      "line1": "3rd cross",
      "line2": "6th main",
      "line3": "",
      "landmark": "",
      "location": "k r puram",
      "pincode": "",
      "city": "Bangalore"
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

当我给出查询时,k r puram我得到了匹配的结果。

但是当我给出查询 asr r puramr k puram时,我也得到了属于 的结果k r puram

在上面的查询中,我仅列出了k r puram其他人的列表,我没有列表,因此除了k r puram它应该给出空结果之外。

这是我的查询:

{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "published": true
          }
        },
        {
          "match": {
            "inActive": false
          }
        },
        {
          "range": {
            "propertyDetailsCategory.build_up_area": {
              "lte": 200
            }
          }
        },
        {
          "match": {
            "type": "commercial"
          }
        },
        {
          "match": {
            "purpose": "rent"
          }
        },
        {
          "range": {
            "commercialsCategory.exp_rent": {
              "lte": 50000
            }
          }
        },
        {
          "match": {
            "address.location": "k r puram"
          }
        }
      ]
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

Oll*_*ank 5

如果数据恰好是“kr puram”并且您正在搜索恰好“kr puram” - 那么您不应该使用分析器。

插入数据时,Elasticsearch 中的默认行为是使用标准分析器。

禁用此用途

 "index": "not_analyzed" 
Run Code Online (Sandbox Code Playgroud)

在适当字段的映射中。


如果您的映射如下:

curl -XPOST http://localhost:9200/index/address/_mapping -d '
{"address": {
  "properties": { 
    "city": {"type": "string"}, 
    "line1": {"type": "string"}, 
    "line2": {"type": "string"}, 
    "line3": {"type": "string"}, 
    "location": { "type": "string", "index": "not_analyzed"}, 
    "pincode": {"type": "string"} 
 }}}'
Run Code Online (Sandbox Code Playgroud)

那么你的数据必须匹配它,例如这不匹配它:

curl -XPOST http://localhost:9200/index/address/ -d '
{"title":"testing",
 "address":
      {"line1":"#51",
       "line2":"3rd cross",
       "line3":"6th main",
       "location":"k r puram",
       "pincode":"560041"}}
Run Code Online (Sandbox Code Playgroud)

然而,这确实匹配(我的修改):

curl -XPOST http://localhost:9200/index/address/ -d '
{"line1":"#51",
 "line2":"3rd cross",
 "line3":"6th main",
 "location":"k r puram",
 "pincode":"560041"}'
Run Code Online (Sandbox Code Playgroud)

此查询按预期找到文档:

curl -XGET http://localhost:9200/index/address/_search -d '
{
   "query" :{"match" : {"location": "k r puram"}}
}'
Run Code Online (Sandbox Code Playgroud)

如果您无法更改数据,请向映射添加额外的级别,例如:

curl -XPOST http://localhost:9200/index/address3/_mapping -d '{
  "address3" : {
    "properties" : {
      "address" : {
        "properties" : {
          "city" : {
            "type" : "string"
          },
          "line1" : {
            "type" : "string"
          },
          "line2" : {
            "type" : "string"
          },
          "location" : {
            "type" : "string", "index": "not_analyzed"
          }
        }
      },
      "title" : {
        "type" : "string"
     }
   }
 }
}'
Run Code Online (Sandbox Code Playgroud)

查询再次运行良好:

curl -XGET http://localhost:9200/index/address3/_search -d '
{
   "query" :{"match" : {"address.location": "k r puram"}}
}'
Run Code Online (Sandbox Code Playgroud)