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 5938557
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T15:42:11+00:00 2026-05-22T15:42:11+00:00

how do you get the matching fuzzy term and its offset when using Lucene

  • 0

how do you get the matching fuzzy term and its offset when using Lucene Fuzzy Search?

    IndexSearcher mem = ....(some standard code)

    QueryParser parser = new QueryParser(Version.LUCENE_30, CONTENT_FIELD, analyzer);

    TopDocs topDocs = mem.search(parser.parse("wuzzy~"), 1);
    // the ~ triggers the fuzzy search as per "Lucene In Action" 

The fuzzy search works fine. If a document contains the term “fuzzy” or “luzzy”, it is matched. How do I get which term matched and what are their offsets?

I have made sure that all CONTENT_FIELDs are added with termVectorStored with positions and offsets .

  • 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-22T15:42:12+00:00Added an answer on May 22, 2026 at 3:42 pm

    There was no straight forward way of doing this, however I reconsidered Jared’s suggestion and was able to get the solution working.

    I am documenting this here just in case someone else has the same issue.

    Create a class that implements org.apache.lucene.search.highlight.Formatter

    public class HitPositionCollector implements Formatter
    {
        // MatchOffset is a simple DTO
        private List<MatchOffset> matchList;
        public HitPositionCollector(
        {
            matchList = new ArrayList<MatchOffset>();
        }
    
        // this ie where the term start and end offset as well as the actual term is captured
        @Override
        public String highlightTerm(String originalText, TokenGroup tokenGroup)
        {
            if (tokenGroup.getTotalScore() <= 0)
            {
            }
            else
            {
                MatchOffset mo= new MatchOffset(tokenGroup.getToken(0).toString(), tokenGroup.getStartOffset(),tokenGroup.getEndOffset());
                getMatchList().add(mo);
            }
    
            return originalText;
        }
    
        /**
        * @return the matchList
        */
        public List<MatchOffset> getMatchList()
        {
            return matchList;
        }
    }
    

    Main Code

    public void testHitsWithHitPositionCollector() throws Exception
    {
        System.out.println(" .... testHitsWithHitPositionCollector");
        String fuzzyStr = "bro*";
    
        QueryParser parser = new QueryParser(Version.LUCENE_30, "f", analyzer);
        Query fzyQry = parser.parse(fuzzyStr);
        TopDocs hits = searcher.search(fzyQry, 10);
    
        QueryScorer scorer = new QueryScorer(fzyQry, "f");
    
        HitPositionCollector myFormatter= new HitPositionCollector();
    
        //Highlighter(Formatter formatter, Scorer fragmentScorer)
        Highlighter highlighter = new Highlighter(myFormatter,scorer);
        highlighter.setTextFragmenter(
            new SimpleSpanFragmenter(scorer)
        );
    
        Analyzer analyzer2 = new SimpleAnalyzer();
    
        int loopIndex=0;
        //for (ScoreDoc sd : hits.scoreDocs) {
            Document doc = searcher.doc( hits.scoreDocs[0].doc);
            String title = doc.get("f");
    
            TokenStream stream = TokenSources.getAnyTokenStream(searcher.getIndexReader(),
                                        hits.scoreDocs[0].doc,
                                        "f",
                                        doc,
                                        analyzer2);
    
            String fragment = highlighter.getBestFragment(stream, title);
    
            System.out.println(fragment);
            assertEquals("the quick brown fox jumps over the lazy dog", fragment);
            MatchOffset mo= myFormatter.getMatchList().get(loopIndex++);
    
            assertTrue(mo.getEndPos()==15);
            assertTrue(mo.getStartPos()==10);
            assertTrue(mo.getToken().equals("brown"));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using yahoo pipes to get content matching a certian category from my
How to query to get count of matching words in a field, specifically in
I would like to get the first item from a list matching a condition.
Is there a way to get pattern matching to match my value with any
I'm having some serious issues using Zend_Lucene and foreign characters like åäö. These issues
In MySQL, how can I compare two strings and get matching percentage. Something like
I was looking through some code on a project of mine and thinking about
I've had some brilliant help before and I'm hoping you can get me out
I have to perform a fuzzy matching between a source string and a set
I am looking for a way to do a fuzzy match using regular expressions.

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.