Lucene not null query?
Asked Answered
T

8

26

How can we construct a query to search for particular field to be not null?

field_name:* is not working. I tried field_name:[a* to z*] this works fine for English, but does not cover all languages.

Any alternative suggestions?

Tum answered 21/3, 2011 at 22:20 Comment(0)
R
6

This is currently not supported by Lucene. See this for a discussion.

An alternative option may be to store some pre-defined string (like nullnullnullnull) as the field value if it is null. Then you can use a negative filter to remove these records. (I don't like this much, but can't think of a better option)

Rathskeller answered 22/3, 2011 at 6:58 Comment(1)
this answer is outdated, since Lucene introduced FieldValueQuery. Please refer to my answer belowKarlenekarlens
D
18

I found this to work in some cases field:([0 TO 9] [a TO z])

Deadwood answered 28/10, 2011 at 14:17 Comment(1)
In that case I would prefer: field:([0 TO 9] [a TO z] [A TO Z]) to get also starting capital letterAncell
T
16

For anyone else arriving late to the question, the documentation includes this little snippet:

  • where the field title has any non-null value:
    _exists_:title
Troup answered 19/12, 2018 at 19:14 Comment(0)
C
11

Try field:[* TO *] or field:["" TO *]. But it's probably better to use a filter for this though.

Cackle answered 21/3, 2011 at 22:27 Comment(1)
The latter worked for me with lucene version 6.6.1, es version 5.6.8.Sheryllshetland
R
6

This is currently not supported by Lucene. See this for a discussion.

An alternative option may be to store some pre-defined string (like nullnullnullnull) as the field value if it is null. Then you can use a negative filter to remove these records. (I don't like this much, but can't think of a better option)

Rathskeller answered 22/3, 2011 at 6:58 Comment(1)
this answer is outdated, since Lucene introduced FieldValueQuery. Please refer to my answer belowKarlenekarlens
C
3

I was having the same problem but there's a property you can set on the query parser which lets you have wildcard characters at the start of a search term.

queryParser.setAllowLeadingWildcard(true);

This solved the problem for me

Please see Wildcard at the Beginning of a searchterm -Lucene

Chloromycetin answered 24/10, 2011 at 10:48 Comment(0)
B
3

In the current Lucene version FieldValueQuery is outdated. Currently you can use:

new DocValuesFieldExistsQuery(name)

This works only for SortedDocValuesFields, so you have to add them while creating the document.

doc.add(new SortedDocValuesField(name, new BytesRef(value));
doc.add(new StringField(name, value, Field.Store.Yes)); //optional

The second line enables you to retrieve the value (DocValuesFields' values cannot be read as Strings).
Note that lucene allows for multiple fields with the same name as in the aforementioned example.

Balk answered 20/6, 2019 at 10:59 Comment(0)
K
0

Have a look at org.apache.lucene.search.FieldValueQuery:

A Query that matches documents that have a value for a given field as reported by LeafReader#getDocsWithField(String).

please note, that it only works with DocValues, so you would need to change a way you create your document:

document.add(new StringField("field-name", "field-value", Field.Store.YES));
document.add(new SortedDocValuesField("field-name", new BytesRef("field-value")));

here I added two fields - you still need regular StringField to get value. You may choose to use BinaryDocValues#get() for older versions of Lucene, but as I see, it is removed in v7. Not sure, what is a proper way to retrieve a value now - please check this

Karlenekarlens answered 1/8, 2018 at 22:55 Comment(0)
T
-1

I have just started to play around with lucene (via logstash elastic search) and find that this seems to work from the kibana UI. I am not sure yet if this is some intelligence in elastic search or kibana, i just know that elastic search borrows from the lucene syntax.

application:unit-test && !exception

will return all results from my unit tests which have not had an exception

application:unit-test && exception

returns those which have a non null exception indexed. so you might try just

field

or

!field
Thilde answered 18/6, 2014 at 0:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.