在 ElasticSearch 中查询列表时未获得预期输出
Not getting intended output on querying on a list in ElasticSearch
======== 尝试在下面查询列表,但没有成功=============
输入记录:
{
"somerecord": [
{
"fieldValue": "1",
"sampleKey": [
"1",
"2"
]
},
{
"fieldValue": "2",
"sampleKey": [
"3",
"4"
]
}
]
}
“fieldValue”预期或需要的输出:“1”搜索:
{
"fieldValue": "1",
"sampleKey": [
"1",
"2"
]
}
步骤:
创建了一个映射
放个记录
使用不同的查询类型进行查询
参考文献:
http://www.spacevatican.org/2012/6/3/fun-with-elasticsearch-s-children-and-nested-documents/
https://www.elastic.co/guide/en/elasticsearch/guide/current/nested-mapping.html
https://www.elastic.co/guide/en/elasticsearch/reference/current/nested.html
https://gist.github.com/nicolashery/6317643
http://elasticsearch-cheatsheet.jolicode.com/
http://obtao.com/blog/2014/04/elasticsearch-advanced-search-and-nested-objects/
http://joelabrahamsson.com/elasticsearch-nested-mapping-and-filter/
请让我知道如何实现我的意图。
========一些命令运行=========
1.
curl -XPOST https://someClusterURL/tax2 -d '{
"mappings": {
"ids": {
"properties": {
"somerecord": {
"type": "nested",
"properties": {
"fieldValue": {
"type": "string"
}
}
}
}
}
}
}'
curl -XPUT https://someClusterURL/tax2/ids/1 -d '{
"somerecord": [
{
"fieldValue": "1",
"sampleKey": [
"1",
"2"
]
},
{
"fieldValue": "2",
"sampleKey": [
"3",
"4"
]
}
]
}'
3.
curl -XGET https://someClusterURL/tax2/ids/_search -d '{
"query": {
"nested": {
"path": "somerecord",
"query": {
"bool": {
"must": [
{ "match": { "fieldValue": "1" }}
]
}
}
}
}
}'
结果:
{
"somerecord": [
{
"fieldValue": "1",
"sampleKey": [
"1",
"2"
]
},
{
"fieldValue": "2",
"sampleKey": [
"3",
"4"
]
}
]
}
试了很多,还是不行。
问题是您将两个对象都存储在一个嵌套文档中。当您在一个嵌套对象中命中时,将返回 complete 文档。
您是否需要嵌套文档?
使用 Inner_Hits and Source Filtering(禁用源),例如:
{
"_source": false,
"query": {
"nested": {
"path": "somerecord",
"query": {
"bool": {
"must": [
{ "match": { "fieldValue": "1" }}
]
}
},
"inner_hits" :{}
}
}
使用 "_source": false
将不会获取源详细信息。
======== 尝试在下面查询列表,但没有成功=============
输入记录:
{ "somerecord": [ { "fieldValue": "1", "sampleKey": [ "1", "2" ] }, { "fieldValue": "2", "sampleKey": [ "3", "4" ] } ] }
“fieldValue”预期或需要的输出:“1”搜索:
{ "fieldValue": "1", "sampleKey": [ "1", "2" ] }
步骤:
创建了一个映射
放个记录
使用不同的查询类型进行查询
参考文献:
http://www.spacevatican.org/2012/6/3/fun-with-elasticsearch-s-children-and-nested-documents/ https://www.elastic.co/guide/en/elasticsearch/guide/current/nested-mapping.html https://www.elastic.co/guide/en/elasticsearch/reference/current/nested.html https://gist.github.com/nicolashery/6317643 http://elasticsearch-cheatsheet.jolicode.com/ http://obtao.com/blog/2014/04/elasticsearch-advanced-search-and-nested-objects/ http://joelabrahamsson.com/elasticsearch-nested-mapping-and-filter/
请让我知道如何实现我的意图。
========一些命令运行=========
1.
curl -XPOST https://someClusterURL/tax2 -d '{ "mappings": { "ids": { "properties": { "somerecord": { "type": "nested", "properties": { "fieldValue": { "type": "string" } } } } } } }'
curl -XPUT https://someClusterURL/tax2/ids/1 -d '{ "somerecord": [ { "fieldValue": "1", "sampleKey": [ "1", "2" ] }, { "fieldValue": "2", "sampleKey": [ "3", "4" ] } ] }'
3.
curl -XGET https://someClusterURL/tax2/ids/_search -d '{ "query": { "nested": { "path": "somerecord", "query": { "bool": { "must": [ { "match": { "fieldValue": "1" }} ] } } } } }'
结果:
{ "somerecord": [ { "fieldValue": "1", "sampleKey": [ "1", "2" ] }, { "fieldValue": "2", "sampleKey": [ "3", "4" ] } ] }
试了很多,还是不行。
问题是您将两个对象都存储在一个嵌套文档中。当您在一个嵌套对象中命中时,将返回 complete 文档。
您是否需要嵌套文档?
使用 Inner_Hits and Source Filtering(禁用源),例如:
{
"_source": false,
"query": {
"nested": {
"path": "somerecord",
"query": {
"bool": {
"must": [
{ "match": { "fieldValue": "1" }}
]
}
},
"inner_hits" :{}
}
}
使用 "_source": false
将不会获取源详细信息。