2014-12-19 65 views
2

在这里,我给我更新的映射如何在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 
     } 
     } 
    } 
}' 

我的映射如下

{ 
    "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" 
      } 
     } 
     ] 
    } 
    } 
} 
+0

1.为什么要使用multi_match只针对单场? 2.“address.location”实际包含什么? 3.如何分析? – 2014-12-19 11:34:23

+0

我也用那场比赛也是同样的结果来了。 address.location包含位置,'address.location'是列表对象中的字段'。 @OllyCruickshank – 2014-12-19 11:39:06

+0

让我重新解释一下我的问题 - “address.location”是否包含“kr puram”的确切值? – 2014-12-19 11:52:08

回答

3

如果数据恰好为“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"}} 
}' 
+0

如果“索引”设置为“not_analysed”,那么elasticsearch将不会根据空间拆分文本,即整个文本将被视为单个标记。因此,在搜索过程中将搜索完整的文本。另一个简单的解决方案是在索引之前用空格替换一些其他字符。例如:“kr puram”=> k_r_puram。 – 2014-12-20 03:14:46

+0

@Olly我修改的映射是curl -X PUT localhost:9200/testing/listings/_mapping -d'{ “listing”:{ “properties”:{“address”:{ “properties”:{ “location “:{ “类型”: “串”, “索引”: “not_analyzed” } } }, “建议”:{ “类型”: “完成”, “index_analyzer”: “简单”, “search_analyzer”:“简单”, “有效载荷”:真 } } } }' – 2014-12-23 12:31:25

+0

真棒,它工作吗?如果是这样,你能接受我的回答吗? – 2014-12-23 12:36:57

0

你试过吗? (使用。原子场的“不记号化”值)

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

尝试使用您的旧映射此查询匹配值,它应该工作:)