gpt4 book ai didi

elasticsearch - 当有时仅出现其中一个字段时,对ElasticSearch中的两个字段进行过滤

转载 作者:行者123 更新时间:2023-12-03 01:53:29 26 4
gpt4 key购买 nike

假设我们正在寻找价格不超过$ 100的鞋子。
鞋子具有“价格”字段,但也可以具有“discounted_price”字段。

举个例子:

{
"_index": "shoes",
"_type": "shoe",
"_id": "1",
"_score": 1,
"_source": {
"price": 150,
"discounted_price": 90
}
}
{
"_index": "shoes",
"_type": "shoe",
"_id": "2",
"_score": 2,
"_source": {
"price": 100,
"discounted_price": null
}
}

您将如何构造一个最大价格为100美元的查询,在这种情况下将返回两个文档?

最佳答案

您可以尝试如下操作:

POST /shoes/shoe/_search
{
"query": {
"bool": {
"minimum_should_match": 1,
"should": [
{
"bool": {
"filter": [
{
"range": {
"discounted_price": {
"lte": 100
}
}
},
{
"exists": {
"field": "discounted_price"
}
}
]
}
},
{
"bool": {
"filter": [
{
"range": {
"price": {
"lte": 100
}
}
},
{
"missing": {
"field": "discounted_price"
}
}
]
}
}
]
}
}
}

更新:

对于2.0之前的ES版本,您可以改用以下查询:
POST /shoes/shoe/_search
{
"query": {
"filtered": {
"filter": {
"bool": {
"minimum_should_match": 1,
"should": [
{
"bool": {
"must": [
{
"range": {
"discounted_price": {
"lte": 100
}
}
},
{
"exists": {
"field": "discounted_price"
}
}
]
}
},
{
"bool": {
"must": [
{
"range": {
"price": {
"lte": 100
}
}
},
{
"missing": {
"field": "discounted_price"
}
}
]
}
}
]
}
}
}
}
}

关于elasticsearch - 当有时仅出现其中一个字段时,对ElasticSearch中的两个字段进行过滤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38435408/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com