13

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?

snakile
  • 52,936
  • 62
  • 169
  • 241

2 Answers2

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
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
  • 2
    If 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