DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Retrieval Augmented Generation With Spring AI 2.0, Claude, and PGvector
  • Building High‑Precision Vector Search for Document Retrieval on Databricks
  • How to Detect Spam Content in Documents Using C#
  • Responsible AI Is an Engineering Problem, not a Policy Document

Trending

  • Rethinking Java Design Patterns: From OOP to FP
  • Agentic RAG: Basic RAG Plus MCP Tool Calls
  • How to Design a Distributed Job Scheduler
  • Understanding Agentic SDLC: The Future of Software Engineering

Highlight Matched Text in Solr + Tika Indexed Documents

By 
Ricci Gian Maria user avatar
Ricci Gian Maria
·
Jun. 10, 13 · Tutorial
Likes (0)
Comment
Save
Tweet
Share
14.6K Views

Join the DZone community and get the full member experience.

Join For Free

i’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.

http://localhost:8080/testinstance/tikacrawl/select?q=text%3abranch&fl=id%2ctitle%2cauthor&wt=xml&hl=true&hl.snippets=20&hl.fl=content&hl.usephrasehighlighter=true

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

image

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

http://localhost:8080/testinstance/tikacrawl/select?q=text%3a% 22branch+strategy%22~3 &fl=id%2ctitle&wt=xml&hl=true&hl.snippets=5&hl.fl=content&hl.usephrasehighlighter=true&hl.fragsize=300

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.

image

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

Document

Published at DZone with permission of Ricci Gian Maria. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Retrieval Augmented Generation With Spring AI 2.0, Claude, and PGvector
  • Building High‑Precision Vector Search for Document Retrieval on Databricks
  • How to Detect Spam Content in Documents Using C#
  • Responsible AI Is an Engineering Problem, not a Policy Document

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook