首页 文章

如何在elasticsearch查询中的特定字段上查找多个精确值?

提问于
浏览
0

我的样本数据如下所示:

{"listings":{"title" : "testing 1", "address" : { "line1" : "3rd cross", "line2" : "6th main", "line3" : "", "landmark" : "", "location" : "k r puram", "pincode" : "", "city" : "Bangalore" },"purpose":"rent","published": true, "inActive": false }, {"listings":{"title" : "testing 2", "address" : { "line1" : "3rd cross", "line2" : "6th main", "line3" : "", "landmark" : "", "location" : "banaswadi", "pincode" : "", "city" : "Bangalore" },"purpose":"sale","published": true, "inActive": false }, {"listings":{"title" : "testing 3", "address" : { "line1" : "3rd cross", "line2" : "6th main", "line3" : "", "landmark" : "", "location" : "tin factory", "pincode" : "", "city" : "Bangalore" },"purpose":"rent","published": true, "inActive": false }

我的索引映射如下所示:

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

我根据租金或销售等目的 property Value 访问列表对象 . 我可以单独访问租赁和销售对象 . 如何访问租赁和销售 Value 的列表对象 . 我使用下面的查询来获取租金和销售列表对象 .

{"query":{
   "filtered": {
     "filter": {
       "terms": {
         "purpose" : ["rent", "sale"]
        }
      }
    },
   "bool":{
     "must":[
       {"match":{"published":true}},
       {"match":{"inActive":false}},
       {"match":{"address.city": "bangalore"}}
      ]
   }
 }
}

请根据需要建议更改 . 提前致谢 .

1 回答

  • 0

    有几件事:

    • address 应声明为 nested object ,以防止将来搜索地址字段时出现错误的搜索结果 . 您可以在这里参考 inner objectnested objecthttp://www.elasticsearch.org/blog/managing-relations-inside-elasticsearch/的问题的更多信息

    • 使用 nested object 时,映射将如下所示(地址类型:嵌套): "listings" : { "properties" : { "address" : { "type" : "nested", "properties": { "location": { "type" : "string", "index" : "not_analyzed" } } }, "suggest" : { "type" : "completion", "index_analyzer" : "simple", "search_analyzer" : "simple", "payloads" : true } } }

    • 并且查询会改变一点:使用 termsexecution=and ,并使用 nested query 作为地址字段:

    "query":{ "filtered": { "filter": { "terms": { "execution" : "and", "purpose" : ["rent", "sale"] } }, "query" : { "bool":{ "must":[ {"term":{"published":true}}, {"term":{"inActive":false}}, { "nested": { "path": "address", "query": {"match":{"address.city": "bangalore"}} } } ] } } } }

    您应该参考elasticsearch文档以获取每种查询的语法 . 希望它有所帮助

相关问题