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

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?

Related

How to search multiple words in one field on solr?

I have a field in solr of type list of texts.
field1:{"key1:val1,key2:val2,key3:val3", "key1:val1,key2:val2"}
I want to form a query such that when I search for key1:val1 and key3:val3 I get the result who has both the strings i.e key1:val1 and key3:val3.
How shall I form the query?
If these are values in a multivalued field, you can't - directly. You'll have to use something like highlighting to tell you where Solr matched it.
There is no way to tell Solr "I only want the value that matched inside this set of values".
If this is a necessary way to query your index, index the values as separate documents instead in a separate collection. In that case you'd have to documents instead, one with field1:"key1:val1,key2:val2,key3:val3" and one with key1:val1,key2:val2.
You can use AND with fq.
Like:
fq=key1:val1 AND key3:val3
With this filter query you will get only records where key1 = val1 AND key3 = val3.

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 - 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.

Behavior of the OR clause in Solr

My solr index contains documents which have a field named department. This field is a multivalue non-required int field. I want to construct a query whose result must be union of
All the documents that do not contain the field department
All the documents that contain the field department, but the values of the field are restricted to a selected few.
I tried constructing the query that looks like so:
-department:* OR (department:* AND department:(100 OR 200))
This doesn't return any results. Whereas if I just just use
-department:*
or
department:* AND department:(100 OR 200)
, the query seems to work well. In short I'm having trouble understanding the behavior of OR clause in this context. Any pointers?
Checkout SolrQuerySyntax
Pure Negative Queries :-
-field:[* TO *] finds all documents without a value for field
You can try :-
q=-department:[* TO *] OR department:(100 OR 200)
To achieve what you want, I think you can use Solr grouping.
You can give something like,
&q=*&group=true&group.query=department:[100]&group.query=department:[200]&group.query=-department:[*]

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

Resources