Elasticsearch Painless 脚本根据可用性搜索文档

Elasticsearch Painless script search document based on availability

我使用的是 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
              }
          }
        }
      }
    }
  }
}

上面的查询returns下面的错误

{ ....
"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是下面使用的对象类型

{
  "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
        }
      }
    }
  }
}

如果availability.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
                    }
                  }
                }
              ]
            }
          }
        }
      }
    }
  }
}