Find all Lucene documents having a certain field

2020-01-31 03:31发布

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?

标签: lucene
3条回答
我欲成王,谁敢阻挡
2楼-- · 2020-01-31 03:32

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.

查看更多
Evening l夕情丶
3楼-- · 2020-01-31 03:35

Another solution is using a ConstantScoreQuery with a FieldValueFilter

new ConstantScoreQuery(new FieldValueFilter("field"))
查看更多
冷血范
4楼-- · 2020-01-31 03:38

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.

查看更多
登录 后发表回答