Lucene.NET and search across multiple fields with specific values

I created an index with different bits of data for each document to be added, each document may differ in it by field name.

Later, when I come to search for the index, I need to query it with exact fields / values โ€‹โ€‹- for example:

FieldName1 = X AND FieldName2 = Y AND FieldName3 = Z 

What is the best way to create the following using Lucene.NET:

  • Which analyzer is best used for this type of exact match?
  • After getting the match, I need only one specific field that needs to be returned (which I add to each document) - should it be the only one that has been saved?
  • Later I will need to support keyword searches (so the field can have a list of values, and I need to do a partial match).

Fields and values โ€‹โ€‹are taken from Dictionary<string, string> . This is not user input, it is built from code.

Thanks,
Kiron

+7
source share
1 answer

Well, in the end, I figured it out - here is my take over (it may be completely wrong, but it works):

 public Guid? Find (Dictionary<string, string> searchTerms) { if (searchTerms == null) throw new ArgumentNullException ("searchTerms"); try { var directory = FSDirectory.Open (new DirectoryInfo (IndexRoot)); if (!IndexReader.IndexExists (directory)) return null; var mainQuery = new BooleanQuery (); foreach (var pair in searchTerms) { var parser = new QueryParser ( Lucene.Net.Util.Version.LUCENE_CURRENT, pair.Key, GetAnalyzer ()); var query = parser.Parse (pair.Value); mainQuery.Add (query, BooleanClause.Occur.MUST); } var searcher = new IndexSearcher (directory, true); try { var results = searcher.Search (mainQuery, (Filter)null, 10); if (results.totalHits != 1) return null; return Guid.Parse (searcher.Doc (results.scoreDocs[0].doc).Get (ContentIdKey)); } catch { throw; } finally { if (searcher != null) searcher.Close (); } } catch { throw; } } 
+7
source share

All Articles