Lucene.NET - search for documents that do not contain the specified field

Let's say I have 2 instances of a class called "Animal".

The animal has 3 fields: name, age and type

The name field is NULL, so before I insert an instance of Animal as a document with the Lucene index, I check if Animal.Name == null, and if so, I do not insert it as a field in my document. If I were to return all the animals, I would see that the Name field does not exist, and I can set its value to null.

However, there may be situations where I want to say, "Bring me all the animals that do not yet have the name indicated." In this situation, I want to get all Lucene.NET documents from my index of animals that do not contain the Name field.

Is there an easy way to do this with Lucene.NET? I want to stay away from having to do some kind of hack to check if my name field has a value of "null".

+3
source share
1 answer

I believe that you can do this with Solr, but not directly with Lucene, so this is not possible with Lucene.Net.

Here are two workarounds that are not so bad:

  • For elements with a NULL value, add a custom type string __NULL__or similar to the field, instead of omitting the field. This will be searchable.
  • NULL , . . EMPTY_FIELD = "no". .

, .

+5

Source: https://habr.com/ru/post/1743234/


All Articles