Home Articles

如何在elasticsearch中完全匹配?

Asked
Viewed 30 times
2

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

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
        }
      }
   }
}'

我的映射创建索引如下

{
  "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"
          }
        }
      }
    }
  }
}

但我的数据仍然不匹配 .

我的样本数据是

{
  "listings": {
    "title": "testing 3",
    "address": {
      "line1": "3rd cross",
      "line2": "6th main",
      "line3": "",
      "landmark": "",
      "location": "k r puram",
      "pincode": "",
      "city": "Bangalore"
    }
  }
}

当我将查询作为 k r puram 时,我得到匹配的结果 .

但是当我将查询作为 r 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"
          }
        }
      ]
    }
  }
}

2 Answers

  • 3

    你试过这个吗? (使用 .raw 子字段匹配"not tokenized"值上的值)

    {"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.raw": "k r puram"}}
         ]
       }
     }
    }
    

    尝试在旧映射上使用此查询,它应该工作:)

  • 0

    如果数据完全是“k r puram”并且您正在搜索“k r puram” - 那么您不应该使用分析器 .

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

    要禁用此用途

    "index": "not_analyzed"
    

    在适当字段的映射中 .


    如果您的映射如下:

    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"} 
     }}}'
    

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

    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"}}
    

    但这确实匹配(我的修改):

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

    此查询按预期查找文档:

    curl -XGET http://localhost:9200/index/address/_search -d '
    {
       "query" :{"match" : {"location": "k r puram"}}
    }'
    

    如果您无法更改数据,则将额外级别添加到映射中,例如:

    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"
         }
       }
     }
    }'
    

    再次查询效果很好:

    curl -XGET http://localhost:9200/index/address3/_search -d '
    {
       "query" :{"match" : {"address.location": "k r puram"}}
    }'
    

Related