搜索功能

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Lucene.Net.Index;
using Lucene.Net.Documents;
using Lucene.Net.Search;
using Lucene.Net.Analysis;
using Lucene.Net.Store;
using System.Web.UI.WebControls;
using Lucene.Net.QueryParsers;
using Lucene.Net.Analysis.Standard;

public class LuceneTest
{
    //private const string FieldName = "name";
    private const string FieldValue = "value";

    private Directory directory = new RAMDirectory();
    private Analyzer analyzer = new StandardAnalyzer();

    public LuceneTest(Repeater r, string FieldName)
    {
        Index(FieldName);
        Search(r, FieldName);
    }
    private List<SeoResult> GetList(string name)
    {
        List<SeoResult> list = new List<SeoResult>();
        for (int i = 1; i <= 100; i++)
        {
            list.Add(new SeoResult()
            {
                docId = i,
                name = "name" + i,
                value = "Hello, World!" + i,
                score = 0.4912025f
            });
        }
        var NewList = from l in list where l.name.Contains(name) select l;
        return NewList.ToList();
    }
    private void Index(string FieldName)
    {
        IndexWriter writer = new IndexWriter(directory, analyzer, true);
        writer.maxFieldLength = 1000;
        List<SeoResult> list = GetList(FieldName);
        for (int i = 0; i < list.Count; i++)
        {
            Document document = new Document();

            document.Add(new Field(FieldName, list[i].name, Field.Store.YES, Field.Index.UN_TOKENIZED));
            document.Add(new Field(FieldValue, list[i].value, Field.Store.YES, Field.Index.TOKENIZED));

            writer.AddDocument(document);
        }

        writer.Optimize();
        writer.Close();
    }

    private void Search(Repeater r, string FieldName)
    {
        Query query = QueryParser.Parse("name*", FieldName, analyzer);

        IndexSearcher searcher = new IndexSearcher(directory);
        List<SeoResult> list = new List<SeoResult>();
        Hits hits = searcher.Search(query);

        for (int i = 0; i < hits.Length(); i++)
        {
            int docId = hits.Id(i);
            string name = hits.Doc(i).Get(FieldName);
            string value = hits.Doc(i).Get(FieldValue);
            float score = hits.Score(i);
            list.Add(new SeoResult()
            {
                docId = docId,
                name = name,
                value = value,
                score = score
            });

        }
        r.DataSource = list;
        r.DataBind();
        searcher.Close();
    }
}
public class SeoResult
{
    public int docId { get; set; }
    public string name { get; set; }
    public string value { get; set; }
    public float score { get; set; }
}

页面:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <br />
        <asp:Button ID="Button1" runat="server" Text="搜索" OnClick="Button1_Click" />
        <br />
    </div>
    <table>
        <asp:Repeater ID="Repeater1" runat="server">
            <ItemTemplate>
                <tr>
                    <td>
                        <%#Eval("docId")%>
                    </td>
                    <td>
                        <%#Eval("name")%>
                    </td>
                    <td>
                        <%#Eval("value")%>
                    </td>
                    <td>
                        <%#Eval("score")%>
                    </td>
                </tr>
            </ItemTemplate>
        </asp:Repeater>
    </table>
    </form>
</body>
</html>

后台:

 protected void Button1_Click(object sender, EventArgs e)
        {
            new LuceneTest(this.Repeater1,TextBox1.Text);
        }

需要的dll文件下载地址:http://url.cn/IuuCag

posted @ 2013-11-14 18:02  Lukas89  阅读(140)  评论(0编辑  收藏  举报