1 year ago

#314251

test-img

Ion

Lucene.Net: How to match one or more of the search Fields?

I've started currently to use Lucene.Net to search across some txt files. I've implemented the single field search and it works awesome, but I'm stuck at the multiple fields part. It seems that the term should match all the fields to get a Hit, but I want to be able to match one or more.

For example if would have the Fields: id:10, name:exampleName, brand:exampleBrand and the term = "exampleName", then it should be a hit.

Here is what I tried:

        public IEnumerable<Document> Search(string searchTerm, string[] searchFields, int limit)
        {
            DirectoryReader ireader = DirectoryReader.Open(_indexDirectory);
            var searcher = new IndexSearcher(ireader);
            var parser = new MultiFieldQueryParser(LuceneVersion.LUCENE_48, searchFields, _analyzer);

            var query = parser.Parse(searchTerm);
            var hits = searcher.Search(query, limit).ScoreDocs;

            var documents = new List<Document>();
            foreach (var hit in hits)
            {
                documents.Add(searcher.Doc(hit.Doc));
            }

            return documents;
        }

but in my case I don't get a hit and I always get 0 results.

Edit: My fault, it seems like my code is working like it should do.

asp.net

lucene.net

0 Answers

Your Answer

Accepted video resources