Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find all Lucene documents having a certain field

Tags:

lucene

I want to find all documents in the index that have a certain field, regardless of the field's value. If at all possible using the query language, not the API.

Is there a way?

like image 371
Michael Böckling Avatar asked Sep 14 '10 14:09

Michael Böckling


2 Answers

If you know the type of data stored in your field, you can try a range query. Per example, if your field contain string data, a query like field:[a* TO z*] would return all documents where there is a string value in that field.

like image 114
Pascal Dimassimo Avatar answered Oct 29 '22 15:10

Pascal Dimassimo


I've done some experimenting, and it seems the simplest way to achieve this is to create a QueryParser and call SetAllowLeadingWildcard( true ) and search for field:* like so:

var qp = new QueryParser( Lucene.Net.Util.Version.LUCENE_29, field, analyzer );
qp.SetAllowLeadingWildcard( true );
var query = qp.Parse( "*" ) );

(Note I am setting the default field of the QueryParser to field in its constructor, hence the search for just "*" in Parse()).

I cannot vouch for how efficient this method is over other methods, but being the simplest method I can find, I would expect it to be at least as efficient as field:[* TO *], and it avoids having to do hackish things like field:[0* TO z*], which may not account for all possible values, such as values starting with non-alphanumeric characters.

like image 20
devios1 Avatar answered Oct 29 '22 14:10

devios1