Hi Mark & All,
I am trying to implement NHibernate Search with Lucene.Net Highlight in my project. All I am trying to do is finding the item that stored in a database. I am successful in that, but when I try to highlight the result with GradientFormatter, I run into trouble. I get FormatException error which says, "One of the identified items was in an invalid format". I am using Visual Studio 2005 to develop the ASP.NET application. The program compiles correctly, but during run time it throws exception and tells one of the arguments in the GradientFormatter constructor is wrong which I do not understand since I think all the arguments are in correct format. I put the code below. If you, Mark or Any body can help me in this regards that will be really great. Thanks a lot. Hasan public IList searchBook(string searchItem) { protected IList highlightedText = null; Analyzer analyzer = new SimpleAnalyzer(); ISession session = factory.OpenSession(new SearchInterceptor()); IFullTextSession fullTextSession = Search.CreateFullTextSession(session); IndexReader indexReader = IndexReader.Open(SearchFactory .GetSearchFactory(session) .GetDirectoryProvider(typeof(Book)).Directory); IndexSearcher searcher = new IndexSearcher(indexReader); MultiFieldQueryParser parser = new MultiFieldQueryParser(new string[] { "Name", "Author"}, analyzer); Query queryObj; using (ITransaction transaction = session.BeginTransaction()) { List<string> queryParts = new List<string>(); string[] searchWords = searchItem.Split(new char[] { ' ' }); foreach (string searchWord in searchWords) { queryParts.Add("Name:" + searchWord.Trim() + "*"); queryParts.Add("Author:" + searchWord.Trim() + "*"); } //queryParts are probably ArrayList. ArrayList al = new ArrayList(); //Basically above Name & Author are the fields where the search sould be performed //If only Name is mentioned then nothing will be searched in Author try { string query = string.Join(" or ", queryParts.ToArray()); queryObj = parser.Parse(query); //IQuery nhQuery = fullTextSession.CreateFullTextQuery(queryObj, new Type[] { typeof(Book) }); //IList<Book> books = nhQuery.List<Book>(); //QueryScorer fragScorer = new QueryScorer(queryObj, indexReader, "Name"); QueryScorer fragScorer = new QueryScorer(queryObj.Rewrite(indexReader)); float topScore = fragScorer.GetMaxTermWeight(); //Create a gradient formatter with appropriate color ranges GradientFormatter formatter = new GradientFormatter(topScore, "#888888", "#ff8888","#ffffff","#ffffff"); //create the highlighter Highlighter highlighter=new Highlighter(formatter,fragScorer); //run the query and highlight the results - graded by color to reflect value of match Hits hits = searcher.Search(queryObj); //int numToShow=Math.min(10,hits.length()); for (int i = 0; i < hits.Length(); i++) { String text=hits.Doc(i).Get("Name"); TokenStream tokenStream = analyzer.TokenStream("Name", new StringReader(text)); String highlightText = highlighter.GetBestFragments(tokenStream, text, 2, "..."); highlightedText.Add(highlightText); } //_results = fullTextSession.CreateFullTextQuery<Book>(query).List<Book>(); return highlightedText; } finally { fullTextSession.Close(); } //transaction.Commit(); } }