I need to find all the documents in my collection for which the length of a certain field exceeds a certain limit. The field's type is string. Is there a function query that returns the length of a string field in Solr?
Asked
Active
Viewed 1.5k times
2 Answers
37
The solution is to use Solr's regex capabilities. For example, the following query retrieves all the documents for which the title
field is at least 42:
title:/.{42}.*/

snakile
- 52,936
- 62
- 169
- 241
-
I have a document who's "description" field is 292 chars and 28 words (the largest description in the collection), but somehow when i query `description:/.{71}.*/` there are 0 results, and querying`description:/.{70}.*/` does return my document... what am I missing? – Joey Baruch Feb 13 '17 at 07:57
-
@joey, what is the type of the field? – snakile Feb 13 '17 at 08:40
-
org.apache.solr.schema.TextField – Joey Baruch Feb 13 '17 at 09:10
-
@snakile same issue here :/ – Rodrigo Espinoza Oct 19 '18 at 18:46
-
Yeah this actually only works for very long words... so are any words greater then or equal too 42 chars. At least that is what I am seeing when I try too use this approach – byoungb Apr 20 '20 at 20:39
-
for me this worked great, but only with indexed fields, not just mere stored fields. – fabb Aug 24 '23 at 07:18
2
Querying by length is not supported out of the box, the most sensible way would be to create a separate (numeric) field and query by this field.

mindas
- 26,463
- 15
- 97
- 154
-
So I have to change my schema in order to do so? This is a one time thing that I need to do. Is the best way really to define a length field and then to throw it away? – snakile Apr 24 '14 at 14:25
-
2If it's a one-off thing, you could [iterate over all documents](http://stackoverflow.com/questions/5051460/solr-solrj-how-to-iterate-results-without-creating-a-giant-arraylist) and do the processing manually. – mindas Apr 24 '14 at 14:32
-
+1 because your solution works and is totally fine, but I think I've came up with an easier one. See my post. – snakile Apr 27 '14 at 15:05