Wednesday, May 22, 2013

Apache Lucene Search Word Highlighter use LIKE wildcard




public static String highlightField(String searchField, String searchWord,  String text) throws Exception {
        QueryParser parser = new QueryParser(Version.LUCENE_42, searchField, analyzer);
        parser.setAllowLeadingWildcard(true);
        Query query = parser.parse("*"+searchWord+"*");

        TokenStream tokenStream = analyzer.tokenStream(searchField, new StringReader(text));

        // Assuming "", "" used to highlight
        SimpleHTMLFormatter formatter = new SimpleHTMLFormatter("", ""); // #F8B270 - Orange
        //SimpleHTMLFormatter formatter = new SimpleHTMLFormatter();
        MyQueryScorer scorer = new MyQueryScorer(query, searchField, searchField);
        Highlighter highlighter = new Highlighter(formatter, scorer);
        highlighter.setTextFragmenter(new SimpleFragmenter(Integer.MAX_VALUE));

        String rv = highlighter.getBestFragments(tokenStream, text, 1, "");
        //String rv = highlighter.getBestFragments(tokenStream, text, 1, "(FIELD TEXT TRUNCATED)");
        return (rv == null || rv.trim().isEmpty()) ? text : rv;
    }


public static class MyWeightedSpanTermExtractor extends WeightedSpanTermExtractor {

        public MyWeightedSpanTermExtractor() {
            super();
        }

        public MyWeightedSpanTermExtractor(String defaultField) {
            super(defaultField);
        }

        @Override
        protected void extractUnknownQuery(Query query,
                Map terms) throws IOException {
            if (query instanceof CustomQuery) {
                extractWeightedTerms(terms, new TermQuery(((CustomQuery) query).term));
            }
        }
    }

    public static class MyQueryScorer extends QueryScorer {

        public MyQueryScorer(Query query, String field, String defaultField) {
            super(query, field, defaultField);
        }

        @Override
        protected WeightedSpanTermExtractor newTermExtractor(String defaultField) {
            return defaultField == null ? new MyWeightedSpanTermExtractor()
                    : new MyWeightedSpanTermExtractor(defaultField);
        }
    }

    public static class CustomQuery extends Query {

        private final Term term;

        public CustomQuery(Term term) {
            super();
            this.term = term;
        }

        @Override
        public String toString(String field) {
            return new TermQuery(term).toString(field);
        }

        @Override
        public Query rewrite(IndexReader reader) throws IOException {
            return new TermQuery(term);
        }

        @Override
        public int hashCode() {
            final int prime = 31;
            int result = super.hashCode();
            result = prime * result + ((term == null) ? 0 : term.hashCode());
            return result;
        }

        @Override
        public boolean equals(Object obj) {
            if (this == obj) {
                return true;
            }
            if (!super.equals(obj)) {
                return false;
            }
            if (getClass() != obj.getClass()) {
                return false;
            }
            CustomQuery other = (CustomQuery) obj;
            if (term == null) {
                if (other.term != null) {
                    return false;
                }
            } else if (!term.equals(other.term)) {
                return false;
            }
            return true;
        }
    }


No comments: