Arrays 在匹配嵌套/内部对象中的字段时连接elasticsearch索引
我试图通过使用术语过滤器查找来加入2个elasticsearch索引。我提到和。这些示例在字段数组(如“followers”):[“1”、“3”]和join上查找,对于类似的数据效果很好 我的要求是在对象数组中加入一个字段。当我扩展上面的示例以包含一个对象数组时,我的查询失败。 以下是样本数据:Arrays 在匹配嵌套/内部对象中的字段时连接elasticsearch索引,arrays,join,elasticsearch,indices,Arrays,Join,elasticsearch,Indices,我试图通过使用术语过滤器查找来加入2个elasticsearch索引。我提到和。这些示例在字段数组(如“followers”):[“1”、“3”]和join上查找,对于类似的数据效果很好 我的要求是在对象数组中加入一个字段。当我扩展上面的示例以包含一个对象数组时,我的查询失败。 以下是样本数据: PUT /users/user/2 { "followers" : [ { "userId":"1", "username":"abc", "location":"xy
PUT /users/user/2 {
"followers" : [
{
"userId":"1",
"username":"abc",
"location":"xyz"
},
{
"userId":"3",
"username":"def",
"location":"xyz"
}
}
]
}
PUT /tweets/tweet/1 {
"user" : "2"
}
PUT /tweets/tweet/2 {
"user" : "1"
}
我现在试图找到由用户2的追随者创建的推文
POST /tweets/_search {
"query" : {
"filtered" : {
"filter" : {
"terms" : {
"user" : {
"index" : "users",
"type" : "user",
"id" : "2",
"path" : "followers.userId"
},
"_cache_key" : "user_2_friends"
}
}
}
}
}
我对上述查询的搜索结果为0。我还尝试了其他两种方法1)在映射期间将followers对象声明为嵌套对象,并在查询中使用“nested”,2)在将路径指定为“followers”后尝试为followers.userId添加匹配查询。没有任何结果
术语筛选查找是否支持对象数组?任何解决我问题的方法都会对我有很大帮助,除非我遗漏了什么。您使用的Elasticsearch版本是什么?我用的是1.3.4 因此,我创建了两个索引并添加了您列出的文档:
curl -XPUT "http://localhost:9200/users"
curl -XPUT "http://localhost:9200/users/user/2 " -d '
{
"followers" : [
{
"userId":"1",
"username":"abc",
"location":"xyz"
},
{
"userId":"3",
"username":"def",
"location":"xyz"
}
]
}'
curl -XPUT "http://localhost:9200/tweets"
curl -XPUT "http://localhost:9200/tweets/tweet/1 " -d'
{
"user" : "2"
}'
curl -XPUT "http://localhost:9200/tweets/tweet/2 " -d'
{
"user" : "1"
}'
然后运行搜索查询:
curl -XPOST "http://localhost:9200/tweets/_search " -d'
{
"query": {
"filtered": {
"filter": {
"terms": {
"user": {
"index": "users",
"type": "user",
"id": "2",
"path": "followers.userId"
},
"_cache_key": "user_2_friends"
}
}
}
}
}'
得到了这个结果:
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 1,
"hits": [
{
"_index": "tweets",
"_type": "tweet",
"_id": "2",
"_score": 1,
"_source": {
"user": "1"
}
}
]
}
}
以下是我使用的代码:
如果有索引,请清除索引
curl -XDELETE "http://example.com:9200/currencylookup/"
curl -XDELETE "http://example.com:9200/currency/"
创建查找表
curl -XPUT http://example.com:9200/currencylookup/type/2 -d '
{ "conv" : [
{ "currency":"usd","username":"abc", "location":"USA" },
{ "currency":"inr", "username":"def", "location":"India" },
{ "currency":"IDR", "username":"def", "location":"Indonesia" }]
}'
让我们放置一些虚拟文档
curl -XPUT "http://example.com:9200/currency/type/USA" -d '{ "amount":"100", "currency":"usd", "location":"USA" }'
curl -XPUT "http://example.com:9200/currency/type/JPY" -d '{ "amount":"50", "currency":"JPY", "location":"JAPAN" }'
curl -XPUT "http://example.com:9200/currency/type/INR" -d '{ "amount":"50", "currency":"inr", "location":"INDIA" }'
curl -XPUT "http://example.com:9200/currency/type/IDR" -d '{ "amount":"30", "currency" : "IDR", "location": "Indonesia" }'
# curl http://example.com:9200/currency/_search?pretty -d '{
"query" : {
"filtered" : {
"filter" : {
"terms" : {
"currency" : {
"index" : "currencylookup",
"type" : "type",
"id" : "2",
"path" : "conv.currency"
},
"_cache_key" : "currencyexchange"
}
}
}
}
}'
{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"hits" : {
"total" : 2,
"max_score" : 1.0,
"hits" : [ {
"_index" : "currency",
"_type" : "type",
"_id" : "INR",
"_score" : 1.0,
"_source":{ "amount":"50", "currency":"inr", "location":"INDIA" }
}, {
"_index" : "currency",
"_type" : "type",
"_id" : "USA",
"_score" : 1.0,
"_source":{ "amount":"100", "currency":"usd", "location":"USA" }
} ]
}
}
检查输出的时间
curl http://example.com:9200/currency/_search?pretty -d '{
"query" : {
"filtered" : {
"filter" : {
"terms" : {
"currency" : {
"index" : "currencylookup",
"type" : "type",
"id" : "2",
"path" : "conv.currency"
},
"_cache_key" : "currencyexchange"
}
}
}
}
}'
结果
curl -XPUT "http://example.com:9200/currency/type/USA" -d '{ "amount":"100", "currency":"usd", "location":"USA" }'
curl -XPUT "http://example.com:9200/currency/type/JPY" -d '{ "amount":"50", "currency":"JPY", "location":"JAPAN" }'
curl -XPUT "http://example.com:9200/currency/type/INR" -d '{ "amount":"50", "currency":"inr", "location":"INDIA" }'
curl -XPUT "http://example.com:9200/currency/type/IDR" -d '{ "amount":"30", "currency" : "IDR", "location": "Indonesia" }'
# curl http://example.com:9200/currency/_search?pretty -d '{
"query" : {
"filtered" : {
"filter" : {
"terms" : {
"currency" : {
"index" : "currencylookup",
"type" : "type",
"id" : "2",
"path" : "conv.currency"
},
"_cache_key" : "currencyexchange"
}
}
}
}
}'
{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"hits" : {
"total" : 2,
"max_score" : 1.0,
"hits" : [ {
"_index" : "currency",
"_type" : "type",
"_id" : "INR",
"_score" : 1.0,
"_source":{ "amount":"50", "currency":"inr", "location":"INDIA" }
}, {
"_index" : "currency",
"_type" : "type",
"_id" : "USA",
"_score" : 1.0,
"_source":{ "amount":"100", "currency":"usd", "location":"USA" }
} ]
}
}
结论
大写字母是罪魁祸首
您可以看到,'IDR'在大写字母中,因此匹配失败,'JPY'不在查找中,即使它在那里,也不会匹配,因为它在大写字母中
交叉匹配值必须使用小写字母或数字,如
例如:
- abc
- 1abc
curl-XPUT”http://localhost:9200/etest/e/1“-d”{“金额”:“100”,“货币”:“美元”,“地点”:“美国”}“curl-XPUT”http://localhost:9200/etest/e/2“-d'{”amount:“50”,“curl:“JPY”,“location:“JAPAN”}”
我的查询没有结果curl-XPOST”http://localhost:9200/etest/e/_search“-d”{“查询”:{“筛选”:{“筛选”:{“术语”:{“货币”:{“索引”:“ctest”,“类型”:“cur”,“id”:“1”,“路径”:“conv.currency”},“\u cache\u key”:“test”}”“
区分大小写似乎是个问题,它现在对我有效。但是有一个小的更正。看起来匹配工作正常,即使货币文档中有大写,只要查找文档有小写。有一个选项可以解决此问题,请检查