Lupuss
Lupuss

Reputation: 649

Get JSON facet_queries from Solr using JS

I am using Solr facets to split queried documents based on their category and their published date. I managed to write a query that returns what I want in JSON format. The result is something like this:

  "facet_counts":{
    "facet_queries":{
      "publ_tmstmp:[NOW/DAY-7DAYS TO NOW/DAY+1DAY]":0, //this week
      "publ_tmstmp:[NOW/DAY-1MONTH TO NOW/DAY+1DAY]":0, //this month
      "publ_tmstmp:[NOW/DAY-1YEAR TO NOW/DAY+1DAY]":30}, //this year
    "facet_fields":{
      "doc_typ_sht_nm":[
        "GUIDE",30,
        "ANNOUNCEMENT",0,
        "HEADLINE",0,
        "IMAGE",0,
        "LATEST_UPDATES",0]},
    "facet_dates":{},
    "facet_ranges":{}}}

I have no problem looping through the categories but I can't figure out how to do the same for the publication dates using Javascript/jQuery. I have tried several ways like:

result.facet_counts["facet_fields"]
result.facet_counts.facet_fields[0]
result.facet_counts.facet_queries["publ_tmstmp:[NOW/DAY-7DAYS TO NOW/DAY+1DAY]"]
var x in result.facet_counts.facet_queries

None of them work. Most of the time I get an "undefined". How can I iterate through my results?

Thanks.

Upvotes: 0

Views: 1212

Answers (1)

Jayendra
Jayendra

Reputation: 52799

Either of it should work :-

For facet_fields -

console.log(data.facet_counts.facet_fields.doc_typ_sht_nm[0]);
console.log(data['facet_counts']['facet_fields']['doc_typ_sht_nm'][0]);

For facet_queries -

$.each(data['facet_counts']['facet_queries'], function(key, element){
    console.log(key+' : '+element);
});

Example JSFiddle

Upvotes: 2

Related Questions