Node.js 如何在elasticsearch中进行精确匹配?
这里我给出了我的更新映射Node.js 如何在elasticsearch中进行精确匹配?,node.js,mongodb,indexing,
elasticsearch,exact-match,Node.js,Mongodb,Indexing,
elasticsearch,Exact Match,这里我给出了我的更新映射 curl -X PUT localhost:9200/testing/listings/_mapping -d '{ "listings" : { "properties" : { "address" : { "properties": { "location": { "type" : "string", "index" : "not_a
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"
}
}
}
当我以krpuram
的形式给出查询时,我得到了匹配的结果
但是当我以rrpuram
或rkpuram
的形式给出查询时,我也得到了属于krpuram
的结果
在上面的查询中,我只有krpuram
的列表,其他我没有列表,所以除了krpuram
之外,它应该给出空结果
这是我的疑问:
{
"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"
}
}
]
}
}
}
如果数据完全是“kr puram”,并且您正在搜索完全是“kr 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 -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"}}
}'
你试过这个吗?(使用.raw子字段匹配“未标记化”值上的值)
尝试在旧映射上使用此查询,它应该可以工作:)1。为什么只对单个字段使用multi_匹配?2.“地址.位置”实际上包含什么?3.它是如何分析的?我用了比赛也就那个时候同样的结果来了。address.location包含位置,“address.location”是列表对象中的字段@OllyCruickshank让我重新表述我的问题,“地址.位置”是否包含“kr puram”的确切值?是的,它包含kr purami如果数据正好是“kr puram”,而您正在搜索的正好是“kr puram”,那么听起来您不需要分析仪。是否已将字段设置为“索引”:“未分析”在映射中?如果“索引”设置为“未分析”,则elasticsearch不会基于空间分割文本,即整个文本将被视为单个标记。因此,在搜索过程中,将搜索完整的文本。。另一个简单的解决方案是在索引之前用其他字符替换空格。例如:“krpuram”=>krpuram@Olly我修改过的映射是curl-xput localhost:9200/testing/listings/_-mapping-d'{“listings”:{“properties”:{“address”:{“properties”:{“location”:{“type”:“string”,“index”:“not u analyzed”}},“建议”:{“类型”:“完成”,“索引分析器”:“简单”,“搜索分析器”:“简单”,“有效载荷”:真的}}}}真的有效吗?如果是,你能接受我的回答吗?不,它没有给出任何位置的单一结果@ollycruickshank你能给你的问题添加一些样本数据吗?
curl -XGET http://localhost:9200/index/address3/_search -d '
{
"query" :{"match" : {"address.location": "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.raw": "k r puram"}}
]
}
}
}