简体   繁体   中英

Unable to apply date range in stats field in SOLR

I need to create pivot data by applying filter of two fields ie city and date range. Able to filter data by cities and date range but filter is not applying on stats field. Following is the Solr query that i'm using :

select?&fq={!tag=f1}city:(%221000%20OAKS%22)&facet=true&facet.query=true&facet.query={!tag=queryOne}datadate:[2015-01-01%20TO%202015-12-31]&facet.query={!tag=queryTwo}datadate:[2014-01-01%20TO%202014-12-31]&stats=true&stats.field={!tag=a1%20sum=true%20key=charge1}charge&stats.field={!tag=a2%20sum=true%20key=spend1}spend&facet.pivot={!query=queryOne%20key=c1%20stats=a1}city&facet.pivot={!query=queryTwo%20stats=a2%20key=c2}city&facet=on&indent=on&wt=json&rows=0&q= :

Actual Result : 在此处输入图片说明

As you can see, the sum remains same (ie 2348) regardless of what datadate im giving in queryone & querytwo tags. It appears that datadate filters is not having any effect on stats field. Can anyone please explain what is happening here as i'm pretty new to this. Thanks in advance.

The problem is that the StatsComponent & FacetComponent are not in perfect integration with each other. The following Solr Jira comprises your use-case: SOLR-6348 .

But nevertheless there is work-around for your use-case via JSON Facet API . Here is facet parameter which solves your particular use-case:

{
  "date_range": {
    "type": "range",
    "field": "datadate_dt",
    "start": "2014-01-01T00:00:00Z",
    "end": "2016-01-01T00:00:00Z",
    "gap": "%2B1YEAR",
    "facet": {
      "cities_and_spends": {
        "type": "terms",
        "field": "city_s",
        "facet": {"sum_of_spend": "sum(spend_d)"}
      },
      "cities_and_charges": {
        "type": "terms",
        "field": "city_s",
        "facet": {"sum_of_spend": "sum(charge_d)"}
      }
    }
  }
}

Hence the overall GET request would look like:

.../select?indent=on&q=*:*&fq=city:(%221000%20OAKS%22)&rows=0&wt=json&json.facet={%20%22date_range%22:%20{%20%22type%22:%20%22range%22,%20%22field%22:%20%22datadate_dt%22,%20%22start%22:%20%222014-01-01T00:00:00Z%22,%20%22end%22:%20%222016-01-01T00:00:00Z%22,%20%22gap%22:%20%22%2B1YEAR%22,%20%22facet%22:%20{%20%22cities_and_spends%22:%20{%20%22type%22:%20%22terms%22,%20%22field%22:%20%22city_s%22,%20%22facet%22:%20{%22sum_of_spend%22:%20%22sum(spend_d)%22}%20},%20%22cities_and_charges%22:%20{%20%22type%22:%20%22terms%22,%20%22field%22:%20%22city_s%22,%20%22facet%22:%20{%22sum_of_spend%22:%20%22sum(charge_d)%22}%20}%20}%20}%20}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM