Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 794337
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T22:17:53+00:00 2026-05-14T22:17:53+00:00

What is the best way to find out which terms in a query matched

  • 0

What is the best way to find out which terms in a query matched against a given document returned as a hit in lucene?

I have tried a weird method involving hit highlighting package in lucene contrib and also a method that searches for every word in the query against the top most document (“docId: xy AND description: each_word_in_query”).

Do not get satisfactory results?
Hit highlighting does not report some of the words that matched for a document other than the first one.
I’m not sure if the second approach is the best alternative.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T22:17:54+00:00Added an answer on May 14, 2026 at 10:17 pm

    The method explain in the Searcher is a nice way to see which part of a query was matched and how it affects the overall score.

    Example taken from the book Lucene In Action 2nd Edition:

    public class Explainer {
    
      public static void main(String[] args) throws Exception {
    
         if (args.length != 2) {
            System.err.println("Usage: Explainer <index dir> <query>");
            System.exit(1);
         }
    
         String indexDir = args[0];
         String queryExpression = args[1];
         Directory directory = FSDirectory.open(new File(indexDir));
         QueryParser parser = new QueryParser(Version.LUCENE_CURRENT,
                                         "contents", new SimpleAnalyzer());
    
         Query query = parser.parse(queryExpression);
         System.out.println("Query: " + queryExpression);
         IndexSearcher searcher = new IndexSearcher(directory);
         TopDocs topDocs = searcher.search(query, 10);
         for (int i = 0; i < topDocs.totalHits; i++) {
            ScoreDoc match = topDocs.scoreDocs[i];
            Explanation explanation = searcher.explain(query, match.doc);   
            System.out.println("----------");
            Document doc = searcher.doc(match.doc);
            System.out.println(doc.get("title"));
            System.out.println(explanation.toString());
         }
      }
    }
    

    This will explain the score of each document that matches the query.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have to find a best way to find out that elements which is
Which is the best way to find out whether the division of two numbers
what is the best way to find out whether the current device supports a
I am interested to find out what is the best/fastest (most efficient) way to
Given an input sequence, what is the best way to find the longest (not
What's the best possible way to find a given div.myClass element in the current
I'm trying to find out the best way to write my code for better
I am trying to find out the best way for testing admin.ModelAdmin in admin.py
what is the best way to find out how much time an oracle select
What's the best way to find out what's causing unsolved metas? Is there a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.