我正在使用ES版本7.0。我有一个商店的索引,其中以UTC时间表示可用性(开门和关门时间)。我将时间存储在Integer中,以便可以轻松地将其与当前时间匹配。
下面是一个示例文档:
{
"availability" : {
"thu" : [
{
"start" : 0,
"end" : 400
},
{
"start" : 1300,
"end" : 2400
}
],
"tue" : [
{
"start" : 1300,
"end" : 2400
},
{
"start" : 0,
"end" : 400
}
],
"wed" : [
{
"start" : 0,
"end" : 400
},
{
"start" : 1300,
"end" : 2400
}
],
"sat" : [
{
"start" : 1400,
"end" : 2400
},
{
"start" : 0,
"end" : 500
}
],
"fri" : [
{
"start" : 0,
"end" : 400
},
{
"start" : 1300,
"end" : 2400
}
],
"mon" : [
{
"start" : 0,
"end" : 200
},
{
"start" : 1300,
"end" : 2400
}
],
"sun" : [
{
"start" : 1400,
"end" : 2400
},
{
"start" : 0,
"end" : 200
}
]
},
.
.
.
.
}
下面是带有简单脚本的查询:
GET stores/_search
{
"query": {
"bool": {
"filter" : {
"script" : {
"script" : {
"source": "String d = params.day, start_key = 'availability.'+d+'.start', end_key = 'availability.'+d+'.end'; long t = params.time; if(doc[start_key].size() != 0 && doc[end_key].size() != 0){ long s = doc[start_key].value; long e = doc[end_key].value; return (s <= t && e > t); }",
"lang": "painless",
"params" : {
"day" : "wed",
"time" : 300
}
}
}
}
}
}
}
上面的查询在星期三的时间300起作用,并在结果中给出上述文档,但在星期三的时间1400不起作用。看起来脚本始终匹配可用性数组中的第一个值。
我也尝试遍历可用性的这一天,但这并没有给我带来任何字段发现错误。
GET stores/_search
{
"query": {
"bool": {
"filter" : {
"script" : {
"script" : {
"source": "String d = params.day, start_key = 'availability.'+d+'.start', end_key = 'availability.'+d+'.end'; long t = params.time; if(doc[start_key].size() != 0 && doc[start_key].size() != 0){ for(item in doc['availability.'+d]){ long s = item['start'].value; long e = item['end'].value; if (t >= s && t < e){ return true; } }}",
"lang": "painless",
"params" : {
"day" : "wed",
"time" : 300
}
}
}
}
}
}
}
上面的查询返回以下错误
{ ....
"reason": {
"type": "script_exception",
"reason": "runtime error",
"script_stack": [
"org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:94)",
"org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:41)",
"for(item in doc['availability.'+d]){ long ",
" ^---- HERE"
],
"script": "String d = params.day, start_key = 'availability.'+d+'.start', end_key = 'availability.'+d+'.end'; long t = params.time; if(doc[start_key].size() != 0 && doc[start_key].size() != 0){ for(item in doc['availability.'+d]){ long s = item['start'].value; long e = item['end'].value; if (t >= s && t < e){ return true; } }}",
"lang": "painless",
"caused_by": {
"type": "illegal_argument_exception",
"reason": "No field found for [availability.wed] in mapping with types []"
}
}
..... }
使用
doc['availability']['wed']
时也会出错我在这里想念的东西吗?
最佳答案
如果Availability.wed是object类型,请在下面使用
{
"query": {
"script": {
"script": {
"source": "String d = params.day; for(int i=0; i<doc['availability.'+params.day+'.start'].length;i++){ long start =doc['availability.'+params.day+'.start'][i]; long end = doc['availability.'+params.day+'.end'][i]; if(start <= params.time && end > params.time){ return true;}} ",
"lang": "painless",
"params": {
"day": "wed",
"time": 2300
}
}
}
}
}
如果available.wed是嵌套类型,请在下面使用
对应:
PUT testindex10/_mappings
{
"properties": {
"name":{
"type": "text"
},
"availability": {
"type": "object",
"properties": {
"mon": {
"type": "nested",
"properties": {
"start": {
"type": "integer"
}
}
},
"tue": {
"type": "nested",
"properties": {
"start": {
"type": "integer"
}
}
},
"wed": {
"type": "nested",
"properties": {
"start": {
"type": "integer"
}
}
},
"thu": {
"type": "nested",
"properties": {
"start": {
"type": "integer"
}
}
},
"fri": {
"type": "nested",
"properties": {
"start": {
"type": "integer"
}
}
},
"sat": {
"type": "nested",
"properties": {
"start": {
"type": "integer"
}
}
}
}
}
}
}
数据:
[
{
"_index" : "testindex10",
"_type" : "_doc",
"_id" : "snyiPm0ButCCF6l_WyTl",
"_score" : 1.0,
"_source" : {
"name" : "store1",
"availability" : {
"mon" : [
{
"start" : 0,
"end" : 400
},
{
"start" : 1300,
"end" : 2400
}
],
"tue" : [
{
"start" : 1300,
"end" : 2400
},
{
"start" : 0,
"end" : 400
}
],
"wed" : [
{
"start" : 0,
"end" : 200
},
{
"start" : 1300,
"end" : 2400
}
],
"thu" : [
{
"start" : 1400,
"end" : 2400
},
{
"start" : 0,
"end" : 500
}
],
"fri" : [
{
"start" : 0,
"end" : 400
},
{
"start" : 1300,
"end" : 2400
}
]
}
}
},
{
"_index" : "testindex10",
"_type" : "_doc",
"_id" : "s3yiPm0ButCCF6l_liQq",
"_score" : 1.0,
"_source" : {
"name" : "store2",
"availability" : {
"mon" : [
{
"start" : 0,
"end" : 400
},
{
"start" : 1300,
"end" : 2400
}
],
"tue" : [
{
"start" : 1300,
"end" : 2400
},
{
"start" : 0,
"end" : 400
}
],
"wed" : [
{
"start" : 0,
"end" : 500
},
{
"start" : 1300,
"end" : 2400
}
],
"thu" : [
{
"start" : 1400,
"end" : 2400
},
{
"start" : 0,
"end" : 500
}
],
"fri" : [
{
"start" : 0,
"end" : 400
},
{
"start" : 1300,
"end" : 2400
}
]
}
}
}
]
询问
GET testindex10/_search
{
"query": {
"bool": {
"filter": {
"nested": {
"path": "availability.wed",
"query": {
"script": {
"script": {
"source": "String d = params.day; long start =doc['availability.'+params.day+'.start'].value; long end = doc['availability.'+params.day+'.end'].value; if(start <= params.time && end > params.time){ return true;} ",
"lang": "painless",
"params": {
"day": "wed",
"time": 400
}
}
}
}
}
}
}
}
}
结果:
[
{
"_index" : "testindex10",
"_type" : "_doc",
"_id" : "s3yiPm0ButCCF6l_liQq",
"_score" : 0.0,
"_source" : {
"name" : "store2",
"availability" : {
"mon" : [
{
"start" : 0,
"end" : 400
},
{
"start" : 1300,
"end" : 2400
}
],
"tue" : [
{
"start" : 1300,
"end" : 2400
},
{
"start" : 0,
"end" : 400
}
],
"wed" : [
{
"start" : 0,
"end" : 500
},
{
"start" : 1300,
"end" : 2400
}
],
"thu" : [
{
"start" : 1400,
"end" : 2400
},
{
"start" : 0,
"end" : 500
}
],
"fri" : [
{
"start" : 0,
"end" : 400
},
{
"start" : 1300,
"end" : 2400
}
]
}
}
}
]
没有脚本(更好的性能)的另一种解决方法是
{
"query": {
"bool": {
"filter": {
"nested": {
"path": "availability.wed",
"query": {
"bool": {
"must": [
{
"range": {
"availability.wed.start": {
"lte": 400
}
}
},
{
"range": {
"availability.wed.end": {
"gte": 400
}
}
}
]
}
}
}
}
}
}
}
关于elasticsearch - 基于可用性的Elasticsearch无痛脚本搜索文档,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57968917/