Show all facet values when filtering is applied? - azure-cognitive-search

Is there a way to show/get all facets even if filtering on a specific facet value?
Example:
Facets "facet" with values "A","B" and "C".
User selects A =>
filter: facet eq 'A'
This gives and result with only facet "A" back (and its count).
But if wer then still want to show all facets (A,B and C) to the user, so the user can add other facetvalues. Say user to select also B.
(And we have "OR"-operation for the filtering for A and B)

You need two queries to support your requirement.
Imagine your result set has items with three types of color. The color can be red, green and blue. Facets are always generated based on your result set. If you filter by blue, your result set only contains items where the color is blue.
To support your requirement, you need to issue a separate query that does not filter by the selected facet. Your two queries should be something like:
Query for all items: search=*&facet=Color
Query for blue items: search=*&$filter=Color eq 'blue'
As for your second question about OR for filters, this is supported. It's up to your application to generate the appropriate filters.
search=*&$filter=Color eq 'blue' or Color eq 'red'
Or use the search.in syntax if you have many color values. See https://learn.microsoft.com/en-us/azure/search/search-query-odata-search-in-function.
search=*&$filter=search.in(Color, 'blue,red', ',')

Related

Solr - How to highlight specific terms in specific fields

How can i highlight a specif term in a specif field?
For example, imagine the following query:
foo TITLE("bar")
, what i want to achieve is the highlight of foo in all fields and bar only in the field TITLE.
Until now the following has not worked:
q=<TITLE_field_internal_name>:"bar"hl.fl=&&hl.requireFieldMatch=true
Note: In the above example TITLE is re-mapped correctly to a solr field.
Most highlighting parameters supports the per-field parameter syntax:
f.TITLE.hl.<parameter>
Seeing as your syntax isn't valid Solr syntax and there is no way it'll know that TITLE("bar") refers to a field named TITLE, you'll have to extract that (or provide) that metadata yourself.
If you're querying different fields and only want to highlight the terms hit in those fields (i.e. if your query had been title:bar to only search for bar in the field title), you don't have to use per field settings, but can set hl.requireFieldMatch to true instead.
By default, false, all query terms will be highlighted for each field to be highlighted (hl.fl) no matter what fields the parsed query refer to. If set to true, only query terms aligning with the field being highlighted will in turn be highlighted.
Note: if the query references fields different from the field being highlighted and they have different text analysis, the query may not highlight query terms it should have and vice versa. The analysis used is that of the field being highlighted (hl.fl), not the query fields.
Finally, after much trial and error, i managed to have this thing working Here are a few snippets:
fq=(_query_:"{!edismax+qf%3D'container_title_en'+v%3D'hormones'}"+OR+_query_:"{!edismax+qf%3D$fqf+v%3D'cancer'}")
fqf=authors_tnss+etc+etc+...
hl.q=(_query_:"{!edismax+qf%3D'container_title_en'+v%3D'hormones'}"+OR+_query_:"{!edismax+qf%3D$fqf+v%3D'cancer'}")
hl.fl=id,external_id_s,etc,etc,...
hl.requireFieldMatch=true
, notice that if hl.fl fields are separated by , and in my custom fqf field in fq they are separated by +.

Using solr facets as filters

When querying solr for products I also return the facets. For fields such as category, size, colour, price.
So I do something along the lines of:
solr.search(*:*, **{'start': 0,
'rows': 50,
'defType': 'edismax',
'fq': '(category=Shoes)',
'facet': 'true',
'facet.limit': -1,
'facet.field': ['size', 'colour', 'price'],
'facet.mincount': 0})
If I query for category "Shoes" I see the possible size, colours and prices which match that category. Now if I add to the filter (colour:Red) then all the other possible colours will disappear which is logical as its now filtered on the colour but the user might want to choose two colours.
What is the a better/the usual way to achieve this?
You can implement this by adding tags to your fq's and then excluding those tags when creating the facets. An example is show in Faceting - LocalParameters for Faceting:
To return counts for doctype values that are currently not selected, tag filters that directly constrain doctype, and exclude those filters when faceting on doctype.
q=mainquery&fq=status:public&fq={!tag=dt}doctype:pdf&facet=true&facet.field={!ex=dt}doctype
Filter exclusion is supported for all types of facets. Both the tag and ex local parameters may specify multiple values by separating them with commas.
The implementation would then just OR the different selected values for each filter together in the fq for that filter (so you get color:red OR color:blue or color:(red OR blue).
Yonik also has an example on his blog that's very close to your use-case, but it uses the same method as shown in the reference manual above.

Partial results in Solr

Let's assume a query with the following words in Solr: yellow, blue, green.
I would like to return documents matching in this order:
all documents that contain: yellow AND blue AND green
then documents that contain (yellow AND blue) OR (yellow AND green) OR (blue AND green)
then documents that contain yellow OR blue OR green
In short: documents matching all 3 terms, then matching 2 terms, then single matches.
How can I structure the query for this?
This should be the default behavior when searching against a single field. The more optional SHOULD clauses are matched - the higher the score.
However, if you are using Dismax or eDismax and terms match against different fields, you will get competing scores and only the highest will get picked. This could cause some terms in one field shadow the presence of additional terms in a different field and give you possible different order.
One strategy to try in those situations is the tie (Tie Breaker) parameter, which will take into account scores from those non-maximum-score field matches. Try setting it to 1 for the extreme effect and see if the results are more like what you expected.

Solr Facets - Newbie - Can I have the JSON response for facets to include both key and value?

I am experimenting with Solr Facets. I imported my DB
pseudo:
item colorName colorID
ball red 1
plate red 1
table blue 2
Now, when I display the facet on the site, I instinctually want to have both colorName and colorID in my json, however the Solr Fusion interface and all the documents I have read the last several hours tell me I can only do either {Colorname, count} or {colorID, count} where I actually want { colorID,ColorName, count}. I dont have a real reason actually but coming from the old times, I couldn't feel comfortable with just a name or id and not both...
Facets are meant to be simple key => value pairs, with the key being your facet.field and the value being the number of documents that fall under that facet. The idea is to be able to give users a broad overview of various categories, along with the number of items available in that category. From there, you can use filter queries to drill down further into the data.
Your best bet in your example is to facet on multiple fields. For example:
/select?q=field_name%3APIN&rows=0&wt=json&indent=true&facet=true&facet.field=unique&facet.field=manufacturer_id
And then filter on whatever combination you need.

Offer facets with zero count for certain category with min count set to one using Solr

I have integrated Solr 4 for e-commerce application. And offers a facet filters like flipkart on category pages. Filters works fine, however, the facets has min count set to 1, so facets with zero count are not returned by Solr.
Now, I want to display those with zero count like displayed here in this image.
However, just like in this image, Screen Size can be present in other category as well, so in this case instead of displaying only those options presents for this category it displays all options which are no applicable for the this current category.
So, it lists all the facets as zero even if they have nothing to do with this category. My problem is I want to display only those facets which are available if there no filters applied and then display them greyed out when they are no longer applicable.
Any clue how to do this?
One thing you can try is getting the facets with and without the filter within the same query.
Check Multi-Select_Faceting_and_LocalParams
Return the Same facet with and without exclusion
If No filters they would be same.
If filter applied, the Normal facet will have filtered Facets and exclusion facet will have facets without the filter. fq={!tag=dt}doctype:pdf&facet=on&facet.field={!ex=dt}doctype&facet.field=doctype
Compare at Client side for the differences and display accordingly.

Resources