Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Below block of code is to create index. It creates additional .cfs file whenever i execute the code.
C#
using (var directory = GetDirectory())
using (var analyzer = GetAnalyzer())
using (var writer = new IndexWriter(directory, analyzer, IndexWriter.MaxFieldLength.UNLIMITED))
{
    
    foreach (var pg in lstPage)
    {
        try {
            var document = AddDoc(pg);

            if (isScheduled.ToUpper() != "T")
            {
                Term trm = new Term(pg.Id.ToString(), "Id");
                writer.DeleteDocuments(trm);
            }

            writer.AddDocument(document);

        } catch (Exception exIndx)
        {
            sbLog.AppendLine("exIndx->" + exIndx);
        }
        
    }
}

So the problem here is search functionality has to apply search on multiple .cfs files but its not happening. below is the search code
C#
static List<Page> Search(string keywords, int limit, out int count)
{
    using (var directory = GetDirectory())
    using (var searcher = new IndexSearcher(directory))
    {
        var query = GetQuery(keywords);
        var sort = GetSort();
        var filter = GetFilter();

        var docs = searcher.Search(query, filter, limit, sort);
        count = docs.TotalHits;

        var products = new List<Page>();
        foreach (var scoreDoc in docs.ScoreDocs)
        {
            var doc = searcher.Doc(scoreDoc.Doc);
            var product = new Page
            {
                Id = int.Parse(doc.Get("Id")),
                name = doc.Get("url").Substring(doc.Get("url").LastIndexOf("/") + 1, doc.Get("url").Length - doc.Get("url").LastIndexOf("/") - 1),
                data = doc.Get("data"),
                url = doc.Get("url")
            };

            if (!products.Any(x => x.Id == product.Id)) 
            products.Add(product);
        }

        return products;
    }
}


What I have tried:

Unable to apply searching on multiple .cfs files as each .cfs file has different set of keywords to search. Please help me with this. Thanks
Posted
Updated 26-Jul-20 0:10am
v2

It's not exactely obvious what's going on .. I would change
using (var directory = GetDirectory())
to
using (var directory = GetDirectory(@"C:\Some-Directory\Indexes", true))
in the index creator, ensure indexes ARE being generated, then when you know the indexes are being created, use that same directory eg
using (var directory = GetDirectory(@"C:\Some-Directory\Indexes"))
in the 2nd piece of code that uses the indexes

There was one example I saw here Create and retrieve informations from an index with Lucene.NET – Tier-1[^] that may help
 
Share this answer
 
It doesn't work for my case. My requirement is today i may create index for www.sample.com/page1.aspx, www.sample.com/page2.aspx pages. Tomorrow i may create index for www.sample.com/page3.aspx. If I search the index then the search should apply on all the 3 pages.

In this case when I create Index on daily basis with new page urls, its creating cfs files like _0.cfs, _1.cfs, _2.cfs so on and also segment file are updating.

I got struck here. Could anyone please guide me.
 
Share this answer
 
Comments
Member 13517891 26-Jul-20 6:56am    
It doesn't work for my case. My requirement is today i may create index for www.sample.com/page1.aspx, www.sample.com/page2.aspx pages. Tomorrow i may create index for www.sample.com/page3.aspx. If I search the index then the search should apply on all the 3 pages.

In this case when I create Index on daily basis with new page urls, its creating cfs files like _0.cfs, _1.cfs, _2.cfs so on and also segment file are updating.

I got struck here. Could anyone please guide me.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900