Solr Facet搜索 - 根据Max和Group by获取价值

时间:2017-09-13 21:25:47

标签: solr lucene

我试图根据按来源分组的最大时间戳来获取价格。

我现在拥有的Solr查询为每个源提取最大时间戳。如何修改此查询以提取与最大时间戳相关联的相应价格?

http://localhost:6500/solr/listings/select?q=desc_t:"Watch"&indent=on&rows=0&indent=true&wt=json&json.facet=
{
    prices: {
        type: terms,
        field: source_s,
        facet: {
            max_ingestdate: "max(timestamp_dt)"                   
                }

            }
        }

返回:

"facets":{
    "count":141211,
    "prices":{
      "buckets":[{
          "val":"a1",
          "count":71466,
          "max_ingestdate":1.505277283278E12},
        {
          "val":"a2",
          "count":52415,
          "max_ingestdate":1.501872553356E12},
        {
          "val":"a3",
          "count":7866,
          "max_ingestdate":1.504798294686E12},....

我也想要相应的价格,所以最终的结果应该是:

"facets":{
"count":141211,
"prices":{
  "buckets":[{
      "val":"a1",
      "count":71466,
      "max_ingestdate":1.505277283278E12,
      "price": 1334},
    {
      "val":"a2",
      "count":52415,
      "max_ingestdate":1.501872553356E12,
      "price": 1234},
    {
      "val":"a3",
      "count":7866,
      "max_ingestdate":1.504798294686E12
      "price": 1342},...

1 个答案:

答案 0 :(得分:1)

我不确定是否可以在json facet中抓取一个文档,但如果你只是想使用最近的价格,那么你可以使用半黑客。从本质上讲,你可以像源一样在源头上进行分析,然后在源代码中使用一系列短时间窗口中的价格查询方面。

solr调用看起来像:

http://localhost:65000/solr/listings/select?q=description_t:"Watch"&indent=on&rows=0&indent=true&wt=json&json.facet=
    {
        prices: {
            type: terms,
            field: source_s,
            facet: {
                max_ingestdate: "max(timestamp_dt)",
                max_price: "max(price_d)",
                price_1: {
                    type: query,
                    q: "timestamp_dt:[NOW-1DAY TO NOW]",
                    facet: {
                        avg_price_1: "avg(price_d)"
                    }
                },
                price_2: {
                    type: query,
                    q: "timestamp_dt:[NOW-7DAY TO NOW-1DAY]",
                    facet: {
                        avg_price_2: "avg(price_d)"
                    }
                },
                price_3: {
                    type: query,
                    q: "timestamp_dt:[NOW-14DAY TO NOW-7DAY]",
                    facet: {
                        avg_price_3: "avg(price_d)"
                    }
                }
            }

        }
    }