How do I use lucene.net for searching file content?

假装没事ソ 提交于 2019-12-24 08:46:32

问题


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.

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.

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;
    }

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

No FileName KeyWord(s)Found

1 Test.Doc karthik

2 Test2.Doc steven

i hope u guys undesrtood the question,


回答1:


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.

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



来源:https://stackoverflow.com/questions/8667471/how-do-i-use-lucene-net-for-searching-file-content

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!