SOLR sort by IN Query - solr

I was wondering if it is possible to sort by the order that you request documents from SOLR. I am running a In based query and would just like SOLR to return them based on the order that I ask.
In (4,2,3,1) should return me documents ordered 4,2,3,1.
Thanks.

You need Sorting in solr, to order them by field.
I assume that "In based query" means something like: fetch docs whose fieldx has values in (val1,val2). You can a field as multi-valued field and facet on that field. A facet query is a 'is in' search, out of the box (so to say) and it can do more sophisticated searches too.
Edited on OP's query:
Updating a document with a multi-valued field in JSON here. See the line
"my_multivalued_field": [ "aaa", "bbb" ] /* use an array for a multi-valued field */
As for doing a facet query, check this.
You need to do one or more fq statements:
&fq=field1:[400 to 500]
&fq=field2:johnson,thompson
Also do read up on the fact (in link above) that you need to facet on stored rather than indexed fields.

You can easily apply sorting with QueryOptions and field sort (ExtraParams property - I am sorting by savedate field, descending):
var results = _solr.Query(textQuery,
new QueryOptions
{
Highlight = new HighlightingParameters
{
Fields = new[] { "*" },
},
ExtraParams = new Dictionary<string, string>
{
{"fq", dateQuery},
{"sort", "savedate desc"}
}
});

Related

Is there a way to replace or transform the result of a solr subquery to a multivalued field?

I have documents with fields id, relatedIds, value and want to have the values of value of the documents with related ids in the result. My current solution is a subquery p with fl: value,relatedIds,p:[subquery] and p.q: {!terms f=id v=$row.relatedIds}. The response contains a field p with the result of the subquery.
Is there a way to get the result as a (multivalued) field p containing only the values of the field value of the result documents of the subquery?
Instead of
{
"value":"ABC",
"relatedIds":["33344876", "33358333"],
"p":{"numFound":4,"start":0,"numFoundExact":true,"docs":[
{
"value":"DEF",
"id":"33358333"},
{
"value":"GHI",
"id":"33344876"}]
}
},...
I would like to have this result:
{
"value":"ABC",
"relatedIds":["33344876", "33358333"],
"p":["DEF","GHI"]
},...
Is there a way to accomplish this in a single solr query?
Would there even a way to combine this with the main result documents value field to
{
"value":"ABC",
"relatedIds":["33344876", "33358333"],
"values":["ABC","DEF","GHI"]
},...
and possibly remove duplicate values from it?

Solr facet with additional metadata

Is it possible to use additional metadata fields when using Solr facets? I would like to aggregate one attribute by counting them and desplaying the related group as additional metadata field.
http://localhost:8983/solr/gitIndex/select?indent=on&q=*:*&rows=0&wt=json&
json.facet={
Repository_s: {
type: terms,
field: Repository_s,
limit: 10,
facet: {
x:"count()"
}
}
}
The result should look like this:
...
"facets":{
"count":1354013,
"<name of attribute>":{
"buckets":[{
"val":"<value of attribute>",
"count":173997,
"<metadata_field>":<value of metadata_field>},
...
A solution is to use facet pivots - it'll get you any values in a secondary field under each facet, and if the value is unique for the set of documents, it'll just be a single value.
The reference guide has the syntax for non-json facets.

Solr - Sort documents based on chidren

I have a schema with nested documents that looks like:
{
"id":"227686",
"ProductID":"227686",
"type":"product",
"SKU":"DAFA2A1F047E438B8462667F987D80A5",
"Name":"product name",
"ShortDescription":"s description",
"UOM":"Unit",
"UomSize":"48",
"CategoryID":59,
"CategoryName":"Produce",
"ManufacturerID":322,
"ManufacturerName":"-------",
"Active":"true",
"_version_":1509403723402575872,
"_childDocuments_":[
{
"id":"227686_83",
"type":"buyer",
"BuyerID":83,
"DisplayOrder":0,
"ProductID":"227686"},
{
"id":"227686_86",
"type":"buyer",
"BuyerID":86,
"DisplayOrder":10,
"ProductID":"227686"},
{
"id":"227686_83_84",
"type":"seller",
"BuyerID":83,
"SellerID":84,
"SellerName":"-----",
"ProductID":"227686"},
{
"id":"227686_83_89",
"type":"seller",
"BuyerID":83,
"SellerID":89,
"SellerName":"-----",
"ProductID":"227686"},
]},
Is there a way to query so I can get parent documents sorted by child document's DisplayOrder field?
I want to query for a product name and get results for a specific buyer and I do:
http://localhost:8983/solr/dine/select?q=Name:"product name"&fq={!parent%20which=type:product v="type:buyer AND BuyerID=83"}&wt=json&indent=true&fl=*,[child%20parentFilter=type:product%20childFilter=%22((type:buyer%20AND%20BuyerSiteID:83)%20OR%20(type:seller%20AND%20BuyerSiteID:83))%22%20%20limit=1000]&rows=1000
But the results are not sorted by child document's DisplayOrder field.
Thanks.
It's possible with a function sort see: https://blog.griddynamics.com/how-to-sort-parent-documents-by-child-attributes-in-solr
&sort={!parent which=doc_type:parent score=max v=’+doc_type:child +{!func}DisplayOrder’} asc
This means that you will need to add a field to identify the parent and child.
You can use the sort parameter to achieve this.
sort: The sort parameter arranges search results in either ascending (asc) or descending (desc) order. The parameter can be used with either numerical or alphabetical content.
You need to either index your DisplayOrder field or define it as DocValue in the schema and it should not be a multivalue field.
If you do not define DisplayOrder field as DocValues then it should not be tokenzied by any analyzer or it should uses KeywordTokenizer, which essentially produces single term.
You can find the more information here. Common Query Parameters

Solr facet counts for specific field values

Solr creates multi-select facet counts for me as described here:
https://web.archive.org/web/20131202095639/http://wiki.apache.org/solr/SimpleFacetParameters#Multi-Select_Faceting_and_LocalParams
I also have various predefined searches that allow a user to browse the catalog. Here is one such example and its query parameters:
q=*:*
fq={!tag=g}genre:western
facet=on
facet.field={!ex=g}genre
facet.mincount=1
facet.limit=50
With this search I get up to 50 genre values in the facet list. I then go through and mark which values were selected by the user; western in this case. This works well except when western is pushed out of the top 50. So I manually add it to the list to make a total of 51. This way the user can see that it is indeed selected. The problem is I have to leave the count for western blank because I don't know it.
Is there a way to get counts for specific facet values such as western in this case? Or another approach to solve this issue?
I am using Solr 4.7.0.
Solr allows you to create a query-based facet count by using the facet.query parameter. When creating a filter query (fq) that's based on a facet field value, I now create a corresponding facet query:
facet.query={!ex=g}genre:western
and add it to the rest of my parameters:
q=*:*
fq={!tag=g}genre:western
facet=on
facet.field={!ex=g}genre
facet.query={!ex=g}genre:western
facet.mincount=1
facet.limit=50
The facet_queries object will now be populated in the solr response:
{
...
"facet_counts": {
"facet_queries": {
"{!ex=g}genre:western": 7
},
...
},
...
}
Regardless of what is returned in the facet_fields object, I'm now guaranteed to have a facet count for genre:western. With some parsing, facet field counts can be extracted from the facet queries.

Solr Faceting on Multiple Concatenated Fields

I need a way to get facets on two combined field names. To show you what I mean, take a look at the query as it is now:
{
"responseHeader":{
"status":0,
"QTime":16,
"params":{
"facet":"true",
"indent":"true",
"q":"productId:(1 OR 2 OR 3 OR 4)",
"facet.field":["productMetaType",
"productId"],
"rows":"10"}},
"response":{"numFound":4,"start":0,"docs":[
{
"productId":1,
"productMetaType":"PRIMARY_PHOTO",
"url":"1_PRIM.JPG"},
{
"productId":1,
"productMetaType":"OTHER_PHOTO",
"url":"1_1.JPG"},
{
"productId":1,
"productMetaType":"OTHER_PHOTO",
"url":"1_2.JPG"},
{
"productId":2,
"productMetaType":"OTHER_PHOTO",
"url":"2_1.JPG"}]
},
"facet_counts":{
"facet_queries":{},
"facet_fields":{
"productMetaType":[
"PRIMARY_PHOTO",1,
"OTHER_PHOTO",3],
"productId":[
"1",3,
"2",1]},
"facet_dates":{},
"facet_ranges":{}
}
}
I get two facet fields, productMetaType and productId. What I need to do is somehow combine those fields so I get data back something like this:
1_PRIMARY_PHOTO, 1,
1_OTHER_PHOTO, 2,
2_PRIMARY_PHOTO, 0,
2_OTHER_PHOTO, 1
Does the pivot functionality do this? Unfortunately, we're running Solr 3.1, so pivot isn't available, but if that is the only way to do this, I might have some ammo for upgrading.
The only other thing I could think of was some how concatenating the field names. I am new to Solr and don't know what is possible. Any advice or assistance is appreciated. Thank you for your time.
Yes, Pivot would work do the trick, but as you observed, this feature is only available in Solr trunk.
Your idea to combine both fields would work too. Actually, if your fields have a limited number of values, the easiest and most flexible way to do this would be to use facet queries:
productId:1 AND productMetaType:PRIMARY_PHOTO
productId:2 AND productMetaType:OTHER_PHOTO
productId:1 AND productMetaType:OTHER_PHOTO
productId:2 AND productMetaType:PRIMARY_PHOTO
Otherwise, just create a new field in your Solr schema.xml with string type, recreate your index by adding your documents as previously, but with this new field (that you can generate as you wish, using '_' as a separator between the two field values would work perfectly).

Resources