0
votes

I am new to lucene.net. I am developing an API to scan through the namelist. The Parameter will enter a full name of the person.

However with the current code, if i enter 'Pit Bull' search against the namelist, it will appear 'Pit Bull Adam', 'Pit Tim Bull' and etc...

But the result i want is 'Pit Bull' or 'Bull Pit' only.

Am i use the correct Analyzer or query? What should I do?

This is my index created.

        var doc = new Document();
        doc.Add(new Field("Id", model.EntityId.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED));
        doc.Add(new Field("FullName", model.FullName, Field.Store.YES, Field.Index.ANALYZED));
        doc.Add(new Field("Name2", model.Name2?? "", Field.Store.YES, Field.Index.ANALYZED));
        doc.Add(new Field("Name3", model.Name3?? "", Field.Store.YES, Field.Index.ANALYZED));
        doc.Add(new Field("Gender", indexModel.GenderType.HasValue ? indexModel.GenderType.Value.ToString() : "", Field.Store.YES, 
        Field.Index.NOT_ANALYZED, Field.TermVector.NO));

This is my search method.

using (var indexReader = IndexReader.Open(ramDirectory, true))
        {
            using (var searcher = new IndexSearcher(indexReader))
            {

                var analyzer = new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30);

                MultiFieldQueryParser _MultiMatchName = new 
                MultiFieldQueryParser(Lucene.Net.Util.Version.LUCENE_30,
                    new string[] { "FullName", "Name2", "Name3" }, analyzer);

                const int hitLimits = 1000;

                _MultiMatchName.DefaultOperator = QueryParser.Operator.AND;

                var query = new BooleanQuery();

                query.Add(inputName,Occur.MUST);

            }
        }
1

1 Answers

0
votes

It turned out i should use WhitespaceAnalyzer. When creating Index and searching index.

using (var indexReader = IndexReader.Open(ramDirectory, true))
    {
        using (var searcher = new IndexSearcher(indexReader))
        {

            var analyzer = new WhitespaceAnalyzer();

            MultiFieldQueryParser _MultiMatchName = new 
            MultiFieldQueryParser(Lucene.Net.Util.Version.LUCENE_30,
                new string[] { "FullName", "Name2", "Name3" }, analyzer);

            const int hitLimits = 1000;

            _MultiMatchName.DefaultOperator = QueryParser.Operator.AND;

            var query = new BooleanQuery();

            query.Add(inputName,Occur.MUST);

        }
    }