如何使用lucene.net搜索文件内容? [英] How do I use lucene.net for searching file content?

查看:122
本文介绍了如何使用lucene.net搜索文件内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前正在使用lucene.net来搜索文件内容以进行关键字搜索.我能够正确获得结果,但是有一种情况需要显示在特定文件中找到的关键字.

I am currently using lucene.net to search the content of files for keyword search. I am able to get the results correctly but I have a scenario where I need to display the keywords found in a particular file.

有两个不同的文件,分别包含"karthik"和"steven",如果我搜索"karthik and steven",则可以同时显示两个文件.如果仅分别搜索"karthik"和"steven",则仅显示相应的文件.

There are two different files containing "karthik" and "steven", and if I search for "karthik and steven" I am able to get both the files displayed. If I search only for "karthik" and "steven" separately, only the respective files are getting displayed.

当我同时搜索"karthik and steven"时,由于我仅显示文件名,因此在结果中同时获得了两个文件,现在我需要在该特定文件中找到的特定关键字显示为列表视图中的记录.

When I search for "karthik and steven" simultaneously I get both the files in the result as I am displaying the filename alone, and now I need to display the particular keyword found in that particular file as a record in the listview.

   Public bool StartSearch()
    {
        bool bResult = false;
        Searcher objSearcher = new IndexSearcher(mstrIndexLocation);
        Analyzer objAnalyzer = new StandardAnalyzer();

        try
        {
            //Perform Search
            DateTime dteStart = DateTime.Now;
            Query objQuery = QueryParser.Parse(mstrSearchFor, "contents", objAnalyzer);
            Hits objHits = objSearcher.Search(objQuery, objFilter);
            DateTime dteEnd = DateTime.Now;
            mlngTotalTime = (Date.GetTime(dteEnd) - Date.GetTime(dteStart));
            mlngNumHitsFound = objHits.Length();
            //GeneratePreviewText(objQuery, mstrSearchFor,objHits);
            //Generate results - convert to XML
            mstrResultsXML = "";
            if (mlngNumHitsFound > 0)
            {
                mstrResultsXML = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?><Results>";
                //Loop through results
                for (int i = 0; i < objHits.Length(); i++)
                {
                    try
                    {
                        //Get the next result
                        Document objDocument = objHits.Doc(i);
                        //Extract the data
                        string strPath = objDocument.Get("path");
                        string strFileName = objDocument.Get("name");
                        if (strPath == null) { strPath = ""; }
                        string strLastWrite = objDocument.Get("last_write_time");
                        if (strLastWrite == null)
                            strLastWrite = "unavailable";
                        else
                        {
                            strLastWrite = DateField.StringToDate(strLastWrite).ToShortDateString();
                        }
                        double dblScore = objHits.Score(i) * 100;
                        string strScore = String.Format("{0:00.00}", dblScore);
                        //Add results as an XML row
                        mstrResultsXML += "<Row>";
                        //mstrResultsXML += "<Sequence>" + (i + 1).ToString() + "</Sequence>";
                        mstrResultsXML += "<Path>" + strPath + "</Path>";
                        mstrResultsXML += "<FileName>" + strFileName + "</FileName>";
                        //mstrResultsXML += "<Score>" + strScore + "%" + "</Score>";
                        mstrResultsXML += "</Row>";
                    }
                    catch
                    {
                        break;
                    }
                }
                //Finish off XML
                mstrResultsXML += "</Results>";
                //Build Dataview (to bind to datagrid
                DataSet objDS = new DataSet();
                StringReader objSR = new StringReader(mstrResultsXML);
                objDS.ReadXml(objSR);
                objSR = null;
                mobjResultsDataView = new DataView();
                mobjResultsDataView = objDS.Tables[0].DefaultView;
            }
            //Finish up
            objSearcher.Close();
            bResult = true;
        }
        catch (Exception e)
        {
            mstrError = "Exception: " + e.Message;
        }
        finally
        {
            objSearcher = null;
            objAnalyzer = null;
        }
        return bResult;
    }

上面是我用于搜索的代码以及我绑定到listview的xml,现在我需要标记在相应文档中找到的特定关键字,并将其作为记录显示在listview中,类似于下面的listview

Above is the code i am using for search and the xml i am binding to the listview, now i need to tag the particular keywords found in the respective document and display it in the listview as recordsss,simlar to the below listview

未找到文件名关键字

1个Test.Doc karthik

1 Test.Doc karthik

2 Test2.Doc史蒂文

2 Test2.Doc steven

我希望你们能理解这个问题,

i hope u guys undesrtood the question,

推荐答案

这取决于文档的索引方式.您需要提取原始内容,将其通过分析器以获取索引的标记,然后检查与生成的查询匹配的内容.

This depends on how your documents were indexed. You'll need to extract the original content, pass it through the analyzer to get the indexed tokens, and check which matches the generated query.

只需使用 Highlighter.Net 包,是contrib的一部分,它可以完成更多操作.

Just go with the Highlighter.Net package, part of contrib, which does this and more.

这篇关于如何使用lucene.net搜索文件内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆