Search code examples
javasolrsolrj

"Group by" on a solr field using solrj


I am using SOLR 2.5.1

I have a field type. I am querying using SOLR Java driver. I am only fetching type field in the query.

Note: query field and fl (type in my case) are different

Say response is something like

{
  "response": {
    "numFound": 200,
    "start": 0,
    "docs": [
      {
        "type": "A"
      },
        {
        "type": "B"
      },
       {
        "type": "A"
      },
        {
        "type": "A"
      },
       {
        "type": "C"
      },
       {
        "type": "D"
      },
       {
        "type": "A"
      },
       {
        "type": "B"
      },
       {
        "type": "C"
      },
       {
        "type": "B"
      }
    ]
  }
}

I need to group output and find the count of occurrence. Output like:

"A":4, "B":3, "C":2, "D":1

I can process it in java code. But is there any way to group and count on a particular field?


Solution

  • This is called faceting and gives you a count for each indexed term into the field. It is important that each term is kept as a single token if you want to generate counts for the complete text (i.e. you want to count New York and not New and York). A string field is usually well suited as a field type to facet on.

    For the SolrJ integration, you use .addFacetField("type") in your query, and then you can use getFacetField("type") on the response object.