在弹性搜索中保存数组类型数据,并按范围搜索 ID



如何在弹性搜索中添加数组类型的 ID 。 所以对于/documentStoreIndex/notesType/1001

{
“index”: 0,
“time”:1590652925925,
“chracter”:“H”
},
{
“index”: 1,
“time”:1590652925925,
“chracter”:“I”
}

稍后,我想进行搜索

1( "让我有时间获取 ID 1001 的索引 1">

2( "获取从索引 0 到 1 的时间数组,ID 为 1001">

请帮忙。

你需要使用嵌套类型存储数组

映射:

PUT index92
{
"mappings": {
"properties": {
"field1":{
"type": "nested"
}
}
}
}

查询要访问嵌套类型,需要嵌套查询

  1. 查找索引为 1 的文档
{
"query": {
"bool": {
"must": [
{
"term": {
"_id": {
"value": "1000"
}
}
},
{
"nested": {
"inner_hits": {  --> returns matched nested objects
"_source": "field1.time"--> select fields to show.
}, 
"path": "field1",
"query": {
"term": {
"field1.index": {
"value": 1
}
}
}
}
}
]
}
}
}
  1. 从范围 0-1 获取索引

"query": {
"bool": {
"must": [
{
"term": {
"_id": {
"value": "XQxiWnIBonxtIN2b07x2"
}
}
},
{
"nested": {
"inner_hits": {
"_source": "field1.time"
}, 
"path": "field1",
"query": {
"range": {
"field1.index": {
"gte": 0,
"lte": 1
}
}
}
}
}
]
}
}
}

结果:Innert_hits在查询重新调整中指定与嵌套文档匹配

"hits" : [
{
"_index" : "index92",
"_type" : "_doc",
"_id" : "XQxiWnIBonxtIN2b07x2",
"_score" : 2.0,
"_source" : {
"field1" : [
{
"index" : 0,
"time" : 1590652925925,
"chracter" : "H"
},
{
"index" : 1,
"time" : 1590652925925,
"chracter" : "I"
}
]
},
"inner_hits" : {
"field1" : {
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "index92",
"_type" : "_doc",
"_id" : "XQxiWnIBonxtIN2b07x2",
"_nested" : {
"field" : "field1",
"offset" : 0
},
"_score" : 1.0,
"_source" : {
"time" : 1590652925925
}
},
{
"_index" : "index92",
"_type" : "_doc",
"_id" : "XQxiWnIBonxtIN2b07x2",
"_nested" : {
"field" : "field1",
"offset" : 1
},
"_score" : 1.0,
"_source" : {
"time" : 1590652925925
}
}
]
}
}
}
}
]
}

最新更新