Search in sources :

Example 21 with TestHighlightRunner

use of org.apache.lucene.search.highlight.SynonymTokenizer.TestHighlightRunner in project lucene-solr by apache.

the class SynonymTokenizer method testGetBestFragmentsWithOr.

public void testGetBestFragmentsWithOr() throws Exception {
    TestHighlightRunner helper = new TestHighlightRunner() {

        @Override
        public void run() throws Exception {
            numHighlights = 0;
            BooleanQuery.Builder query = new BooleanQuery.Builder();
            query.add(new TermQuery(new Term(FIELD_NAME, "jfk")), Occur.SHOULD);
            query.add(new TermQuery(new Term(FIELD_NAME, "kennedy")), Occur.SHOULD);
            doSearching(query.build());
            doStandardHighlights(analyzer, searcher, hits, query.build(), HighlighterTest.this);
            assertTrue("Failed to find correct number of highlights " + numHighlights + " found", numHighlights == 5);
        }
    };
    helper.start();
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) MultiTermQuery(org.apache.lucene.search.MultiTermQuery) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) TermQuery(org.apache.lucene.search.TermQuery) TestHighlightRunner(org.apache.lucene.search.highlight.SynonymTokenizer.TestHighlightRunner) Builder(org.apache.lucene.search.PhraseQuery.Builder) DocumentBuilder(javax.xml.parsers.DocumentBuilder) Term(org.apache.lucene.index.Term)

Example 22 with TestHighlightRunner

use of org.apache.lucene.search.highlight.SynonymTokenizer.TestHighlightRunner in project lucene-solr by apache.

the class SynonymTokenizer method testGetBestFragmentsFilteredPhraseQuery.

public void testGetBestFragmentsFilteredPhraseQuery() throws Exception {
    TestHighlightRunner helper = new TestHighlightRunner() {

        @Override
        public void run() throws Exception {
            numHighlights = 0;
            PhraseQuery pq = new PhraseQuery("contents", "john", "kennedy");
            BooleanQuery.Builder bq = new BooleanQuery.Builder();
            bq.add(pq, Occur.MUST);
            bq.add(TermRangeQuery.newStringRange("contents", "john", "john", true, true), Occur.FILTER);
            doSearching(bq.build());
            doStandardHighlights(analyzer, searcher, hits, query, HighlighterTest.this);
            // Currently highlights "John" and "Kennedy" separately
            assertTrue("Failed to find correct number of highlights " + numHighlights + " found", numHighlights == 2);
        }
    };
    helper.start();
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) TestHighlightRunner(org.apache.lucene.search.highlight.SynonymTokenizer.TestHighlightRunner) PhraseQuery(org.apache.lucene.search.PhraseQuery) MultiPhraseQuery(org.apache.lucene.search.MultiPhraseQuery) Builder(org.apache.lucene.search.PhraseQuery.Builder) DocumentBuilder(javax.xml.parsers.DocumentBuilder)

Example 23 with TestHighlightRunner

use of org.apache.lucene.search.highlight.SynonymTokenizer.TestHighlightRunner in project lucene-solr by apache.

the class SynonymTokenizer method testMaxSizeHighlightTruncates.

public void testMaxSizeHighlightTruncates() throws Exception {
    TestHighlightRunner helper = new TestHighlightRunner() {

        @Override
        public void run() throws Exception {
            String goodWord = "goodtoken";
            CharacterRunAutomaton stopWords = new CharacterRunAutomaton(Automata.makeString("stoppedtoken"));
            // we disable MockTokenizer checks because we will forcefully limit the 
            // tokenstream and call end() before incrementToken() returns false.
            final MockAnalyzer analyzer = new MockAnalyzer(random(), MockTokenizer.SIMPLE, true, stopWords);
            analyzer.setEnableChecks(false);
            TermQuery query = new TermQuery(new Term("data", goodWord));
            String match;
            StringBuilder sb = new StringBuilder();
            sb.append(goodWord);
            for (int i = 0; i < 10000; i++) {
                sb.append(" ");
                // only one stopword
                sb.append("stoppedtoken");
            }
            SimpleHTMLFormatter fm = new SimpleHTMLFormatter();
            // new Highlighter(fm,
            Highlighter hg = getHighlighter(query, "data", fm);
            // new
            // QueryTermScorer(query));
            hg.setTextFragmenter(new NullFragmenter());
            hg.setMaxDocCharsToAnalyze(100);
            match = hg.getBestFragment(analyzer, "data", sb.toString());
            assertTrue("Matched text should be no more than 100 chars in length ", match.length() < hg.getMaxDocCharsToAnalyze());
            // add another tokenized word to the overrall length - but set way
            // beyond
            // the length of text under consideration (after a large slug of stop
            // words
            // + whitespace)
            sb.append(" ");
            sb.append(goodWord);
            match = hg.getBestFragment(analyzer, "data", sb.toString());
            assertTrue("Matched text should be no more than 100 chars in length ", match.length() < hg.getMaxDocCharsToAnalyze());
        }
    };
    helper.start();
}
Also used : MultiTermQuery(org.apache.lucene.search.MultiTermQuery) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) TermQuery(org.apache.lucene.search.TermQuery) TestHighlightRunner(org.apache.lucene.search.highlight.SynonymTokenizer.TestHighlightRunner) MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer) CharacterRunAutomaton(org.apache.lucene.util.automaton.CharacterRunAutomaton) Term(org.apache.lucene.index.Term) IntPoint(org.apache.lucene.document.IntPoint)

Example 24 with TestHighlightRunner

use of org.apache.lucene.search.highlight.SynonymTokenizer.TestHighlightRunner in project lucene-solr by apache.

the class SynonymTokenizer method testNearSpanSimpleQuery.

public void testNearSpanSimpleQuery() throws Exception {
    doSearching(new SpanNearQuery(new SpanQuery[] { new SpanTermQuery(new Term(FIELD_NAME, "beginning")), new SpanTermQuery(new Term(FIELD_NAME, "kennedy")) }, 3, false));
    TestHighlightRunner helper = new TestHighlightRunner() {

        @Override
        public void run() throws Exception {
            mode = QUERY;
            doStandardHighlights(analyzer, searcher, hits, query, HighlighterTest.this);
        }
    };
    helper.run();
    assertTrue("Failed to find correct number of highlights " + numHighlights + " found", numHighlights == 2);
}
Also used : TestHighlightRunner(org.apache.lucene.search.highlight.SynonymTokenizer.TestHighlightRunner) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) Term(org.apache.lucene.index.Term) SpanNearQuery(org.apache.lucene.search.spans.SpanNearQuery) SpanQuery(org.apache.lucene.search.spans.SpanQuery)

Example 25 with TestHighlightRunner

use of org.apache.lucene.search.highlight.SynonymTokenizer.TestHighlightRunner in project lucene-solr by apache.

the class SynonymTokenizer method testGetBestFragmentsMultiTerm.

public void testGetBestFragmentsMultiTerm() throws Exception {
    TestHighlightRunner helper = new TestHighlightRunner() {

        @Override
        public void run() throws Exception {
            numHighlights = 0;
            BooleanQuery.Builder booleanQuery = new BooleanQuery.Builder();
            booleanQuery.add(new TermQuery(new Term(FIELD_NAME, "john")), Occur.SHOULD);
            PrefixQuery prefixQuery = new PrefixQuery(new Term(FIELD_NAME, "kenn"));
            prefixQuery.setRewriteMethod(MultiTermQuery.SCORING_BOOLEAN_REWRITE);
            booleanQuery.add(prefixQuery, Occur.SHOULD);
            doSearching(booleanQuery.build());
            doStandardHighlights(analyzer, searcher, hits, query, HighlighterTest.this);
            assertTrue("Failed to find correct number of highlights " + numHighlights + " found", numHighlights == 5);
        }
    };
    helper.start();
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) MultiTermQuery(org.apache.lucene.search.MultiTermQuery) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) TermQuery(org.apache.lucene.search.TermQuery) TestHighlightRunner(org.apache.lucene.search.highlight.SynonymTokenizer.TestHighlightRunner) PrefixQuery(org.apache.lucene.search.PrefixQuery) Builder(org.apache.lucene.search.PhraseQuery.Builder) DocumentBuilder(javax.xml.parsers.DocumentBuilder) Term(org.apache.lucene.index.Term)

Aggregations

TestHighlightRunner (org.apache.lucene.search.highlight.SynonymTokenizer.TestHighlightRunner)28 Term (org.apache.lucene.index.Term)24 SpanTermQuery (org.apache.lucene.search.spans.SpanTermQuery)20 MultiTermQuery (org.apache.lucene.search.MultiTermQuery)16 TermQuery (org.apache.lucene.search.TermQuery)16 DocumentBuilder (javax.xml.parsers.DocumentBuilder)10 BooleanQuery (org.apache.lucene.search.BooleanQuery)10 Builder (org.apache.lucene.search.PhraseQuery.Builder)10 CannedTokenStream (org.apache.lucene.analysis.CannedTokenStream)8 TokenStream (org.apache.lucene.analysis.TokenStream)8 SpanNearQuery (org.apache.lucene.search.spans.SpanNearQuery)6 IntPoint (org.apache.lucene.document.IntPoint)5 WildcardQuery (org.apache.lucene.search.WildcardQuery)5 SpanQuery (org.apache.lucene.search.spans.SpanQuery)5 MultiPhraseQuery (org.apache.lucene.search.MultiPhraseQuery)4 PhraseQuery (org.apache.lucene.search.PhraseQuery)4 MockAnalyzer (org.apache.lucene.analysis.MockAnalyzer)3 Document (org.apache.lucene.document.Document)3 ConstantScoreQuery (org.apache.lucene.search.ConstantScoreQuery)3 FuzzyQuery (org.apache.lucene.search.FuzzyQuery)3