Search in sources :

Example 1 with CustomQueryScorer

use of org.elasticsearch.search.fetch.subphase.highlight.CustomQueryScorer in project elasticsearch by elastic.

the class PlainHighlighterTests method checkGeoQueryHighlighting.

public void checkGeoQueryHighlighting(Query geoQuery) throws IOException, InvalidTokenOffsetsException {
    Map analysers = new HashMap<String, Analyzer>();
    analysers.put("text", new StandardAnalyzer());
    FieldNameAnalyzer fieldNameAnalyzer = new FieldNameAnalyzer(analysers);
    Query termQuery = new TermQuery(new Term("text", "failure"));
    Query boolQuery = new BooleanQuery.Builder().add(new BooleanClause(geoQuery, BooleanClause.Occur.SHOULD)).add(new BooleanClause(termQuery, BooleanClause.Occur.SHOULD)).build();
    org.apache.lucene.search.highlight.Highlighter highlighter = new org.apache.lucene.search.highlight.Highlighter(new CustomQueryScorer(boolQuery));
    String fragment = highlighter.getBestFragment(fieldNameAnalyzer.tokenStream("text", "Arbitrary text field which should not cause " + "a failure"), "Arbitrary text field which should not cause a failure");
    assertThat(fragment, equalTo("Arbitrary text field which should not cause a <B>failure</B>"));
    Query rewritten = boolQuery.rewrite(null);
    highlighter = new org.apache.lucene.search.highlight.Highlighter(new CustomQueryScorer(rewritten));
    fragment = highlighter.getBestFragment(fieldNameAnalyzer.tokenStream("text", "Arbitrary text field which should not cause " + "a failure"), "Arbitrary text field which should not cause a failure");
    assertThat(fragment, equalTo("Arbitrary text field which should not cause a <B>failure</B>"));
}
Also used : TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) Query(org.apache.lucene.search.Query) PhraseQuery(org.apache.lucene.search.PhraseQuery) GeoPointDistanceQuery(org.apache.lucene.spatial.geopoint.search.GeoPointDistanceQuery) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) GeoPointInBBoxQuery(org.apache.lucene.spatial.geopoint.search.GeoPointInBBoxQuery) GeoPointInPolygonQuery(org.apache.lucene.spatial.geopoint.search.GeoPointInPolygonQuery) HashMap(java.util.HashMap) FieldNameAnalyzer(org.elasticsearch.index.analysis.FieldNameAnalyzer) CustomQueryScorer(org.elasticsearch.search.fetch.subphase.highlight.CustomQueryScorer) Term(org.apache.lucene.index.Term) BooleanClause(org.apache.lucene.search.BooleanClause) StandardAnalyzer(org.apache.lucene.analysis.standard.StandardAnalyzer) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with CustomQueryScorer

use of org.elasticsearch.search.fetch.subphase.highlight.CustomQueryScorer in project elasticsearch by elastic.

the class PlainHighlighterTests method testHighlightPhrase.

public void testHighlightPhrase() throws Exception {
    Query query = new PhraseQuery.Builder().add(new Term("field", "foo")).add(new Term("field", "bar")).build();
    QueryScorer queryScorer = new CustomQueryScorer(query);
    org.apache.lucene.search.highlight.Highlighter highlighter = new org.apache.lucene.search.highlight.Highlighter(queryScorer);
    String[] frags = highlighter.getBestFragments(new MockAnalyzer(random()), "field", "bar foo bar foo", 10);
    assertArrayEquals(new String[] { "bar <B>foo</B> <B>bar</B> foo" }, frags);
}
Also used : Query(org.apache.lucene.search.Query) PhraseQuery(org.apache.lucene.search.PhraseQuery) GeoPointDistanceQuery(org.apache.lucene.spatial.geopoint.search.GeoPointDistanceQuery) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) GeoPointInBBoxQuery(org.apache.lucene.spatial.geopoint.search.GeoPointInBBoxQuery) GeoPointInPolygonQuery(org.apache.lucene.spatial.geopoint.search.GeoPointInPolygonQuery) PhraseQuery(org.apache.lucene.search.PhraseQuery) QueryScorer(org.apache.lucene.search.highlight.QueryScorer) CustomQueryScorer(org.elasticsearch.search.fetch.subphase.highlight.CustomQueryScorer) CustomQueryScorer(org.elasticsearch.search.fetch.subphase.highlight.CustomQueryScorer) Term(org.apache.lucene.index.Term) MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer)

Aggregations

Term (org.apache.lucene.index.Term)2 BooleanQuery (org.apache.lucene.search.BooleanQuery)2 PhraseQuery (org.apache.lucene.search.PhraseQuery)2 Query (org.apache.lucene.search.Query)2 TermQuery (org.apache.lucene.search.TermQuery)2 GeoPointDistanceQuery (org.apache.lucene.spatial.geopoint.search.GeoPointDistanceQuery)2 GeoPointInBBoxQuery (org.apache.lucene.spatial.geopoint.search.GeoPointInBBoxQuery)2 GeoPointInPolygonQuery (org.apache.lucene.spatial.geopoint.search.GeoPointInPolygonQuery)2 CustomQueryScorer (org.elasticsearch.search.fetch.subphase.highlight.CustomQueryScorer)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1 MockAnalyzer (org.apache.lucene.analysis.MockAnalyzer)1 StandardAnalyzer (org.apache.lucene.analysis.standard.StandardAnalyzer)1 BooleanClause (org.apache.lucene.search.BooleanClause)1 QueryScorer (org.apache.lucene.search.highlight.QueryScorer)1 FieldNameAnalyzer (org.elasticsearch.index.analysis.FieldNameAnalyzer)1