Developer Forum »
Using Lucene search engine to query based on first character
31 posts

Hi,

I am trying to add a search filter based on the first character. For instance if I select the letter 'A' I expect to get a match for "Apple", but not "Green Apple". Instead I'm getting a match for both. Is there a way to change this behaviour?

I am using a simple NameSearch with wildcard:

query.NameSearch = firstCharacter + "*";
42 posts

I believe you would have to use the Lucene regex query, ala new RegexQuery(newTerm("^a.*$")) which Webnodes does not support (?). Furthermore, in order to get this correctly for the Nowegian locale you should use the NorwegianAnalyzer: http://lucene.apache.org/core/3_3_0/api/all/org/apache/lucene/analysis/no/NorwegianAnalyzer.html, which Webnodes does not support (?).

I'd recommend indexing your nodes on Application_Start using a ConcurrentDictionary as well as maintaining your index in WAF_Custom.

31 posts

I solved it by adding a new field in the search index, in WAF_Custom, with the first character as value:

public override void BuildSearchIndex(Lucene.Net.Documents.Document document)
{
	base.BuildSearchIndex(document);
	document.Add(new Field("FirstCharacterInName", Name.TrimStart().Substring(0,1).ToLower(), Field.Store.YES, Field.Index.NOT_ANALYZED));
}

And then using a custom query to filter on the new field:

booleanQuery.Add(new TermQuery(new Term("FirstCharacterInName", alphabetFilter.ToLower())), BooleanClause.Occur.MUST);

query.CustomQuery = booleanQuery;
42 posts

So searching for å and not finding a now works?

If that is not the case you could index (int)char.ToLowerInvariant(c) and use NumericField and not Field.

31 posts

Yes, it does actually differentiate between "Å" and "A" now. It was only a problem in my first example, with NameSeach with wildcard.

1