This project is read-only.

Errors in examples?

Feb 10, 2012 at 6:06 PM

Hi

I noticed some error (at least I think they are). I am using Lucene.Net(version 2.9.2.2) and SimpleLucene(1.0.0.0). As I mentioned the nuget package does not work so I had to get it from the source code(Lucene.Net also does not seem to be the most current version as well)

http://simplelucene.codeplex.com/wikipage?title=Seaching%20an%20index&referringTitle=Documentation

 


        var indexSearcher = new DirectoryIndexSearcher(new DirectoryInfo(indexPath), true);
            using (var searchService = new SearchService(indexSearcher)) {   
                var result = searchService.SearchIndex(new TermQuery(new Term("id", "1")));
                var luceneDocs = result.Results;
                Console.Write(luceneDocs.First().GetValue("name"));
                Console.ReadLine();
            }


There seems to be no "GetValue("name")" I had to do "GetValues("name").First() to get back the result that I needed.

Also I think the id's are wrong

    public class ProductRepository {
        public static IList<Product> GetProducts() {
            return new List<Product> {
                new Product { Id = 1, Name = "Football", Price = 12.99M, CreatedOn = new DateTime(2011, 1, 1)},
                new Product { Id = 1, Name = "Trainers", Price = 34.99M, CreatedOn = new DateTime(2011, 2, 15)},
                new Product { Id = 1, Name = "Laptop", Price = 499.50M, CreatedOn = new DateTime(2010, 12, 28)},
                new Product { Id = 1, Name = "DVD", Price = 11.00M, CreatedOn = new DateTime(2010, 11, 5)},
                new Product { Id = 1, Name = "Mobile Phone", Price = 134.45M, CreatedOn = new DateTime(2010, 9, 18)}
            };
        }
 Notice how they all have an Id of 1. So if you do a search on a id of one all of them will come back.
Feb 13, 2012 at 1:35 PM

I've corrected the Id values in the example code. The GetValue([field name]) is an extension method in the SimpleLucene namespace.

Feb 13, 2012 at 4:54 PM
retroviz wrote:

I've corrected the Id values in the example code. The GetValue([field name]) is an extension method in the SimpleLucene namespace.

Oh. I guess VS 2010 did not realize that as it did not ask if I wanted to import the namespace in. I found another possible error

 

        public ProductQuery WithKeywords(string keywords)
        {
            if (!string.IsNullOrEmpty(keywords))
            {
                string[] fields = { "name", "description" };
                var parser = new MultiFieldQueryParser(Version.LUCENE_29,
                    fields, new StandardAnalyzer(Version.LUCENE_29));
                Query multiQuery = parser.Parse(keywords);

                this.AddQuery(multiQuery);
            }
            return this;
        }

http://simplelucene.codeplex.com/wikipage?title=Creating%20Query%20Objects&referringTitle=Documentation

should "description" be there? I don't see any field with description in it.
Feb 14, 2012 at 11:55 AM

You're right, I neglected to include a Description property on the Product class. Either way, this code would still run and demonstrates how you can query multiple fields.