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

我根据目的属性值(如租金或销售)访问列表对象。我可以单独访问要出租和出售的对象。我如何访问列表对象以获取租金和销售价值。我使用下面的查询来获取出租和销售列表对象。

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

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

4

1 回答 1

0

有几件事:

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

  2. 使用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 } } }

  3. 并且查询会发生一些变化:使用termswithexecution=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 文档以了解每种查询的语法。希望有帮助

于 2015-03-06T10:26:19.957 回答