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?
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?
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.Another solution is using a ConstantScoreQuery with a FieldValueFilter
I've done some experimenting, and it seems the simplest way to achieve this is to create a
QueryParser
and callSetAllowLeadingWildcard( true )
and search forfield:*
like so:(Note I am setting the default field of the
QueryParser
tofield
in its constructor, hence the search for just"*"
inParse()
).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 likefield:[0* TO z*]
, which may not account for all possible values, such as values starting with non-alphanumeric characters.