Solr function query that operates on count of multivalued field - solr

Can I, within a Solr function query, count the number of values in a multivalued field? How would I write a function query that returns documents with, say, 3 or more values for a particular field?

Here's the function query reference, and it doesn't list anything like that, so I think it's safe to assume that there's no such thing.
If the value count is somehow relevant in your case, add it as a separate int field, then operate on that field.

Related

In Solr, how to get all the unique values from a field? there are more than 100 values?

Below query gives be the unique values of that field. But it returns only top 100. Not sure how to get all the unique values.
"'http://localhost:8983/solr/select/?q=%3A&rows=0&facet=on&facet.field=txt'"
PS: My field is a tokenized field. Not sure if that makes any difference.
You can use facet.limit to change the number of returned values. Set it to -1 to make Solr return all possible values for a field and not just the top 100.
&facet=on&facet.field=txt&facet.limit=-1
You can also use f.txt.facet.limit=-1 if you have more than one facet and just want to change the value for that single field.
You can also use the Terms component to retrieve the tokens indexed for a specific field - depending on your use case it might be more efficient to use the terms component instead - you'll have to evaluate the performance for your exact use case and setup.

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.

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:[*]

to get the count of multi valued field in solr

In solr i have a multiValued field called animal and it has the values {cat,dog} is it possible to get the number of values inside the multiValued field in solr(in my example 2)?
If you want to count the items in a multivalued field use CountFieldValuesUpdateProcessorFactory
There is no direct way to get the count of items in a multivalued field.
You can always maintain the field count during indexing and use it.
If not using during query time, you can always count the list size.

Using multivalued field in map function

I'm working on implementing Solr in a project and right now I'm stuck on a specific search including an arr field. The thing is:
I'd like to search sub-id's on an object, these sub-id's are stored in a multivalue field, e.g.:
<arr name="SubIds">
<int>12272</int>
<int>12304</int>
<int>12306</int>
</arr>
The query (or part of the query) that I want to use is as follows:
map(SubIds,i,i,1,0)
When I, for example, fill 12304 on the 'i' space in the map function above, I would expect my function to return 1. If I would enter 12345 it should return 0. The thing is that when I run this query it returns 0, or "There's no number 12304 in this field, I return 0".
When removing the 0 from my map function I can see the actual value returned to me (when 12304 return 1, when different return value), in this case that's 12306! I've tried this with some different multivalued fields but the result is the same; it looks like the function is checking the last value in the multivalue field against my filled in ID.
Is this true? And when it does, is there any way in looking through the whole arr and only return 0 when the value doesn't exist in the whole multivalued field?
** Edit: It's just a hunch, but could it be that the map() function automatically orders the arr list when it sees that all the items are of type int (for example). That could mean that the map returns the first number (the highest) which would (in my example) be 12306, not 12304...*
Thanks!
... It looks like function queries don't work with multivalued fields ...
http://lucene.472066.n3.nabble.com/Using-multivalued-field-in-map-function-td3318843.html#a3322023:
Function queries don't work with multivalued field.
http://wiki.apache.org/solr/FunctionQuery#Vector_Functions
Given the following case, is there anybody who has a better idea on how I can query the wanted data?
I've got a website full of blogposts and every blogpost has an owner,
this owner is refererred to through his/her id. For example: BloggerId
= 123. It's also possible that the blog has multiple co-writers, which
are also referred to by there BloggerId but these id's are stored in
the multivalue field, in my previous example SubIds.
When searching for a specific blogger one searches the BloggerId.
Searchresults are influenced by a number of variables, the
country/state/more specific geological data, the blogcategory, etc.
For this I use a facetted query. Next I want to make some results more
important, depending on the BloggerId, I tried to do this with the
following query:
?q={!func}map(sum(map(BloggerId,12304,12304,2,0),map(BloggerId,12304,12304,1,0)),3,3,2)&fl=*,score&facet.field=Country&f.Country.facet.limit=6&facet.field=State&fq=(BlogCategory:internet%20OR%20BlogCategory:sports&sort=score%20desc,Top%20desc,%20SortPriority%20asc&start=0&omitHeader=true
In the resulting list, blogs written by BloggerId 12304 should be on
top of the list, followed by the blogs where BloggerId 12304 was
co-writer. After that, all other blogs that follow the criteria but
aren't written (or co-written) by BloggerId 12304.
Maybe I could make this multivalued field a string field (where id's are seperated by ";") and query my value, but if one has a better idea your always welcome!
In the end I chose to add a string valued field with whitespaces to seperate the different values. After that I used the solr.WhitespaceTokenizerFactory class to quickly scan the string for occurences of a specific ID.

Resources