【问题标题】:Elasticsearch Painless script search document based on availabilityElasticsearch 基于可用性的无痛脚本搜索文档
【发布时间】:2019-09-17 07:00:25
【问题描述】:

我使用的是 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']时也出现错误

我在这里遗漏了什么?

【问题讨论】:

  • 你能分享你的映射吗?

标签: elasticsearch elasticsearch-painless


【解决方案1】:

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

【讨论】:

  • availability.wed 是我的问题。它是嵌套类型而不是我的索引中的对象。非常感谢,也感谢更好的性能查询。
猜你喜欢
  • 1970-01-01
  • 2022-06-19
  • 1970-01-01
  • 2019-03-12
  • 2017-09-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-20
相关资源
最近更新 更多