tags:

views:

437

answers:

5

Here is my code

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;

here is my indexing code

        //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();

why does this return the same reuslts for any search term?

A: 

Have you used same analyzer (StandardAnalyzer), that you are using in searching, when you indexed data?

Juha Syrjälä
yes..I am using the same analyzer. I am not sure what Cryo means in the post comment. Maybe you can explain
Luke101
A: 

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

In the code above instead of searchterm you have used term

Your code must be like below

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

Ranga
I put the searchterm variable in the method but intellisense complained - it will only take a string
Luke101
A: 

Hi , You can make some small alteration as like below

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

TermQuery tq = new TermQuery(searchTerm);

......

......

Lucene.Net.Search.Query query = tq;

Now there is no need of Parser.

IF still u need parser then you can change the above line as

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

Hope this helps.

Ranga
Hmmmm..this still did not work..I have updated my code in the original post. I posted the indexing code as well.
Luke101
A: 

Could you post the results of query.toString() for different searches?

Christian Severin
A: 

Not a direct answer, but get LUKE (It works with .NET indexes too) and open your index -- Try to use it's querier using the right type of optimizer. If that works, you know the problem is in your querying. If it doesn't it could be in both the indexing and the querying, but at least this ought to get you on the right track.

Matt
I meant analyzer not optimizer ---
Matt