开发者

Lucene.NET - Find documents that do not contain a specified field

开发者 https://www.devze.com 2022-12-28 17:51 出处:网络
Let\'s say I have 2 instance of a class called \'Animal\'. Animal has 3 fields: Name, Age, and Type The name field is nullable, so before I insert an instance of Animal as a Lucene indexed document,

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

Animal has 3 fields: Name, Age, and Type

The name field is nullable, so before I insert an instance of Animal as a Lucene indexed document, I check if Animal.Name == null, and if it does, I do not insert it as a field in my document. If I were to retrieve all 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 "Get me all animals that do not have a name specified yet." In this situation I want to retrieve all Lucene.NET documents from my animal index that do not contain the Name field.

Is there an easy way to do this with Lucene.NET? I want to stay away fr开发者_C百科om having to perform some sort of hack to check if my name field has a value of 'null'.


I believe you can do this with Solr, but not with Lucene directly, so it's not possible with Lucene.Net.

Here's two workarounds, which are not that bad:

  1. For items with a NULL value in the field, add a custom string like __NULL__ or similar instead of omitting the field. This would be searchable.
  2. For items with a NULL value in the field, add a field which will not be present on the items with a value. Eg. EMPTY_FIELD = "no". This can be used in a filter.

Hope this helps you a bit on the way.

0

精彩评论

暂无评论...
验证码 换一张
取 消