'Lucene.net query for contains and avoiding empty string fields
I have a Lucene index setup which I can query fine. I just am not able to get a "field not equals to empty string" condition to work. For example in the below code specimen, I want to have 3 conditions
- Where "country tag" field contains "{4ED2F7EE-5C2A-418C-B2F6-236F94166BA1}".
- Where "country tag" field is not empty string.
Where "date" range is between "20110101T000000" and "20121001T000000".
WildcardQuery taggingQuery = new WildcardQuery(new Term("country tag", "*" + ShortID.Encode("{4ED2F7EE-5C2A-418C-B2F6-236F94166BA1}").ToLowerInvariant() + "*")); TermQuery taggingNotQuery = new Term("country tag", " ")); RangeQuery rangeQuery = new RangeQuery(new Term("date", "20110101T000000"), new Term("date", "20121001T000000"), true); BooleanQuery booleanQuery = new BooleanQuery(); booleanQuery.Add(taggingQuery, BooleanClause.Occur.MUST); booleanQuery.Add(taggingNotQuery, BooleanClause.Occur.MUST_NOT); booleanQuery.Add(rangeQuery, BooleanClause.Occur.MUST);
I have a feeling I am doing this wrong or my query is wrong somehow. I should not need a condition where I should have to look out for empty or null fields.
Any help is appreciated!
Solution 1:[1]
I should've paid more attention when setting up the index. I forgot to add field analyzers for each field. The multilist fields were getting indexed with a different analyzer instead of the standard analyzer. I added this to my config section for field crawls and my query started working
<fieldTypes hint="raw:AddFieldTypes">
<!-- Text fields need to be tokenized -->
<fieldType name="single-line text" storageType="NO" indexType="TOKENIZED" vectorType="NO" boost="1f" />
<fieldType name="multi-line text" storageType="NO" indexType="TOKENIZED" vectorType="NO" boost="1f" />
<fieldType name="word document" storageType="NO" indexType="TOKENIZED" vectorType="NO" boost="1f" />
<fieldType name="html" storageType="NO" indexType="TOKENIZED" vectorType="NO" boost="1f" />
<fieldType name="rich text" storageType="NO" indexType="TOKENIZED" vectorType="NO" boost="1f" />
<fieldType name="memo" storageType="NO" indexType="TOKENIZED" vectorType="NO" boost="1f" />
<fieldType name="text" storageType="NO" indexType="TOKENIZED" vectorType="NO" boost="1f" />
<!-- Multilist based fields need to be tokenized to support search of multiple values -->
<fieldType name="multilist" storageType="NO" indexType="TOKENIZED" vectorType="NO" boost="1f" />
<fieldType name="treelist" storageType="NO" indexType="TOKENIZED" vectorType="NO" boost="1f" />
<fieldType name="treelistex" storageType="NO" indexType="TOKENIZED" vectorType="NO" boost="1f" />
<fieldType name="checklist" storageType="NO" indexType="TOKENIZED" vectorType="NO" boost="1f" />
<!-- Legacy tree list field from ver. 5.3 -->
<fieldType name="tree list" storageType="NO" indexType="TOKENIZED" vectorType="NO" boost="1f" />
</fieldTypes>
Solution 2:[2]
If you allow '*' as the first character in the search string, Lucene can use queries like "countrytag:*" to find all documents that contain anything in the countrytag field. (Lucene's default is to disable an initial '*' in a query string.)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Gabbar |
| Solution 2 | Mark Leighton Fisher |
