【发布时间】:2021-06-15 18:26:30
【问题描述】:
我的用例类似于以下内容。
我有嵌套的对象数组warehouses 并尝试根据数组的最后一个元素进行过滤。
我得到了一些结果,但不是正确的。不过也想知道它的工作原理。
比方说,
我想根据库存数组的最后一个元素搜索产品。这是产品文档的样子:
{
"productId": 5,
"productName": "Shoes",
"warehouses": [
{
"location": "Location A",
"quantity": 100
},
{
"location": "Location B",
"quantity": 10
},
{
"location": "Location C",
"quantity": 50
}
]
}
它的映射是:
PUT /products
{
"mappings": {
"properties": {
"productId": {
"type": "integer"
},
"productName": {
"type": "text",
"fields": {
"raw": {
"type": "keyword",
"ignore_above": 256
}
}
},
"warehouses": {
"properties": {
"location": {
"type": "text"
},
"quantity": {
"type": "integer"
}
}
}
}
}
}
假设,我索引了以下 7 个文档:
POST products/_bulk
{"index":{"_id":1}}
{"productId":1,"productName":"Bags","warehouses":[{"location":"Location A","quantity":20},{"location":"Location B","quantity":30},{"location":"Location C","quantity":50}]}
{"index":{"_id":2}}
{"productId":2,"productName":"Shirts","warehouses":[{"location":"Location A","quantity":100},{"location":"Location B","quantity":150},{"location":"Location C","quantity":150}]}
{"index":{"_id":3}}
{"productId":3,"productName":"Shoes","warehouses":[{"location":"Location A","quantity":100},{"location":"Location B","quantity":10},{"location":"Location C","quantity":50}]}
{"index":{"_id":4}}
{"productId":4,"productName":"Shirt","warehouses":[{"location":"Location A","quantity":100},{"location":"Location B","quantity":10},{"location":"Location C","quantity":60}, {"location":"Location F","quantity":70}]}
{"index":{"_id":5}}
{"productId":5,"productName":"Socks","warehouses":[{"location":"Location A","quantity":800},{"location":"Location B","quantity":1500},{"location":"Location Z","quantity":1000}]}
{"index":{"_id":6}}
{"productId":6,"productName":"TV","warehouses":[{"location":"Location A","quantity":20},{"location":"Location B","quantity":150},{"location":"Location C","quantity":123}]}
{"index":{"_id":7}}
{"productId":7,"productName":"Table","warehouses":[{"location":"Location A","quantity":20},{"location":"Location B","quantity":200},{"location":"Location C","quantity":140}, {"location":"Location D","quantity":123}]}
现在我想用“数量”搜索和过滤产品:123。 所以根据上面的索引文档,我想过滤并获取 id:6 和 id:7 的产品,因为它的最后一个元素是数量:123。
这是我的无痛(完整)脚本:
GET /products/_search
{
"query": {
"bool": {
"must": {
"match_all": {}
},
"filter": {
"bool": {
"must": {
"script": {
"script": {
"lang": "painless",
"source": """
def x = doc['warehouses.quantity'];
def flag = false;
if(x[x.length - 2 ] == params.limit) {
flag = true;
}
return flag;
""",
"params": {
"limit": 123
}
}
}
}
}
}
}
}
}
所以在上面的脚本中,我得到了id:6 的结果,这是电视产品。
当我用x[x.length - 3 ] 替换x[x.length - 2 ] 时,我可以得到id:7 的结果。
我不确定如何获得包含两个文档 [id:6 (TV) 和 id:7 (Table)] 的结果。
我使用的是 Elasticsearch 版本:7.8.1。
【问题讨论】:
标签: elasticsearch elasticsearch-painless elasticsearch-7