Search in sources :

Example 26 with Field

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

the class HighlighterSearchIT method testForceSourceWithSourceDisabled.

public void testForceSourceWithSourceDisabled() throws Exception {
    assertAcked(prepareCreate("test").addMapping("type1", jsonBuilder().startObject().startObject("type1").startObject("_source").field("enabled", false).endObject().startObject("properties").startObject("field1").field("type", "text").field("store", true).field("index_options", "offsets").field("term_vector", "with_positions_offsets").endObject().startObject("field2").field("type", "text").endObject().endObject().endObject().endObject()));
    ensureGreen();
    client().prepareIndex("test", "type1").setSource("field1", "The quick brown fox jumps over the lazy dog", "field2", "second field content").get();
    refresh();
    for (String type : UNIFIED_AND_NULL) {
        //works using stored field
        SearchResponse searchResponse = client().prepareSearch("test").setQuery(termQuery("field1", "quick")).highlighter(new HighlightBuilder().field(new Field("field1").preTags("<xxx>").postTags("</xxx>").highlighterType(type))).get();
        assertHighlight(searchResponse, 0, "field1", 0, 1, equalTo("The <xxx>quick</xxx> brown fox jumps over the lazy dog"));
        assertFailures(client().prepareSearch("test").setQuery(termQuery("field1", "quick")).highlighter(new HighlightBuilder().field(new Field("field1").preTags("<xxx>").postTags("</xxx>").highlighterType(type).forceSource(true))), RestStatus.BAD_REQUEST, containsString("source is forced for fields [field1] but type [type1] has disabled _source"));
        SearchSourceBuilder searchSource = SearchSourceBuilder.searchSource().query(termQuery("field1", "quick")).highlighter(highlight().forceSource(true).field("field1").highlighterType(type));
        assertFailures(client().prepareSearch("test").setSource(searchSource), RestStatus.BAD_REQUEST, containsString("source is forced for fields [field1] but type [type1] has disabled _source"));
        searchSource = SearchSourceBuilder.searchSource().query(termQuery("field1", "quick")).highlighter(highlight().forceSource(true).field("field*").highlighterType(type));
        assertFailures(client().prepareSearch("test").setSource(searchSource), RestStatus.BAD_REQUEST, matches("source is forced for fields \\[field\\d, field\\d\\] but type \\[type1\\] has disabled _source"));
    }
}
Also used : Field(org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder.Field) Matchers.containsString(org.hamcrest.Matchers.containsString) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder)

Example 27 with Field

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

the class HighlightBuilderTests method testParsingEmptyStructure.

/**
     * test parsing empty highlight or empty fields blocks
     */
public void testParsingEmptyStructure() throws IOException {
    String highlightElement = "{ }";
    XContentParser parser = createParser(JsonXContent.jsonXContent, highlightElement);
    QueryParseContext context = new QueryParseContext(parser);
    HighlightBuilder highlightBuilder = HighlightBuilder.fromXContent(context);
    assertEquals("expected plain HighlightBuilder", new HighlightBuilder(), highlightBuilder);
    highlightElement = "{ \"fields\" : { } }";
    parser = createParser(JsonXContent.jsonXContent, highlightElement);
    context = new QueryParseContext(parser);
    highlightBuilder = HighlightBuilder.fromXContent(context);
    assertEquals("defining no field should return plain HighlightBuilder", new HighlightBuilder(), highlightBuilder);
    highlightElement = "{ \"fields\" : { \"foo\" : { } } }";
    parser = createParser(JsonXContent.jsonXContent, highlightElement);
    context = new QueryParseContext(parser);
    highlightBuilder = HighlightBuilder.fromXContent(context);
    assertEquals("expected HighlightBuilder with field", new HighlightBuilder().field(new Field("foo")), highlightBuilder);
}
Also used : Field(org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder.Field) QueryParseContext(org.elasticsearch.index.query.QueryParseContext) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 28 with Field

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

the class HighlightBuilderTests method mutate.

/**
     * mutate the given highlighter builder so the returned one is different in one aspect
     */
private static HighlightBuilder mutate(HighlightBuilder original) throws IOException {
    HighlightBuilder mutation = serializedCopy(original);
    if (randomBoolean()) {
        mutateCommonOptions(mutation);
    } else {
        switch(randomIntBetween(0, 2)) {
            // change settings that only exists on top level
            case 0:
                mutation.useExplicitFieldOrder(!original.useExplicitFieldOrder());
                break;
            case 1:
                mutation.encoder(original.encoder() + randomAsciiOfLength(2));
                break;
            case 2:
                if (randomBoolean()) {
                    // add another field
                    mutation.field(new Field(randomAsciiOfLength(10)));
                } else {
                    // change existing fields
                    List<Field> originalFields = original.fields();
                    Field fieldToChange = originalFields.get(randomInt(originalFields.size() - 1));
                    if (randomBoolean()) {
                        fieldToChange.fragmentOffset(randomIntBetween(101, 200));
                    } else {
                        fieldToChange.matchedFields(randomStringArray(5, 10));
                    }
                }
                break;
        }
    }
    return mutation;
}
Also used : Field(org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder.Field)

Aggregations

Field (org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder.Field)28 SearchResponse (org.elasticsearch.action.search.SearchResponse)24 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)22 Matchers.containsString (org.hamcrest.Matchers.containsString)17 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)5 SearchSourceBuilder (org.elasticsearch.search.builder.SearchSourceBuilder)5 IdsQueryBuilder (org.elasticsearch.index.query.IdsQueryBuilder)3 MatchQueryBuilder (org.elasticsearch.index.query.MatchQueryBuilder)3 MultiMatchQueryBuilder (org.elasticsearch.index.query.MultiMatchQueryBuilder)3 FunctionScoreQueryBuilder (org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder)3 SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)2 GeoPoint (org.elasticsearch.common.geo.GeoPoint)2 MatchAllQueryBuilder (org.elasticsearch.index.query.MatchAllQueryBuilder)2 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)2 SearchHit (org.elasticsearch.search.SearchHit)2 HighlightBuilder (org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder)2 HighlightField (org.elasticsearch.search.fetch.subphase.highlight.HighlightField)2 TreeSet (java.util.TreeSet)1 Function (java.util.function.Function)1 Query (org.apache.lucene.search.Query)1