Adding relevant excerpts with highlighted search terms to your search results display makes it much easier for end users to scan the page and assess which hits look promising, dramatically improving their search experience.
Highlighter uses information generated at index time. To save resources, highlighting is disabled by default and must be turned on for individual fields.
{
String *field_str = Str_newf("content");
FullTextType *type = FullTextType_new((Analyzer*)analyzer);
FullTextType_Set_Highlightable(type, true);
Schema_Spec_Field(schema, field_str, (FieldType*)type);
DECREF(type);
DECREF(field_str);
}
To add highlighting and excerpting to the search.cgi sample app, create a
$highlighter
object outside the hits iterating loop…
String *content_str = Str_newf("content");
Highlighter *highlighter
= Highlighter_new((Searcher*)searcher, (Obj*)query,
content_str, 200);
… then modify the loop and the per-hit display to generate and include the excerpt.
String *title_str = Str_newf("title");
String *url_str = Str_newf("url");
HitDoc *hit;
i = 1;
// Loop over search results.
while (NULL != (hit = Hits_Next(hits))) {
String *title = (String*)HitDoc_Extract(hit, title_str);
char *title_c = Str_To_Utf8(title);
String *url = (String*)HitDoc_Extract(hit, url_str);
char *url_c = Str_To_Utf8(url);
String *excerpt = Highlighter_Create_Excerpt(highlighter, hit);
char *excerpt_c = Str_To_Utf8(excerpt);
printf("Result %d: %s (%s)\n%s\n\n", i, title_c, url_c, excerpt_c);
free(excerpt_c);
free(url_c);
free(title_c);
DECREF(excerpt);
DECREF(url);
DECREF(title);
DECREF(hit);
i++;
}
DECREF(url_str);
DECREF(title_str);
DECREF(hits);
DECREF(query_str);
DECREF(highlighter);
DECREF(content_str);
DECREF(searcher);
DECREF(folder);
Our next tutorial chapter, QueryObjectsTutorial, illustrates how to build an “advanced search” interface using Query objects instead of query strings.
Copyright © 2010-2015 The Apache Software Foundation, Licensed under the
Apache License, Version 2.0.
Apache Lucy, Lucy, Apache, the Apache feather logo, and the Apache Lucy project logo are trademarks of The
Apache Software Foundation. All other marks mentioned may be trademarks or registered trademarks of their
respective owners.