Solr - Sort documents based on chidren - solr

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

Related

Mongo DB Query to check if document array field element present in more than one document

I have been searching through the MongoDB query syntax with various combinations of terms to see if I can find the right syntax for the type of query I want to create.
We have a collection containing documents with an array field. This array field contains ids of items associated with the document.
I want to be able to check if an item has been associated more than once. If it has then more than one document will have the id element present in its array field.
I don't know in advance the id(s) to check for as I don't know which items are associated more than once. I am trying to detect this. It would be comparatively straightforward to query for all documents with a specific value in their array field.
What I need is some query that can return all the documents where one of the elements of its array field is also present in the array field of a different document.
I don't know how to do this. In SQL it might have been possible with subqueries. In Mongo Query Language I don't know how to do this or even if it can be done.
You can use $lookup to self join the rows and output the document when there is a match and $project with exclusion to drop the joined field in 3.6 mongo version.
$push with [] array non equality match to output document where there is matching document.
db.col.aggregate([
{"$unwind":"$array"},
{"$lookup":{
"from":col,
"localField":"array",
"foreignField":"array",
"as":"jarray"
}},
{"$group":{
"_id":"$_id",
"fieldOne":{"$first":"$fieldOne"},
... other fields
"jarray":{"$push":"$jarray"}
}},
{"$match":{"jarray":{"$ne":[]}}},
{"$project":{"jarray":0}}
])

SoLR - How to filter out records which do not have the field value for field listed in SORT?

In SoLR, We can set sortMissingLast=true on a field in the schema. If the SORT is on this field, it will to push the results which have missing field values to the end.
Is there a way to filter out the results whose sort field values are missing? Please advise.
If you want to restrict your query to documents that only have a certain field set, add the following parameter to your query:
sort_field_name:[* TO *]
This will limit the query to only those documents where the sort field exists.

Difference between Solr Facet Fields and Filter Queries

I am using SolrMeter to test Apache Solr search engine. The difference between Facet fields and Filter queries is not clear to me. SolrMeter tutorial lists this as an exapmle of Facet fields :
content
category
fileExtension
and this as an example of Filter queries :
category:animal
category:vegetable
categoty:vegetable price:[0 TO 10]
categoty:vegetable price:[10 TO *]
I am having a hard time wrapping my head around it. Could somebody explain by example? Can I use SolrMeter without specifying either facets or filters?
Facet fields are used to get statistics about the returned documents - specifically, for each value of that field, how many returned documents have that value for that field. So for example, if you have 10 products matching a query for "soft rug" if you facet on "origin," you might get 6 documents for "Oklahoma" and 4 for "Texas." The facet field query will give you the numbers 6 and 4.
Filter queries on the other hand are used to filter the returned results by adding another constraint. The thing to remember is that the query when used in filtering results doesn't affect the scoring or relevancy of the documents. So for example, you might search your index for a product, but you only want to return results constrained by a geographic area or something.
A facet is an field (type) of the document, so category is the field. As Ansari said, facets are used to get statistics and provide grouping capabilities. You could apply grouping on the category field to show everything vegetable as one group.
Edit: The parts about searching inside of a specific field are wrong. It will not search inside of the field only. It should be 'adding a constraint to the search' instead.
Performing a filter query of category:vegetable will search for vegetable in the category field and no other fields of the document. It is used to search just specific fields rather than every field. Sometimes you know that the term you want only is in one field so you can search just that one field.

SOLR sort by IN Query

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"}
}
});

solr query without order

I have a solr index with the unique field as "id".
I have a ordered set of ids, using which I would like to query Solr. But I want the results in the same order.
so for example if i have the ids id = [5,1,3,4] I want the results displayed in solr in same order.
I tried http://localhost:8983/solr/select/?q=id:(5 OR 1 OR 3 OR 4)&fl=id, but the results displayed are in ascending order.
Is their a way to query solr, and get results as I mentioned?
I think you can't,
The results appear in the order they are indexed unless you specify a default sort field or the explicit sort field/order.
You can add another field to keep the initial sort order. You then can sort=field asc to retrieve the data in the original order.
The simple way is to query solr and sort the results in codes of yourself.

Resources