Search code examples
.netlucene.netrelevance

Bringing Relevant results to the top using boost in Lucene.NET


We have a number of products that have the word "ipad" and these include iPad tables and the iPad accessories. When we search using Lucene by the word "ipad", all the accessories show up at the top and the iPad shows up at 25th page. We want to bring the iPad tablets to the first page. We are just indexing the name of the products and we set the boost 3 to the name of the ipad tablets while indexing. Still we are not getting iPad to the first page. Following are some of these products. Any help is highly appreciated.

  1. Apple iPad (First Generation) MC496LL/A Tablet (32GB, Wifi + 3G)
  2. Apple iPad 2 MC769LL/A Tablet (16GB, Wifi, Black) NEWEST MODEL
  3. Apple Ipad2 (Ipad 2) tablet 64GB Wifi Only black
  4. Apple iPad 2 MC764LL/A Tablet (64GB, Wifi + Verizon 3G, Black) NEWEST MODEL
  5. iHome iPad Charging Station
  6. Marware Eco-Vue for iPad
  7. iLuv Neoprene Sleeve for iPad

Solution

  • I think you are boosting by field what you need to do boost the whole document if it is the ipad or what I would do is negative boost the accessories.

    Document doc = new Document();
    doc.Add(new Field("name", name, Field.Store.YES, Field.Index.ANALYZED));   
    doc.Add(new Field("category", category, Field.Store.YES, Field.Index.ANALYZED)); 
    doc.Add(new Field("isaccesory", isaccesory.ToString(), Field.Store.NO, Field.Index.NOT_ANALYZED));  
    
    if (isaccesory)
        doc.SetBoost(0.5); // Give all accessories lower relevance
    
    writer.AddDocument(doc);
    

    You will have to play with the boost values if you still get accessories appearing on top it's probably because the description have a lot of words with ipad in them so lucene is considering it more relevant.

    One trick you can try is to use another field which stores weather it's an accessory which i'm doing in the example above and include that as an OR in your search eg.

    var booleanQuery = new BooleanQuery();
    var keywordQuery = new QueryParser().Parse(keyword); // keyword = "ipad"
    var isAccesoryQuery = new TermQuery(new Term("isaccesory", "False"));
    booleanQuery.Add(keywordQuery, BooleanClause.Occur.MUST); 
    booleanQuery.Add(isAccesoryQuery, BooleanClause.Occur.SHOULD);
    
    vat hits = searcher.Search(booleanQuery);
    

    The extra condition will bump anything with isaccesory: False higher in relevency.