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

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Google Cloud Document AI Basics
  • Thumbnail Generator Microservice for PDF in Spring Boot
  • Build a Local AI-Powered Document Summarization Tool
  • Designing a Blog Application Using Document Databases

Trending

  • A Guide to Container Runtimes
  • Solid Testing Strategies for Salesforce Releases
  • Docker Model Runner: Streamlining AI Deployment for Developers
  • Internal Developer Portals: Modern DevOps's Missing Piece

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.3K 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, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Google Cloud Document AI Basics
  • Thumbnail Generator Microservice for PDF in Spring Boot
  • Build a Local AI-Powered Document Summarization Tool
  • Designing a Blog Application Using Document Databases

Partner Resources

×

Comments

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

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: