Search in sources :

Example 1 with Field

use of org.opensearch.search.fetch.subphase.highlight.HighlightBuilder.Field in project OpenSearch by opensearch-project.

the class HighlighterSearchIT method checkMatchedFieldsCase.

private void checkMatchedFieldsCase(boolean requireFieldMatch) throws Exception {
    Settings.Builder settings = Settings.builder();
    settings.put(indexSettings());
    settings.put("index.analysis.analyzer.mock_english.tokenizer", "standard");
    settings.put("index.analysis.analyzer.mock_english.filter", "mock_snowball");
    assertAcked(prepareCreate("test").setSettings(settings).addMapping("type1", XContentFactory.jsonBuilder().startObject().startObject("type1").startObject("properties").startObject("foo").field("type", "text").field("term_vector", "with_positions_offsets").field("store", true).field("analyzer", "mock_english").startObject("fields").startObject("plain").field("type", "text").field("term_vector", "with_positions_offsets").field("analyzer", "standard").endObject().endObject().endObject().startObject("bar").field("type", "text").field("term_vector", "with_positions_offsets").field("store", true).field("analyzer", "mock_english").startObject("fields").startObject("plain").field("type", "text").field("term_vector", "with_positions_offsets").field("analyzer", "standard").endObject().endObject().endObject().endObject().endObject().endObject()));
    ensureGreen();
    index("test", "type1", "1", "foo", "running with scissors");
    index("test", "type1", "2", "foo", "cat cat junk junk junk junk junk junk junk cats junk junk", "bar", "cat cat junk junk junk junk junk junk junk cats junk junk");
    index("test", "type1", "3", "foo", "weird", "bar", "result");
    refresh();
    Field fooField = new Field("foo").numOfFragments(1).order("score").fragmentSize(25).highlighterType("fvh").requireFieldMatch(requireFieldMatch);
    SearchRequestBuilder req = client().prepareSearch("test").highlighter(new HighlightBuilder().field(fooField));
    // First check highlighting without any matched fields set
    SearchResponse resp = req.setQuery(queryStringQuery("running scissors").field("foo")).get();
    assertHighlight(resp, 0, "foo", 0, equalTo("<em>running</em> with <em>scissors</em>"));
    // And that matching a subfield doesn't automatically highlight it
    resp = req.setQuery(queryStringQuery("foo.plain:running scissors").field("foo")).get();
    assertHighlight(resp, 0, "foo", 0, equalTo("running with <em>scissors</em>"));
    // Add the subfield to the list of matched fields but don't match it. Everything should still work
    // like before we added it.
    fooField = new Field("foo").numOfFragments(1).order("score").fragmentSize(25).highlighterType("fvh").requireFieldMatch(requireFieldMatch);
    fooField.matchedFields("foo", "foo.plain");
    req = client().prepareSearch("test").highlighter(new HighlightBuilder().field(fooField));
    resp = req.setQuery(queryStringQuery("running scissors").field("foo")).get();
    assertHighlight(resp, 0, "foo", 0, equalTo("<em>running</em> with <em>scissors</em>"));
    // Now make half the matches come from the stored field and half from just a matched field.
    resp = req.setQuery(queryStringQuery("foo.plain:running scissors").field("foo")).get();
    assertHighlight(resp, 0, "foo", 0, equalTo("<em>running</em> with <em>scissors</em>"));
    // Now remove the stored field from the matched field list. That should work too.
    fooField = new Field("foo").numOfFragments(1).order("score").fragmentSize(25).highlighterType("fvh").requireFieldMatch(requireFieldMatch);
    fooField.matchedFields("foo.plain");
    req = client().prepareSearch("test").highlighter(new HighlightBuilder().field(fooField));
    resp = req.setQuery(queryStringQuery("foo.plain:running scissors").field("foo")).get();
    assertHighlight(resp, 0, "foo", 0, equalTo("<em>running</em> with scissors"));
    // Now make sure boosted fields don't blow up when matched fields is both the subfield and stored field.
    fooField = new Field("foo").numOfFragments(1).order("score").fragmentSize(25).highlighterType("fvh").requireFieldMatch(requireFieldMatch);
    fooField.matchedFields("foo", "foo.plain");
    req = client().prepareSearch("test").highlighter(new HighlightBuilder().field(fooField));
    resp = req.setQuery(queryStringQuery("foo.plain:running^5 scissors").field("foo")).get();
    assertHighlight(resp, 0, "foo", 0, equalTo("<em>running</em> with <em>scissors</em>"));
    // Now just all matches are against the matched field. This still returns highlighting.
    resp = req.setQuery(queryStringQuery("foo.plain:running foo.plain:scissors").field("foo")).get();
    assertHighlight(resp, 0, "foo", 0, equalTo("<em>running</em> with <em>scissors</em>"));
    // And all matched field via the queryString's field parameter, just in case
    resp = req.setQuery(queryStringQuery("running scissors").field("foo.plain")).get();
    assertHighlight(resp, 0, "foo", 0, equalTo("<em>running</em> with <em>scissors</em>"));
    // Finding the same string two ways is ok too
    resp = req.setQuery(queryStringQuery("run foo.plain:running^5 scissors").field("foo")).get();
    assertHighlight(resp, 0, "foo", 0, equalTo("<em>running</em> with <em>scissors</em>"));
    // But we use the best found score when sorting fragments
    resp = req.setQuery(queryStringQuery("cats foo.plain:cats^5").field("foo")).get();
    assertHighlight(resp, 0, "foo", 0, equalTo("junk junk <em>cats</em> junk junk"));
    // which can also be written by searching on the subfield
    resp = req.setQuery(queryStringQuery("cats").field("foo").field("foo.plain", 5)).get();
    assertHighlight(resp, 0, "foo", 0, equalTo("junk junk <em>cats</em> junk junk"));
    // Speaking of two fields, you can have two fields, only one of which has matchedFields enabled
    QueryBuilder twoFieldsQuery = queryStringQuery("cats").field("foo").field("foo.plain", 5).field("bar").field("bar.plain", 5);
    Field barField = new Field("bar").numOfFragments(1).order("score").fragmentSize(25).highlighterType("fvh").requireFieldMatch(requireFieldMatch);
    resp = req.setQuery(twoFieldsQuery).highlighter(new HighlightBuilder().field(fooField).field(barField)).get();
    assertHighlight(resp, 0, "foo", 0, equalTo("junk junk <em>cats</em> junk junk"));
    assertHighlight(resp, 0, "bar", 0, equalTo("<em>cat</em> <em>cat</em> junk junk junk junk"));
    // And you can enable matchedField highlighting on both
    barField.matchedFields("bar", "bar.plain");
    resp = req.setQuery(twoFieldsQuery).highlighter(new HighlightBuilder().field(fooField).field(barField)).get();
    assertHighlight(resp, 0, "foo", 0, equalTo("junk junk <em>cats</em> junk junk"));
    assertHighlight(resp, 0, "bar", 0, equalTo("junk junk <em>cats</em> junk junk"));
    // Setting a matchedField that isn't searched/doesn't exist is simply ignored.
    barField.matchedFields("bar", "candy");
    resp = req.setQuery(twoFieldsQuery).highlighter(new HighlightBuilder().field(fooField).field(barField)).get();
    assertHighlight(resp, 0, "foo", 0, equalTo("junk junk <em>cats</em> junk junk"));
    assertHighlight(resp, 0, "bar", 0, equalTo("<em>cat</em> <em>cat</em> junk junk junk junk"));
    // If the stored field doesn't have a value it doesn't matter what you match, you get nothing.
    barField.matchedFields("bar", "foo.plain");
    resp = req.setQuery(queryStringQuery("running scissors").field("foo.plain").field("bar")).highlighter(new HighlightBuilder().field(fooField).field(barField)).get();
    assertHighlight(resp, 0, "foo", 0, equalTo("<em>running</em> with <em>scissors</em>"));
    assertThat(resp.getHits().getAt(0).getHighlightFields(), not(hasKey("bar")));
    // If the stored field is found but the matched field isn't then you don't get a result either.
    fooField.matchedFields("bar.plain");
    resp = req.setQuery(queryStringQuery("running scissors").field("foo").field("foo.plain").field("bar").field("bar.plain")).highlighter(new HighlightBuilder().field(fooField).field(barField)).get();
    assertThat(resp.getHits().getAt(0).getHighlightFields(), not(hasKey("foo")));
    // But if you add the stored field to the list of matched fields then you'll get a result again
    fooField.matchedFields("foo", "bar.plain");
    resp = req.setQuery(queryStringQuery("running scissors").field("foo").field("foo.plain").field("bar").field("bar.plain")).highlighter(new HighlightBuilder().field(fooField).field(barField)).get();
    assertHighlight(resp, 0, "foo", 0, equalTo("<em>running</em> with <em>scissors</em>"));
    assertThat(resp.getHits().getAt(0).getHighlightFields(), not(hasKey("bar")));
    // You _can_ highlight fields that aren't subfields of one another.
    resp = req.setQuery(queryStringQuery("weird").field("foo").field("foo.plain").field("bar").field("bar.plain")).highlighter(new HighlightBuilder().field(fooField).field(barField)).get();
    assertHighlight(resp, 0, "foo", 0, equalTo("<em>weird</em>"));
    assertHighlight(resp, 0, "bar", 0, equalTo("<em>resul</em>t"));
    assertFailures(req.setQuery(queryStringQuery("result").field("foo").field("foo.plain").field("bar").field("bar.plain")), RestStatus.INTERNAL_SERVER_ERROR, containsString("IndexOutOfBoundsException"));
}
Also used : Field(org.opensearch.search.fetch.subphase.highlight.HighlightBuilder.Field) SearchRequestBuilder(org.opensearch.action.search.SearchRequestBuilder) Builder(org.opensearch.common.settings.Settings.Builder) QueryBuilder(org.opensearch.index.query.QueryBuilder) AbstractQueryBuilder(org.opensearch.index.query.AbstractQueryBuilder) MultiMatchQueryBuilder(org.opensearch.index.query.MultiMatchQueryBuilder) IdsQueryBuilder(org.opensearch.index.query.IdsQueryBuilder) MatchQueryBuilder(org.opensearch.index.query.MatchQueryBuilder) FunctionScoreQueryBuilder(org.opensearch.index.query.functionscore.FunctionScoreQueryBuilder) Settings(org.opensearch.common.settings.Settings) OpenSearchAssertions.assertSearchResponse(org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse) SearchResponse(org.opensearch.action.search.SearchResponse)

Example 2 with Field

use of org.opensearch.search.fetch.subphase.highlight.HighlightBuilder.Field in project OpenSearch by opensearch-project.

the class HighlighterSearchIT method testFieldAliasWithWildcardField.

public void testFieldAliasWithWildcardField() throws IOException {
    XContentBuilder mappings = jsonBuilder().startObject().startObject("type").startObject("properties").startObject("keyword").field("type", "keyword").endObject().startObject("alias").field("type", "alias").field("path", "keyword").endObject().endObject().endObject().endObject();
    assertAcked(prepareCreate("test").addMapping("type", mappings));
    client().prepareIndex("test").setId("1").setSource("keyword", "foo").get();
    refresh();
    HighlightBuilder builder = new HighlightBuilder().field(new Field("al*")).requireFieldMatch(false);
    SearchResponse search = client().prepareSearch().setQuery(matchQuery("alias", "foo")).highlighter(builder).get();
    assertHighlight(search, 0, "alias", 0, equalTo("<em>foo</em>"));
}
Also used : Field(org.opensearch.search.fetch.subphase.highlight.HighlightBuilder.Field) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) OpenSearchAssertions.assertSearchResponse(org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse) SearchResponse(org.opensearch.action.search.SearchResponse)

Example 3 with Field

use of org.opensearch.search.fetch.subphase.highlight.HighlightBuilder.Field in project OpenSearch by opensearch-project.

the class HighlighterSearchIT method testHighlightUsesHighlightQuery.

public void testHighlightUsesHighlightQuery() throws IOException {
    assertAcked(prepareCreate("test").addMapping("type1", "text", "type=text," + randomStoreField() + "term_vector=with_positions_offsets,index_options=offsets"));
    ensureGreen();
    index("test", "type1", "1", "text", "Testing the highlight query feature");
    refresh();
    for (String type : ALL_TYPES) {
        HighlightBuilder.Field field = new HighlightBuilder.Field("text");
        HighlightBuilder highlightBuilder = new HighlightBuilder().field(field).highlighterType(type);
        SearchRequestBuilder search = client().prepareSearch("test").setQuery(QueryBuilders.matchQuery("text", "testing")).highlighter(highlightBuilder);
        Matcher<String> searchQueryMatcher = equalTo("<em>Testing</em> the highlight query feature");
        SearchResponse response = search.get();
        assertHighlight(response, 0, "text", 0, searchQueryMatcher);
        field = new HighlightBuilder.Field("text");
        Matcher<String> hlQueryMatcher = equalTo("Testing the highlight <em>query</em> feature");
        field.highlightQuery(matchQuery("text", "query"));
        highlightBuilder = new HighlightBuilder().field(field);
        search = client().prepareSearch("test").setQuery(QueryBuilders.matchQuery("text", "testing")).highlighter(highlightBuilder);
        response = search.get();
        assertHighlight(response, 0, "text", 0, hlQueryMatcher);
        // Make sure the highlightQuery is taken into account when it is set on the highlight context instead of the field
        highlightBuilder.highlightQuery(matchQuery("text", "query"));
        field.highlighterType(type).highlightQuery(null);
        response = search.get();
        assertHighlight(response, 0, "text", 0, hlQueryMatcher);
    }
}
Also used : Field(org.opensearch.search.fetch.subphase.highlight.HighlightBuilder.Field) SearchRequestBuilder(org.opensearch.action.search.SearchRequestBuilder) Matchers.containsString(org.hamcrest.Matchers.containsString) Field(org.opensearch.search.fetch.subphase.highlight.HighlightBuilder.Field) OpenSearchAssertions.assertSearchResponse(org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse) SearchResponse(org.opensearch.action.search.SearchResponse)

Example 4 with Field

use of org.opensearch.search.fetch.subphase.highlight.HighlightBuilder.Field in project OpenSearch by opensearch-project.

the class HighlighterSearchIT method testACopyFieldWithNestedQuery.

public void testACopyFieldWithNestedQuery() throws Exception {
    String mapping = Strings.toString(jsonBuilder().startObject().startObject("type").startObject("properties").startObject("foo").field("type", "nested").startObject("properties").startObject("text").field("type", "text").field("copy_to", "foo_text").endObject().endObject().endObject().startObject("foo_text").field("type", "text").field("term_vector", "with_positions_offsets").field("store", true).endObject().endObject().endObject().endObject());
    prepareCreate("test").addMapping("type", mapping, XContentType.JSON).get();
    client().prepareIndex("test").setId("1").setSource(jsonBuilder().startObject().startArray("foo").startObject().field("text", "brown").endObject().startObject().field("text", "cow").endObject().endArray().endObject()).setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE).get();
    SearchResponse searchResponse = client().prepareSearch().setQuery(nestedQuery("foo", matchQuery("foo.text", "brown cow"), ScoreMode.None)).highlighter(new HighlightBuilder().field(new Field("foo_text").highlighterType("fvh")).requireFieldMatch(false)).get();
    assertHitCount(searchResponse, 1);
    HighlightField field = searchResponse.getHits().getAt(0).getHighlightFields().get("foo_text");
    assertThat(field.getFragments().length, equalTo(2));
    assertThat(field.getFragments()[0].string(), equalTo("<em>brown</em>"));
    assertThat(field.getFragments()[1].string(), equalTo("<em>cow</em>"));
}
Also used : Field(org.opensearch.search.fetch.subphase.highlight.HighlightBuilder.Field) Matchers.containsString(org.hamcrest.Matchers.containsString) OpenSearchAssertions.assertSearchResponse(org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse) SearchResponse(org.opensearch.action.search.SearchResponse)

Example 5 with Field

use of org.opensearch.search.fetch.subphase.highlight.HighlightBuilder.Field in project OpenSearch by opensearch-project.

the class HighlighterSearchIT method testFastVectorHighlighterMultipleFields.

public void testFastVectorHighlighterMultipleFields() {
    assertAcked(prepareCreate("test").addMapping("type1", "field1", "type=text,term_vector=with_positions_offsets", "field2", "type=text,term_vector=with_positions_offsets"));
    ensureGreen();
    index("test", "type1", "1", "field1", "The <b>quick<b> brown fox", "field2", "The <b>slow<b> brown fox");
    refresh();
    SearchResponse response = client().prepareSearch("test").setQuery(QueryBuilders.matchQuery("field1", "fox")).highlighter(new HighlightBuilder().field(new HighlightBuilder.Field("field1").preTags("<1>").postTags("</1>").requireFieldMatch(true)).field(new HighlightBuilder.Field("field2").preTags("<2>").postTags("</2>").requireFieldMatch(false))).get();
    assertHighlight(response, 0, "field1", 0, 1, equalTo("The <b>quick<b> brown <1>fox</1>"));
    assertHighlight(response, 0, "field2", 0, 1, equalTo("The <b>slow<b> brown <2>fox</2>"));
}
Also used : Field(org.opensearch.search.fetch.subphase.highlight.HighlightBuilder.Field) OpenSearchAssertions.assertSearchResponse(org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse) SearchResponse(org.opensearch.action.search.SearchResponse) Field(org.opensearch.search.fetch.subphase.highlight.HighlightBuilder.Field)

Aggregations

Field (org.opensearch.search.fetch.subphase.highlight.HighlightBuilder.Field)35 SearchResponse (org.opensearch.action.search.SearchResponse)31 OpenSearchAssertions.assertSearchResponse (org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse)29 Matchers.containsString (org.hamcrest.Matchers.containsString)18 XContentBuilder (org.opensearch.common.xcontent.XContentBuilder)10 SearchSourceBuilder (org.opensearch.search.builder.SearchSourceBuilder)6 IdsQueryBuilder (org.opensearch.index.query.IdsQueryBuilder)4 MatchQueryBuilder (org.opensearch.index.query.MatchQueryBuilder)4 MultiMatchQueryBuilder (org.opensearch.index.query.MultiMatchQueryBuilder)4 FunctionScoreQueryBuilder (org.opensearch.index.query.functionscore.FunctionScoreQueryBuilder)4 SearchRequestBuilder (org.opensearch.action.search.SearchRequestBuilder)3 QueryBuilder (org.opensearch.index.query.QueryBuilder)3 IndexRequestBuilder (org.opensearch.action.index.IndexRequestBuilder)2 GeoPoint (org.opensearch.common.geo.GeoPoint)2 Settings (org.opensearch.common.settings.Settings)2 Builder (org.opensearch.common.settings.Settings.Builder)2 AbstractQueryBuilder (org.opensearch.index.query.AbstractQueryBuilder)2 MatchAllQueryBuilder (org.opensearch.index.query.MatchAllQueryBuilder)2 RandomScoreFunctionBuilder (org.opensearch.index.query.functionscore.RandomScoreFunctionBuilder)2 SearchHit (org.opensearch.search.SearchHit)2