I am using Lucene.Net version 4.8.0 with .NET Core 3.1. In the sample code below, I write a new index and add three documents to it. Each document contains the fields ProjectName
, Customer
and Country
.
When I parse the query "Germany"
, the search returns 2 hits for documents that contain the word "Germany" in any of its fields. However, when I parse the query "Country:Germany"
, the search returns 0 hits, although there clearly is a document with the value "Germany" in its Country
field.
What am I doing wrong?
I also checked my index by means of the Luke tool ( https://github.com/DmitryKey/luke/releases/tag/4.8.0 ). With Luke, the search works fine for the same index directory.
My C# code is here:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Lucene.Net.Analysis.Standard;
using Lucene.Net.Documents;
using Lucene.Net.Index;
using Lucene.Net.QueryParsers.Classic;
using Lucene.Net.Search;
using Lucene.Net.Store;
using Lucene.Net.Util;
using Directory = System.IO.Directory;
namespace LuceneTestApp
{
class Program
{
static string CreateTestIndex()
{
string indexDir = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
if(Directory.Exists(indexDir))
throw new IOException("Random index directory already exists. Please try again.");
using var dir = FSDirectory.Open(indexDir);
using var analyzer = new StandardAnalyzer(LuceneVersion.LUCENE_48);
var indexConfig = new IndexWriterConfig(LuceneVersion.LUCENE_48, analyzer);
using var writer = new IndexWriter(dir, indexConfig);
AddDocumentTo(writer, "AwesomeProject_1", "Volkswagen", "Germany");
AddDocumentTo(writer, "AwesomeProject_2", "Ford", "USA");
AddDocumentTo(writer, "AwesomeProject_3", "Audi Germany", "France");
writer.Commit();
return indexDir;
}
static void AddDocumentTo(IndexWriter writer, string projectName, string customer, string country)
{
var doc = new Document();
doc.Add(new StringField( "ProjectName", projectName, Field.Store.YES));
doc.Add(new TextField( "Customer", customer, Field.Store.YES));
doc.Add(new TextField( "Country", country, Field.Store.YES));
writer.AddDocument(doc);
}
static IList<string> Search(string indexDir, string queryString)
{
using var dir = FSDirectory.Open(indexDir);
using var reader = DirectoryReader.Open(dir);
using var analyzer = new StandardAnalyzer(LuceneVersion.LUCENE_48);
var searcher = new IndexSearcher(reader);
string[] searchFields = {"ProjectName", "Customer", "Country"};
var queryParser = new MultiFieldQueryParser(LuceneVersion.LUCENE_48, searchFields, analyzer);
queryParser.DefaultOperator = Operator.AND;
var query = queryParser.Parse(queryString.ToLowerInvariant());
int maxNumHits = 10;
var topDocs = searcher.Search(query, maxNumHits);
return topDocs.ScoreDocs.Select(hit => $"Score {hit.Score,5:0.000} DocId {hit.Doc}").ToList();
}
static void Main(string[] args)
{
Console.WriteLine("=================");
string indexDir = CreateTestIndex();
IList<string> hitsOne = Search(indexDir, "Germany");
IList<string> hitsTwo = Search(indexDir, "Country:Germany");
Console.WriteLine($"Search one yields {hitsOne.Count} hits.");
Console.WriteLine($"Search two yields {hitsTwo.Count} hits.");
Console.WriteLine("=================\n\n");
}
}
}
I found the mistake I made. Apparently, field names in Lucene are case-sensitive. Hence, in my case the code queryString.ToLowerInvariant()
turned the query string "Country:Germany"
into "country:Germany"
and, thus, nothing was found, because the lower-case field country
does not exist.
Solution: Remove the call to ToLowerInvariant()
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.