2 hong34210 hong34210 于 2016.05.06 14:08 提问

关于lucene.net站内搜索的简单问题

我做了一个简单的站内搜索,但是创建索引后无法搜索到数据,不知道为什么

   #region 创建索引
    public void createIndex()
    {
        SqlConnection conn = new SqlConnection("server=localhost;database=news;uid=sa;pwd=intellitrans");
        FSDirectory directory;
        IndexWriter writer;
        string indexPath = Context.Server.MapPath("~/IndexData");
        System.IO.Directory.Delete(indexPath, true);
        directory = FSDirectory.Open(new DirectoryInfo(indexPath), new NativeFSLockFactory());
        writer = new IndexWriter(directory, new PanGuAnalyzer(), true, IndexWriter.MaxFieldLength.UNLIMITED);
        try
        {
            conn.Open();
            string sql = "select newsid,title,url,time,newsContent from news";
            SqlCommand comm = new SqlCommand(sql, conn);
            SqlDataReader dataReader = comm.ExecuteReader();
            while (dataReader.Read())
            {
                Document document = new Document();
                document.Add(new Field("newsid", dataReader[0].ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED));
                document.Add(new Field("title", dataReader[0].ToString(), Field.Store.YES, Field.Index.ANALYZED));
                document.Add(new Field("url", dataReader[1].ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS));
                document.Add(new Field("time", dataReader[2].ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS));
                document.Add(new Field("newsContent", dataReader[3].ToString(), Field.Store.YES, Field.Index.ANALYZED));
                writer.AddDocument(document);
            }
        }
        catch (Exception ex)
        {
            this.Label1.Text = "创建索引失败" + ex.Message;
        }
        finally
        {
            conn.Close();
            writer.Close();
            directory.Close();
        }
    }
    #endregion

    #region 搜索
    private void SearchIndex()
    {
        string indexPath = Context.Server.MapPath("~/IndexData");
        FSDirectory directory = FSDirectory.Open(new DirectoryInfo(indexPath), new NoLockFactory());
        IndexReader reader = IndexReader.Open(directory, true);
        IndexSearcher searcher = new IndexSearcher(reader);
        // PhraseQuery query = new PhraseQuery();
        TermQuery query = null;
        BooleanQuery queryOr = new BooleanQuery();
        foreach (string word in SplitWords(this.TextBox1.Text))
        {
            // query.Add(new Term("title", word));
            query = new TermQuery(new Term("title", word));
            queryOr.Add(query, BooleanClause.Occur.SHOULD);
            query = new TermQuery(new Term("newsContent", word));
            queryOr.Add(query, BooleanClause.Occur.SHOULD);
            //query.Add(new Term("newsContent", word));
        }
        TopScoreDocCollector collector = TopScoreDocCollector.create(1000, true);
        searcher.Search(query, null, collector);
        //Hits hit = searcher.Search(query);
        ScoreDoc[] docs = collector.TopDocs(0, collector.GetTotalHits()).scoreDocs;
        List<News> newResult = new List<News>();
        for (int i = 0; i < docs.Length; i++)
        {
            int docId = docs[i].doc;
            Document doc = searcher.Doc(docId);
            News news = new News();
            news.Title = doc.Get("title");
            news.ContentDescription = HightLight(this.TextBox1.Text, doc.Get("newsContent"));
            newResult.Add(news);
        }
        this.rptSearch.DataSource = newResult;
        this.rptSearch.DataBind();
    }
        #endregion

2个回答

caozhy
caozhy   Ds   Rxr 2016.05.07 06:42
已采纳

是中文么,中文需要分词。
参考:http://www.cnblogs.com/lhj588/archive/2013/02/06/2900937.html

hong34210
hong34210 已经解决了,不过还是谢谢你!
一年多之前 回复
CSDNXIAOD
CSDNXIAOD   2016.05.06 14:12

Lucene.net站内搜索3—最简单搜索引擎代码
----------------------biu~biu~biu~~~在下问答机器人小D,这是我依靠自己的聪明才智给出的答案,如果不正确,你来咬我啊!

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!