Highlight Matched Text in Solr + Tika Indexed Documents
Join the DZone community and get the full member experience.
Join For Freei’ve already dealt on how to index documents with solr and tika and in this article i’ll explain how you can not only search for documents that match your query, but returns even some text extract that shows where the document match the query . to achieve this, you should store the full content of the document inside your index, usually i create a couple of fields, one called content that will contain the content of the file, and with a copyfield directive ( <copyfield source=”content” dest=”text”/> ) automatically copy that value inside the catch all field called text.
<field name="content" type="text_general" indexed="false" stored="true" multivalued="false"/>
<field name="text" type="text_general" indexed="true" stored="false" multivalued="true"/>
text field is multivalued and not stored, it is only indexed to permit search inside various field of the document . content field store the extracted text from tikaand it is useful both for highlighting and to troubleshoot extraction problems, because it contains the exact text extracted by tika.
now suppose you want to search for the term branch and want also to highlight the part of the text where you find that term , you can simply issue a query that ask for highlighting, it is really simple.
this simple query ask for document with text that contains word branch, i want to extract (fl=) only title and author fields, want xml format and with hl=true i’m asking for snippet of matching text, hl.snippets=20 instruct solr to search for a maximum of 20 snippet and hl.usephrasehighlighter=true use a specific highlighter that try to extract a single phrase from the text. the most important parameter is the hl.fl=content that specify the field of the document that contains the text used for highlight. in the results, after all matching documents there is a new section that contains all the highlights for each document
figure 1: hilight for the tfs branching guid – scenarios 2.0.pdf file
the name of the element match the id of the document (in my configuration full path of the file), and there a list of highlights that follows. but the true power of solr comes out if you start to use languages specific fields .
<field name="content" type="text_en" indexed="false" stored="true" multivalued="false"/>
<field name="text" type="text_en" indexed="true" stored="false" multivalued="true"/>
i’ve just changed in schema.xml the type of content and text from general_text to text_en and this simple modification enables a more specific tokenizer, capable of doing full-text searches. suppose you want to know all documents that deal with branching strategies, here is a possible query
the key is in the search query text:”branch strategy”~3 that states i’m interested in documents containing both branch and strategy terms and with a relative distance of no more than three words. since text was indexed with text_en field type i got full-text search, and i have a confirmation looking at the highlights.
figure 2: highlights for a proximity query with full text, as you can see word branching matches even if i searched for branch
and voilà!! you have full-text searching inside file content with minimal amount of work and a simple rest interface for querying the index
Published at DZone with permission of Ricci Gian Maria, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments