salar
salar

Reputation: 709

Lucene.net does not find matches correctly

I am new in lucene.net , in some searches that i had , i found that i can use lucene in my project ,

now i can not fix the bugs in my code .

Let me I explain in Code

First of all i create indexes like these

        var strIndexDir = path;
        Directory indexDir = FSDirectory.Open(new DirectoryInfo(strIndexDir));
        Analyzer std = new StandardAnalyzer(global::Lucene.Net.Util.Version.LUCENE_30)

     foreach (var res in resturant)
        {

            var doc = new Document();

          restaurantName = new Field("Name",
                res.Name, Field.Store.YES,
                Field.Index.ANALYZED, Field.TermVector.YES);
            var restaurantId = new Field("Id",
                res.RestaurantId.ToString(), Field.Store.YES,
                Field.Index.NO, Field.TermVector.NO);
            var restaurantSlug = new Field("Slug",
                res.Slug, Field.Store.YES,
                Field.Index.NO, Field.TermVector.NO);
            var restaurantAddress = new Field("Address",
                res.Address ?? "empty", Field.Store.YES,
                Field.Index.NOT_ANALYZED, Field.TermVector.YES);
            var resturantType = new Field("Type",
                "restaurant", Field.Store.YES,
                Field.Index.NO, Field.TermVector.NO);
            doc.Add(restaurantName);
            doc.Add(restaurantId);
            doc.Add(restaurantSlug);
            doc.Add(restaurantAddress);
            doc.Add(resturantType);

            idxw.AddDocument(doc);
        }
        idxw.Optimize();
        idxw.Close();

I Think with my self the indexing is ok , becuase i want just find the restaurant name and addresses

also for search query i use this way

     string strIndexDir = path;
        Analyzer std = new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30);




     var indexReader = IndexReader.Open(FSDirectory.Open(path), readOnly: true);
        var parserName =
        new Lucene.Net.QueryParsers.QueryParser(Lucene.Net.Util.Version.LUCENE_30, "Name", std);
        var parserAddress =
          new Lucene.Net.QueryParsers.QueryParser(Lucene.Net.Util.Version.LUCENE_30, "Address", std);
        var parserSlug =
          new Lucene.Net.QueryParsers.QueryParser(Lucene.Net.Util.Version.LUCENE_30, "Slug", std);
        var parserTitle =
          new Lucene.Net.QueryParsers.QueryParser(Lucene.Net.Util.Version.LUCENE_30, "Title", std);
        var searcher = new IndexSearcher(FSDirectory.Open(path));

        using (var srchr = new IndexSearcher(IndexReader.Open(directory,true)))
        {


            var qryName = parserName.Parse(q);
            var qryAddress = parserAddress.Parse(q);
            var qrySlug = parserSlug.Parse(q);
            var qrytitle = parserTitle.Parse(q);
            var cllctr = TopScoreDocCollector.Create(10, true);
            searcher.Search(qryName, cllctr);
            searcher.Search(qryAddress, cllctr);
            searcher.Search(qrySlug, cllctr);
            searcher.Search(qrytitle, cllctr);
            var hits = cllctr.TopDocs().ScoreDocs;

Now let me say where is the problem .

for example i search this key word (q="box") want to find the restaurant name that name is boxshaharkgharb and want use "box"

the problem is that hot is always 0 but when i type boxshaharkgharb for example (q="boxshaharkgharb") the the result is ok .

how can handel that

Upvotes: 1

Views: 489

Answers (1)

Giedrius
Giedrius

Reputation: 8540

By using wildcard * you can force Lucene to search by fragment.

If you need to do this for all queries - you need to review your choice - as Lucene best performs using whole term searches. Reason for that is that by default wildcards turn into constant score queries, while term search uses relevancy to rank results.

Upvotes: 1

Related Questions