Pavel
Pavel

Reputation: 974

Solr Aggregation / Facet on DynamicField

Table schema:

CREATE TABLE attributes_v1 (
  profile_id bigint,
  attributes map<text, int>,
  solr_query text,
  PRIMARY KEY ((profile_id))
)

Data inside the table looks like this:

profile_id | attributes                                  
------------+---------------------------------------------
          2 | {'a101': 1, 'a11': 1, 'a12322': 1, 'a51': 3}
          3 |      {'a1': 1, 'a10': 1, 'a11': 3, 'a51': 1}
          1 |     {'a1': 1, 'a10': 1, 'a2322': 1, 'a5': 3}

I can't figure out how to accomplish the following (using solr either via CQL or java)

Desired aggregation / facet :

a1 count: 2 a1 sum: 2

a101 count: 1 a101 sum: 1

a11 count: 2 a11 sum: 4

a12322 count: 1 a12322 sum: 1

a2322 count: 1 a2322 sum: 1

a10 count: 2 a10 sum: 2

a51 count: 2 a51 sum: 4

a5 count: 1 a5 sum: 3

Any ideas?

Thank you!

Upvotes: 0

Views: 470

Answers (1)

PJ.
PJ.

Reputation: 1206

I believe you should have prefixed your map keys with the name of the field itself. This is a requirement/limitation as mentioned in the following links:

link1

link2

link3

So, for example, your 'a1' element should have been called 'attributes1' and your 'a12322' should have been called 'attributes12322'.

Then in your Solr schema, define a dynamicField as follows:

<dynamicField name="attributes*" ... />

You can then query the map elements by referring to them directly. e.g.

q=attributes12322:1

Now, for your question about aggregating, since you also need the sum aside from the count, I think you to use need stats instead of facet.

stats=true&stats.field=attributes12322

You can specify multiple stats.field parameters like:

stats=true&stats.field=attributes12322&stats.field=attributes1&stats.field=attribute51

You can then retrieve the sum from the 'sum' attribute and the count from the 'count' attribute of each stats_fields response item

EDIT:

I didn't notice immediately that you were specifically asking for querying Solr via CQL or Java. I'm not sure if 'stats' is supported by CQL Solr queries

Upvotes: 2

Related Questions