Search in sources :

Example 26 with TestHighlightRunner

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

the class SynonymTokenizer method testGetBestFragmentsQueryScorer.

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

        @Override
        public void run() throws Exception {
            numHighlights = 0;
            SpanQuery[] clauses = { new SpanTermQuery(new Term("contents", "john")), new SpanTermQuery(new Term("contents", "kennedy")) };
            SpanNearQuery snq = new SpanNearQuery(clauses, 1, true);
            doSearching(snq);
            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 : 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 27 with TestHighlightRunner

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

the class SynonymTokenizer method testMaxSizeHighlight.

public void testMaxSizeHighlight() throws Exception {
    // we disable MockTokenizer checks because we will forcefully limit the
    // tokenstream and call end() before incrementToken() returns false.
    // But we first need to clear the re-used tokenstream components that have enableChecks.
    analyzer.getReuseStrategy().setReusableComponents(analyzer, FIELD_NAME, null);
    analyzer.setEnableChecks(false);
    TestHighlightRunner helper = new TestHighlightRunner() {

        @Override
        public void run() throws Exception {
            numHighlights = 0;
            doSearching(new TermQuery(new Term(FIELD_NAME, "meat")));
            TokenStream tokenStream = analyzer.tokenStream(FIELD_NAME, texts[0]);
            Highlighter highlighter = getHighlighter(query, FIELD_NAME, // new Highlighter(this, new
            HighlighterTest.this);
            // QueryTermScorer(query));
            highlighter.setMaxDocCharsToAnalyze(30);
            highlighter.getBestFragment(tokenStream, texts[0]);
            assertTrue("Setting MaxDocBytesToAnalyze should have prevented " + "us from finding matches for this record: " + numHighlights + " found", numHighlights == 0);
        }
    };
    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) CannedTokenStream(org.apache.lucene.analysis.CannedTokenStream) TokenStream(org.apache.lucene.analysis.TokenStream) Term(org.apache.lucene.index.Term)

Example 28 with TestHighlightRunner

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

the class SynonymTokenizer method testGetBestSingleFragment.

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

        @Override
        public void run() throws Exception {
            doSearching(new TermQuery(new Term(FIELD_NAME, "kennedy")));
            numHighlights = 0;
            for (int i = 0; i < hits.totalHits; i++) {
                final int docId = hits.scoreDocs[i].doc;
                final Document doc = searcher.doc(docId);
                String text = doc.get(FIELD_NAME);
                TokenStream tokenStream = getAnyTokenStream(FIELD_NAME, docId);
                Highlighter highlighter = getHighlighter(query, FIELD_NAME, HighlighterTest.this);
                highlighter.setTextFragmenter(new SimpleFragmenter(40));
                String result = highlighter.getBestFragment(tokenStream, text);
                if (VERBOSE)
                    System.out.println("\t" + result);
            }
            assertTrue("Failed to find correct number of highlights " + numHighlights + " found", numHighlights == 4);
            numHighlights = 0;
            for (int i = 0; i < hits.totalHits; i++) {
                final int docId = hits.scoreDocs[i].doc;
                final Document doc = searcher.doc(docId);
                String text = doc.get(FIELD_NAME);
                TokenStream tokenStream = getAnyTokenStream(FIELD_NAME, docId);
                Highlighter highlighter = getHighlighter(query, FIELD_NAME, HighlighterTest.this);
                highlighter.getBestFragment(tokenStream, text);
            }
            assertTrue("Failed to find correct number of highlights " + numHighlights + " found", numHighlights == 4);
            numHighlights = 0;
            for (int i = 0; i < hits.totalHits; i++) {
                final int docId = hits.scoreDocs[i].doc;
                final Document doc = searcher.doc(docId);
                String text = doc.get(FIELD_NAME);
                TokenStream tokenStream = getAnyTokenStream(FIELD_NAME, docId);
                Highlighter highlighter = getHighlighter(query, FIELD_NAME, HighlighterTest.this);
                highlighter.getBestFragments(tokenStream, text, 10);
            }
            assertTrue("Failed to find correct number of highlights " + numHighlights + " found", numHighlights == 4);
        }
    };
    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) CannedTokenStream(org.apache.lucene.analysis.CannedTokenStream) TokenStream(org.apache.lucene.analysis.TokenStream) Term(org.apache.lucene.index.Term) Document(org.apache.lucene.document.Document) IntPoint(org.apache.lucene.document.IntPoint)

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