Find all Lucene documents having a certain field

前端 未结 3 730
星月不相逢
星月不相逢 2020-12-05 19:24

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 t

相关标签:
3条回答
  • 2020-12-05 19:49

    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.

    0 讨论(0)
  • 2020-12-05 20:04

    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.

    0 讨论(0)
  • 2020-12-05 20:05

    Another solution is using a ConstantScoreQuery with a FieldValueFilter

    new ConstantScoreQuery(new FieldValueFilter("field"))
    
    0 讨论(0)
提交回复
热议问题