如何在ElasticSearch中查询可以是整数值的关键字嵌套字段



这是ES文档中的字段示例。如何查询只能是整数的"prices.value"?

在这种情况下,值为"150.99",可以完全转换为整数。但有时in可以是类似于"value"的文本:"很多",我想排除具有此值的文档。

"prices": [
{
"currency": "RUR",
"id_offer": 605994811,
"id_prcdoc": 42172,
"id_prcknd": 20859,
"info": {},
"min_order": null,
"sell_by": null,
"value": "150.99"}]

该字段索引:

"prices": {
"type": "nested",
"properties": {
"currency": {
"type": "keyword"
},
"id_offer": {
"type": "integer"
},
"id_prcdoc": {
"type": "integer"
},
"id_prcknd": {
"type": "integer"
},
"min_order": {
"type": "keyword"
},
"sell_by": {
"type": "keyword"
},
"value": {
"type": "keyword",
"index": false
}
}
}

有时它的"值"字段可以是"0.00",我可能想将这些值排除在..中

您可以使用无痛脚本检查值是否可以转换为数字。

{
"query": {
"bool": {
"must": [
{
"nested": {
"path": "prices",
"query": {
"script": {
"script": "if(doc['prices.value'].size()==0){return false;}if(doc['prices.value'].value=='0.00'){return false;}try{ Double.parseDouble(doc['prices.value'].value); return true;} catch(Exception e){return false;} "
}
},
"inner_hits": {}
}
}
]
}
}
}

结果将在inner_hit中。由于脚本速度较慢,因此最好在索引时解决它。我们可以创建另一个字段,它只有在价格值为数字时才有值,并且在查询中,该字段可以使用

编辑:

{
"query": {
"bool": {
"must": [
{
"nested": {
"path": "prices",
"query": {
"bool": {
"must": [
{
"regexp": {
"prices.value": "[1-9][0-9]*.*[0-9]*"
}
}
]
}
},
"inner_hits": {}
}
}
]
}
}
}

最新更新