首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >尝试在ElasticSearch中查询和聚合,但是不工作- elasticsearch.js客户端

尝试在ElasticSearch中查询和聚合,但是不工作- elasticsearch.js客户端
EN

Stack Overflow用户
提问于 2020-10-15 03:31:59
回答 1查看 292关注 0票数 2

我试图查询我的数据集有两个目的:

  1. 匹配一个术语(可转售=真)
  2. 按最低到最高的价格订购结果

数据集/文档是:

代码语言:javascript
复制
"data" : {
            "resellable" : true,
            "startingPrice" : 0,
            "id" : "4emEe_r_x5DRCc5",
            "buyNowPrice" : 0.006493, //Changes per object
            "sub_title" : "test 1",
            "title" : "test 1",
            "category" : "Education",
      
          }

//THREE OBJECTS WITH THE VALUES OF 0.006, 0.7, 1.05 FOR BUYNOWPRICE

我有三个不同的buyNowPrice对象

有关agg的查询如下:

代码语言:javascript
复制
{
    "query": {
        "bool": {
            "must": [
                {
                    "term": {
                        "data.resellable": true
                    }
                }
            ]
        }
    },
    "from": 0,
    "size": 5,
    "aggs": {
        "lowestPrice": {
            "terms": {
                "field": "data.buyNowPrice",
                "order": {
                    "lowest_price": "desc"
                }
            },
            "aggs": {
                "lowest_price": {
                    "min": {
                        "field": "data.buyNowPrice"
                    }
                },
                "lowest_price_top_hits": {
                    "top_hits": {
                        "size": 5,
                        "sort": [
                            {
                                "data.buyNowPrice": {
                                    "order": "desc"
                                }
                            }
                        ]
                    }
                }
            }
        }
    }
}

查询工作正常,结果是3个具有resellable = true的对象

问题是,agg并没有根据现在的最低买入价格来组织结果。

每个结果,buyNowPrice的顺序是: 1.06,0.006,0.7 -这是不正确的排序。

切换到desc没有任何影响,所以我不相信agg正在运行?

编辑:

现在,使用下面的查询建议如下:

代码语言:javascript
复制
{
    "query": {
        "bool": {
            "must": [
                {
                    "term": {
                        "data.resellable": true
                    }
                }
            ]
        }
    },
    "from": 0,
    "size": 5,
    "aggs": {
        "lowestPrice": {
            "terms": {
                "field": "data.buyNowPrice",
                "order": {
                    "lowest_price": "asc"
                }
            },
            "aggs": {
                "lowest_price": {
                    "min": {
                        "field": "data.buyNowPrice"
                    }
                },
                "lowest_price_top_hits": {
                    "top_hits": {
                        "size": 5
                    }
                }
            }
        }
    }
}

查询的结果是:

代码语言:javascript
复制
  total: { value: 3, relation: 'eq' },
  max_score: 0.2876821,
  hits: [
    {
      _index: 'education',
      _type: 'listing',
      _id: '4emEe_r_x5DRCc5', <--- buyNowPrice of 0.006
      _score: 0.2876821,
      _source: [Object]
    },
    {
      _index: 'education',
      _type: 'listing',
      _id: '4ee_r_x5DRCc5', <--- buyNowPrice of 1.006
      _score: 0.18232156,
      _source: [Object]
    },
    {
      _index: 'education',
      _type: 'listing',
      _id: '4444_r_x5DRCc5', <--- buyNowPrice of 0.7
      _score: 0.18232156,
      _source: [Object]
    }
  ]
}

编辑2:

删除对resellable = true的查询,聚合将正确排序并以正确的顺序返回项。但是,对于可转售的查询,它不包括在内。

我假设这与_score属性覆盖了agg的排序有关吗?如何解决这个问题?

EN

回答 1

Stack Overflow用户

发布于 2020-10-15 03:51:51

您可以使用一个桶排序聚合,它是一个父管道聚合,它对其父多桶聚合的桶进行排序。可以与相应的排序顺序一起指定零或多个排序字段。

添加工作示例(使用与问题中相同的索引数据)、搜索查询和搜索结果

搜索查询:

代码语言:javascript
复制
{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "data.resellable": true
          }
        }
      ]
    }
  },
  "from": 0,
  "size": 5,
  "aggs": {
    "source": {
      "terms": {
        "field": "data.buyNowPrice"
      },
      "aggs": {
        "latest": {
          "top_hits": {
            "_source": {
              "includes": [
                "data.buyNowPrice",
                "data.id"
              ]
            }
          }
        },
        "highest_price": {
          "max": {
            "field": "data.buyNowPrice"
          }
        },
        "bucket_sort_order": {
          "bucket_sort": {
            "sort": {
              "highest_price": {
                "order": "desc"
              }
            }
          }
        }
      }
    }
  }
}

搜索结果:

代码语言:javascript
复制
"buckets": [
        {
          "key": 1.0499999523162842,
          "doc_count": 1,
          "highest_price": {
            "value": 1.0499999523162842
          },
          "latest": {
            "hits": {
              "total": {
                "value": 1,
                "relation": "eq"
              },
              "max_score": 0.08701137,
              "hits": [
                {
                  "_index": "stof_64364468",
                  "_type": "_doc",
                  "_id": "3",
                  "_score": 0.08701137,
                  "_source": {
                    "data": {
                      "id": "4emEe_r_x5DRCc5",
                      "buyNowPrice": 1.05          <-- note this
                    }
                  }
                }
              ]
            }
          }
        },
        {
          "key": 0.699999988079071,
          "doc_count": 1,
          "highest_price": {
            "value": 0.699999988079071
          },
          "latest": {
            "hits": {
              "total": {
                "value": 1,
                "relation": "eq"
              },
              "max_score": 0.08701137,
              "hits": [
                {
                  "_index": "stof_64364468",
                  "_type": "_doc",
                  "_id": "2",
                  "_score": 0.08701137,
                  "_source": {
                    "data": {
                      "id": "4emEe_r_x5DRCc5",
                      "buyNowPrice": 0.7         <-- note this
                    }
                  }
                }
              ]
            }
          }
        },
        {
          "key": 0.006000000052154064,
          "doc_count": 1,
          "highest_price": {
            "value": 0.006000000052154064
          },
          "latest": {
            "hits": {
              "total": {
                "value": 1,
                "relation": "eq"
              },
              "max_score": 0.08701137,
              "hits": [
                {
                  "_index": "stof_64364468",
                  "_type": "_doc",
                  "_id": "1",
                  "_score": 0.08701137,
                  "_source": {
                    "data": {
                      "id": "4emEe_r_x5DRCc5",
                      "buyNowPrice": 0.006         <-- note this
                    }
                  }
                }
              ]
            }
          }
        }
      ]

更新1:

如果将搜索查询修改为:,则为

代码语言:javascript
复制
{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "data.resellable": true
          }
        }
      ]
    }
  },
  "aggs": {
    "lowestPrice": {
      "terms": {
        "field": "data.buyNowPrice",
        "order": {
          "lowest_price": "asc"        <-- change the order here 
        }
      },
      "aggs": {
        "lowest_price": {
          "min": {
            "field": "data.buyNowPrice"
          }
        },
        "lowest_price_top_hits": {
          "top_hits": {
            "size": 5
          }
        }
      }
    }
  }
}

运行上述搜索查询,您将得到所需的结果。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64364468

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档