SUM and groupBy in solr - solr

How to execute this query:
select SUM(Primary_cause_vaR),
RiskType,market from bil_tos Where skuType='Product' group by RiskType,market;
I've used http://wiki.apache.org/solr/StatsComponent for this:
I see only sum with respective groupBy fields but i want to see RiskType,
market fields also in the result

StatsComponent supports only GROUP BY (stats.facet) on a single field at a time. Using multiple stats.facet parameters has the effect of multiple GROUP BY clauses, if SQL allowed such a thing. What you can do is facet on RiskType, and then for each facet value do another query adding fq=RiskType:<value> and facet on market. It will take a bunch of queries but eventually you'll have the entire result.

Using stats feature particularly stats.facet:
select?q=*:*&fq=fldA:O&rows=0&wt=xml&indent=true&stats=true&stats.field=Amount&stats.facet=fldB

Related

Why I am not able to perform group by operation for multiple groups on a solr collection

I have 4 columns in Solr collection.
date,latitude,longitude,gpsdt.
And I want to perform a group by operation on three columns(data,latitude,longitude) and I have to find min and max value of gpsdt for each group.
Thanks in advance
In Solr you have to do this with Pivot Faceting and Stats. It will result in a query like
...&facet=true&facet.pivot={!stats=piv1}data,latitude,longitude&stats=true&stats.field={!tag=piv1}gpsdt
There is a good example in Solr Reference Guide. See Combining Stats Component With Pivots

(Lucene/SOLR) Can I annotate the results of a query based on grouping by subqueries?

I would like to group the results of any query in terms of "categories".
"Categories" are keyword queries, they cannot be pre-defined at index time, since they evolve and change over time.
More specifically:
I have a set of categories defined by queries q1,q2,...qN.
Given a user query (q), I need to return the top resulting docs (d1,...d10) as usual,
but I need to know if they belong or not to each of the groups q1,...qN.
As I understand it I could use grouping with queries, but this has two drawbacks:
I will change the results, since instead of d1,...d10 I will get top docs for each query
I will loose the original ordering of the results
The only solution I can think of right now is to issue first q to get the results and ordering, then each of q AND q1, q AND q2, etc. to get the grouping, then parse all the results and group outside the query... expensive!
Any ideas how can I get what I need?
You can use the normal way to do the query, and then add pseudo-fields in fl param that matches the clipping against your categories using function queries.
http://solr.pl/en/2011/11/22/solr-4-0-new-fl-parameter-functionalities-first-look/
https://cwiki.apache.org/confluence/display/solr/Function+Queries
Example:
fl=category1:sum(0.0, query($q1))
q1={!dismax}your query 1
fl=category2:sum(0.0, query($q2))
q2={!dismax}your query 2

How filter from results of group.limit

I have a problem with solr.
First I group.main = true and group.limit = 1 then I have results, how could I filter from results match my conditions (example : from results (A) of group.limit = 1, I want to filter from A to get results (B)).
Please help.
There is no inherent support for referencing previous groups when using Result Grouping in Solr, but you can achieve the same feature by re-using the query that generates the first query and appending the new clause.
group.query=fieldA:value&group.query=fieldA:value AND fieldB:value
This will give you two grouped entries in the response, one for where fieldA has a value, and one where fieldA and fieldB has a specific value.
If you're talking about more general filtering after presenting a grouped result, issuing a query with q=field:value is enough.

Difference between q and fq in Solr

Someone please give me a decent explanation of the difference between q and fq in Solr query, covering some points such as -
Do they have the same syntax?
Do they return same results?
When to use which one and why?
Any other differences
Standard solr queries use the "q" parameter in a request. Filter queries use the "fq" parameter.
The primary difference is that filtered queries do not affect relevance scores; the query functions purely as a filter (docset intersection, essentially).
The q parameter takes your query and execute against the index. Then you can use filter queries (can use multiple filter queries) to filter the results.
For example your query can look like this.
q=author:shakespeare
this will match the documents which has 'shakespeare' in the 'author' field. Then you can use filter queries like this.
fq=title:hamlet
fq=type:play
Those will filter the results based on the other fields. You can even filter on the same field.
The query syntax is similar for both q and fq parameters

Solr filter queries and boosting

Is it possible to boost fields that appear in filter queries (fq=) in Solr?
I have a faceted query that has a tagged filter query something like this:
...&q=*:*&fq={!tag:X}brand:(+"4911")+OR+body:(abc)&facet.field={!ex:X}brand&..
(I facet on brand and the facet is set to ignore the filter query tagged X, so I need to use a filter query.)
I would like to make matches on the brand field score higher than matches on body field in the filter query.
The fields brand and body are multivalued.
I've tried adding bf=/bq= arguments, and I can get brand matches to score higher if I change the filter query to be the main 'q=' query, but I don't seem to be able to influence the score of anything in the filter query. I think I maybe going about it in the wrong way..
Thanks.
Solr "fq"'s do not affect score -- see the wiki. So, you should add your queries to "q" that you actually want to boost. If need be, you can always duplicate a query restriction in both "q" and "fq", as "fq" only acts as a restriction on the results set.

Resources