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 Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
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

Highlight Matched Text in Solr + Tika Indexed Documents

Ricci Gian Maria user avatar by
Ricci Gian Maria
·
Jun. 10, 13 · Tutorial
Like (0)
Save
Tweet
Share
13.76K 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.

Popular on DZone

  • How Observability Is Redefining Developer Roles
  • Upgrade Guide To Spring Data Elasticsearch 5.0
  • Mr. Over, the Engineer [Comic]
  • ChatGPT: The Unexpected API Test Automation Help

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: