lucene-java-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Mohammad Hasan" <>
Subject GradientFormatter Exception during runtime
Date Wed, 07 May 2008 11:56:05 GMT
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.


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


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

//If only Name is mentioned then nothing will be searched in Author



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",

  //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

        String highlightText = highlighter.GetBestFragments(tokenStream,
text, 2, "...");



//_results = fullTextSession.CreateFullTextQuery<Book>(query).List<Book>();

return highlightedText;









  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message