2010-02-13 169 views
0

這裏是我的代碼Lucene的返回完全相同的搜索結果,無論搜索詞

term = Server.UrlDecode(term); 
     string indexFileLocation = "C:\\lucene\\Index\\post"; 
     Lucene.Net.Store.Directory dir = 
      Lucene.Net.Store.FSDirectory.GetDirectory(indexFileLocation, false); 


     //create an index searcher that will perform the search 
     Lucene.Net.Search.IndexSearcher searcher = new 
     Lucene.Net.Search.IndexSearcher(dir); 

     //build a query object 
     Lucene.Net.Index.Term searchTerm = 
      new Lucene.Net.Index.Term("post_title", term); 

     Lucene.Net.Analysis.Standard.StandardAnalyzer analyzer = new Lucene.Net.Analysis.Standard.StandardAnalyzer(); 

     Lucene.Net.QueryParsers.QueryParser queryParser = new 
Lucene.Net.QueryParsers.QueryParser("post_title", analyzer); 

     Lucene.Net.Search.Query query = queryParser.Parse(term); 



     //execute the query 
     Lucene.Net.Search.Hits hits = searcher.Search(query); 

     List<string> s = new List<string>(); 

     for (int i = 0; i < hits.Length(); i++) 
     { 
      Lucene.Net.Documents.Document doc = hits.Doc(i); 
      s.Add(doc.Get("post_title_raw")); 
     } 

     ViewData["s"] = s; 

這裏是我的索引代碼

 //create post lucene index 
     LuceneType lt = new LuceneType(); 
     lt.Analyzer = new Lucene.Net.Analysis.Standard.StandardAnalyzer(); 
     lt.Writer = new Lucene.Net.Index.IndexWriter("C:/lucene/Index/post", lt.Analyzer, true); 

     using (var context = new MvcApplication1.Entity.test2Entities()) 
     { 
      var posts = from p in context.post 
         where object.Equals(p.post_parentid, null) && p.post_isdeleted == false 
         let Answers = from a in context.post 
             where a.post_parentid == p.post_id 
             select new 
             { 
              a.post_description 
             } 
         let Comments = from c in context.comment 
             where c.post.post_id == p.post_id 
             select new 
             { 
              c.comment_text 
             } 
         select new 
         { 
          p, 
          Answers, 
          Comments 
         }; 


      foreach (var post in posts) 
      { 
       //lets concate all the answers and comments 
       StringBuilder answersSB = new StringBuilder(); 
       StringBuilder CommentsSB = new StringBuilder(); 
       foreach (var answer in post.Answers) 
        answersSB.Append(answer.post_description); 
       foreach (var comment in post.Comments) 
        CommentsSB.Append(comment.comment_text); 



       //add rows 
       lt.Doc.Add(new Lucene.Net.Documents.Field(
        "post_id", 
        post.p.post_id.ToString(), 
        Lucene.Net.Documents.Field.Store.YES, 
        Lucene.Net.Documents.Field.Index.UN_TOKENIZED 
        )); 

       lt.Doc.Add(new Lucene.Net.Documents.Field(
        "post_title", 
        new System.IO.StringReader(post.p.post_title))); 

       lt.Doc.Add(new Lucene.Net.Documents.Field(
        "post_title_raw", 
        post.p.post_title, 
        Lucene.Net.Documents.Field.Store.YES, 
        Lucene.Net.Documents.Field.Index.UN_TOKENIZED)); 

       lt.Doc.Add(new Lucene.Net.Documents.Field(
        "post_titleslug", 
        post.p.post_titleslug, 
        Lucene.Net.Documents.Field.Store.YES, 
        Lucene.Net.Documents.Field.Index.UN_TOKENIZED)); 

       lt.Doc.Add(new Lucene.Net.Documents.Field(
        "post_tagtext", 
        new System.IO.StringReader(post.p.post_tagtext))); 

       lt.Doc.Add(new Lucene.Net.Documents.Field(
        "post_tagtext", 
        post.p.post_tagtext, 
        Lucene.Net.Documents.Field.Store.YES, 
        Lucene.Net.Documents.Field.Index.UN_TOKENIZED)); 

       lt.Doc.Add(new Lucene.Net.Documents.Field(
        "post_description", 
        new System.IO.StringReader(post.p.post_description))); 

       lt.Doc.Add(new Lucene.Net.Documents.Field(
        "post_description_raw", 
        post.p.post_description, 
        Lucene.Net.Documents.Field.Store.YES, 
        Lucene.Net.Documents.Field.Index.UN_TOKENIZED)); 


       lt.Doc.Add(new Lucene.Net.Documents.Field(
        "post_Answers", 
        new System.IO.StringReader(answersSB.ToString()))); 


       lt.Doc.Add(new Lucene.Net.Documents.Field(
        "post_Comments", 
        new System.IO.StringReader(CommentsSB.ToString()))); 

      } 
      lt.Writer.AddDocument(lt.Doc); 
      lt.Writer.Optimize(); 
      lt.Writer.Close(); 

爲什麼會發生這種返回任何搜索詞相同reuslts?

+0

您的搜索結果是否未經過篩選或是否被特定字詞過濾了,無論您發送了什麼內容? – nortron 2010-02-13 09:26:20

+0

你是否已經通過執行並驗證了'term'變量實際包含的內容? – Aaronaught 2010-02-23 02:42:05

+0

你可以發佈不同搜索query.toString()的結果嗎? – 2010-02-19 09:47:15

回答

0

Lucene.Net.Search.Query query = queryParser.Parse(term);

在上面的代碼,而不是搜索關鍵詞您已經使用術語

您的代碼必須如下面

Lucene.Net.Search.Query查詢= queryParser.Parse(搜索關鍵詞);

+0

我把searchterm變量放在方法中,但intellisense抱怨 - 它只會採取一個字符串 – Luke101 2010-02-16 02:58:57

0

你可以做一些小改動像下面

//創建一個查詢對象 Lucene.Net.Index.Term SEARCHTERM = 新Lucene.Net.Index.Term( 「POST_TITLE」,期限);

TermQuery tq = new TermQuery(searchTerm);

......

......

Lucene.Net.Search.Query查詢= TQ;

現在不需要Parser了。

IF仍然u需要解析器則可以更改上面的行作爲

Lucene.Net.Search.Query查詢= queryParser.Parse(tq.ToString());

希望這會有所幫助。

+0

嗯..這仍然沒有工作..我已經更新了我原來的帖子中的代碼。我也發佈了索引代碼。 – Luke101 2010-02-16 05:44:35

0

不是直接的答案,而是得到LUKE(它也適用於.NET索引)並打開您的索引 - 嘗試使用正確類型的優化器來查詢它。如果有效,你知道問題在於你的查詢。如果不是,它可能在索引和查詢中,但至少這應該讓你走上正確的軌道。

+0

我的意思是分析儀不優化--- – Matt 2010-02-23 02:41:08