Search in sources :

Example 31 with XContentBuilder

use of org.elasticsearch.common.xcontent.XContentBuilder in project elasticsearch by elastic.

the class GetTermVectorsIT method testExistingFieldButNotInDocNPE.

public void testExistingFieldButNotInDocNPE() throws Exception {
    XContentBuilder mapping = jsonBuilder().startObject().startObject("type1").startObject("properties").startObject("existingfield").field("type", "text").field("term_vector", "with_positions_offsets_payloads").endObject().endObject().endObject().endObject();
    assertAcked(prepareCreate("test").addAlias(new Alias("alias")).addMapping("type1", mapping));
    // when indexing a field that simply has a question mark, the term vectors will be null
    client().prepareIndex("test", "type1", "0").setSource("anotherexistingfield", 1).execute().actionGet();
    refresh();
    ActionFuture<TermVectorsResponse> termVectors = client().termVectors(new TermVectorsRequest(indexOrAlias(), "type1", "0").selectedFields(randomBoolean() ? new String[] { "existingfield" } : null).termStatistics(true).fieldStatistics(true));
    // lets see if the null term vectors are caught...
    TermVectorsResponse actionGet = termVectors.actionGet();
    assertThat(actionGet, notNullValue());
    assertThat(actionGet.isExists(), equalTo(true));
    assertThat(actionGet.getIndex(), equalTo("test"));
    assertThat(actionGet.getFields().terms("existingfield"), nullValue());
}
Also used : Alias(org.elasticsearch.action.admin.indices.alias.Alias) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Example 32 with XContentBuilder

use of org.elasticsearch.common.xcontent.XContentBuilder in project elasticsearch by elastic.

the class GetTermVectorsIT method testPerFieldAnalyzer.

public void testPerFieldAnalyzer() throws IOException {
    int numFields = 25;
    // setup mapping and document source
    Set<String> withTermVectors = new HashSet<>();
    XContentBuilder mapping = jsonBuilder().startObject().startObject("type1").startObject("properties");
    XContentBuilder source = jsonBuilder().startObject();
    for (int i = 0; i < numFields; i++) {
        String fieldName = "field" + i;
        if (randomBoolean()) {
            withTermVectors.add(fieldName);
        }
        mapping.startObject(fieldName).field("type", "text").field("term_vector", withTermVectors.contains(fieldName) ? "yes" : "no").endObject();
        source.field(fieldName, "some text here");
    }
    source.endObject();
    mapping.endObject().endObject().endObject();
    // setup indices with mapping
    Settings.Builder settings = Settings.builder().put(indexSettings()).put("index.analysis.analyzer", "standard");
    assertAcked(prepareCreate("test").addAlias(new Alias("alias")).setSettings(settings).addMapping("type1", mapping));
    ensureGreen();
    // index a single document with prepared source
    client().prepareIndex("test", "type1", "0").setSource(source).get();
    refresh();
    // create random per_field_analyzer and selected fields
    Map<String, String> perFieldAnalyzer = new HashMap<>();
    Set<String> selectedFields = new HashSet<>();
    for (int i = 0; i < numFields; i++) {
        if (randomBoolean()) {
            perFieldAnalyzer.put("field" + i, "keyword");
        }
        if (randomBoolean()) {
            perFieldAnalyzer.put("non_existing" + i, "keyword");
        }
        if (randomBoolean()) {
            selectedFields.add("field" + i);
        }
        if (randomBoolean()) {
            selectedFields.add("non_existing" + i);
        }
    }
    // selected fields not specified
    TermVectorsResponse response = client().prepareTermVectors(indexOrAlias(), "type1", "0").setPerFieldAnalyzer(perFieldAnalyzer).get();
    // should return all fields that have terms vectors, some with overridden analyzer
    checkAnalyzedFields(response.getFields(), withTermVectors, perFieldAnalyzer);
    // selected fields specified including some not in the mapping
    response = client().prepareTermVectors(indexOrAlias(), "type1", "0").setSelectedFields(selectedFields.toArray(Strings.EMPTY_ARRAY)).setPerFieldAnalyzer(perFieldAnalyzer).get();
    // should return only the specified valid fields, with some with overridden analyzer
    checkAnalyzedFields(response.getFields(), selectedFields, perFieldAnalyzer);
}
Also used : HashMap(java.util.HashMap) ObjectIntHashMap(com.carrotsearch.hppc.ObjectIntHashMap) Alias(org.elasticsearch.action.admin.indices.alias.Alias) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) Settings(org.elasticsearch.common.settings.Settings) HashSet(java.util.HashSet)

Example 33 with XContentBuilder

use of org.elasticsearch.common.xcontent.XContentBuilder in project elasticsearch by elastic.

the class MetaDataTests method testXContentWithIndexGraveyard.

public void testXContentWithIndexGraveyard() throws IOException {
    final IndexGraveyard graveyard = IndexGraveyardTests.createRandom();
    final MetaData originalMeta = MetaData.builder().indexGraveyard(graveyard).build();
    final XContentBuilder builder = JsonXContent.contentBuilder();
    builder.startObject();
    originalMeta.toXContent(builder, ToXContent.EMPTY_PARAMS);
    builder.endObject();
    XContentParser parser = createParser(JsonXContent.jsonXContent, builder.bytes());
    final MetaData fromXContentMeta = MetaData.fromXContent(parser);
    assertThat(fromXContentMeta.indexGraveyard(), equalTo(originalMeta.indexGraveyard()));
}
Also used : XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 34 with XContentBuilder

use of org.elasticsearch.common.xcontent.XContentBuilder in project elasticsearch by elastic.

the class BytesRestResponseTests method testNoErrorFromXContent.

public void testNoErrorFromXContent() throws IOException {
    IllegalStateException e = expectThrows(IllegalStateException.class, () -> {
        try (XContentBuilder builder = XContentBuilder.builder(randomFrom(XContentType.values()).xContent())) {
            builder.startObject();
            builder.field("status", randomFrom(RestStatus.values()).getStatus());
            builder.endObject();
            try (XContentParser parser = createParser(builder.contentType().xContent(), builder.bytes())) {
                BytesRestResponse.errorFromXContent(parser);
            }
        }
    });
    assertEquals("Failed to parse elasticsearch status exception: no exception was found", e.getMessage());
}
Also used : XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 35 with XContentBuilder

use of org.elasticsearch.common.xcontent.XContentBuilder in project elasticsearch by elastic.

the class RepositoryDataTests method testXContent.

public void testXContent() throws IOException {
    RepositoryData repositoryData = generateRandomRepoData();
    XContentBuilder builder = JsonXContent.contentBuilder();
    repositoryData.snapshotsToXContent(builder, ToXContent.EMPTY_PARAMS);
    XContentParser parser = createParser(JsonXContent.jsonXContent, builder.bytes());
    long gen = (long) randomIntBetween(0, 500);
    RepositoryData fromXContent = RepositoryData.snapshotsFromXContent(parser, gen);
    assertEquals(repositoryData, fromXContent);
    assertEquals(gen, fromXContent.getGenId());
}
Also used : XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Aggregations

XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)619 IOException (java.io.IOException)127 XContentParser (org.elasticsearch.common.xcontent.XContentParser)122 Settings (org.elasticsearch.common.settings.Settings)60 ArrayList (java.util.ArrayList)59 SearchResponse (org.elasticsearch.action.search.SearchResponse)56 Matchers.containsString (org.hamcrest.Matchers.containsString)53 HashMap (java.util.HashMap)47 CompressedXContent (org.elasticsearch.common.compress.CompressedXContent)43 Map (java.util.Map)41 RestRequest (org.elasticsearch.rest.RestRequest)37 IndexRequestBuilder (org.elasticsearch.action.index.IndexRequestBuilder)36 BytesRestResponse (org.elasticsearch.rest.BytesRestResponse)35 Test (org.junit.Test)34 RestController (org.elasticsearch.rest.RestController)33 NodeClient (org.elasticsearch.client.node.NodeClient)32 BaseRestHandler (org.elasticsearch.rest.BaseRestHandler)32 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)32 GeoPoint (org.elasticsearch.common.geo.GeoPoint)31 CrateUnitTest (io.crate.test.integration.CrateUnitTest)28