Search in sources :

Example 36 with XContentBuilder

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

the class IndexLookupIT method testAllExceptPosAndOffset.

public void testAllExceptPosAndOffset() throws Exception {
    XContentBuilder mapping = XContentFactory.jsonBuilder().startObject().startObject("type1").startObject("properties").startObject("float_payload_field").field("type", "text").field("index_options", "offsets").field("term_vector", "no").field("analyzer", "payload_float").endObject().startObject("string_payload_field").field("type", "text").field("index_options", "offsets").field("term_vector", "no").field("analyzer", "payload_string").endObject().startObject("int_payload_field").field("type", "text").field("index_options", "offsets").field("analyzer", "payload_int").endObject().endObject().endObject().endObject();
    assertAcked(prepareCreate("test").addMapping("type1", mapping).setSettings(Settings.builder().put(indexSettings()).put("index.analysis.analyzer.payload_float.tokenizer", "whitespace").putArray("index.analysis.analyzer.payload_float.filter", "delimited_float").put("index.analysis.filter.delimited_float.delimiter", "|").put("index.analysis.filter.delimited_float.encoding", "float").put("index.analysis.filter.delimited_float.type", "delimited_payload_filter").put("index.analysis.analyzer.payload_string.tokenizer", "whitespace").putArray("index.analysis.analyzer.payload_string.filter", "delimited_string").put("index.analysis.filter.delimited_string.delimiter", "|").put("index.analysis.filter.delimited_string.encoding", "identity").put("index.analysis.filter.delimited_string.type", "delimited_payload_filter").put("index.analysis.analyzer.payload_int.tokenizer", "whitespace").putArray("index.analysis.analyzer.payload_int.filter", "delimited_int").put("index.analysis.filter.delimited_int.delimiter", "|").put("index.analysis.filter.delimited_int.encoding", "int").put("index.analysis.filter.delimited_int.type", "delimited_payload_filter").put("index.number_of_shards", 1)));
    indexRandom(true, client().prepareIndex("test", "type1", "1").setSource("float_payload_field", "a|1 b|2 a|3 b "), client().prepareIndex("test", "type1", "2").setSource("string_payload_field", "a|a b|b a|a b "), client().prepareIndex("test", "type1", "3").setSource("float_payload_field", "a|4 b|5 a|6 b "), client().prepareIndex("test", "type1", "4").setSource("string_payload_field", "a|b b|a a|b b "), client().prepareIndex("test", "type1", "5").setSource("float_payload_field", "c "), client().prepareIndex("test", "type1", "6").setSource("int_payload_field", "c|1"));
    // get the number of all docs
    Script script = createScript("_index.numDocs()");
    checkValueInEachDoc(6, script, 6);
    // get the number of docs with field float_payload_field
    script = createScript("_index['float_payload_field'].docCount()");
    checkValueInEachDoc(3, script, 6);
    // corner case: what if the field does not exist?
    script = createScript("_index['non_existent_field'].docCount()");
    checkValueInEachDoc(0, script, 6);
    // get the number of all tokens in all docs
    script = createScript("_index['float_payload_field'].sumttf()");
    checkValueInEachDoc(9, script, 6);
    // corner case get the number of all tokens in all docs for non existent
    // field
    script = createScript("_index['non_existent_field'].sumttf()");
    checkValueInEachDoc(0, script, 6);
    // get the sum of doc freqs in all docs
    script = createScript("_index['float_payload_field'].sumdf()");
    checkValueInEachDoc(5, script, 6);
    // get the sum of doc freqs in all docs for non existent field
    script = createScript("_index['non_existent_field'].sumdf()");
    checkValueInEachDoc(0, script, 6);
    // check term frequencies for 'a'
    script = createScript("term = _index['float_payload_field']['a']; if (term != null) {term.tf()}");
    Map<String, Object> expectedResults = new HashMap<>();
    expectedResults.put("1", 2);
    expectedResults.put("2", 0);
    expectedResults.put("3", 2);
    expectedResults.put("4", 0);
    expectedResults.put("5", 0);
    expectedResults.put("6", 0);
    checkValueInEachDoc(script, expectedResults, 6);
    expectedResults.clear();
    // check doc frequencies for 'c'
    script = createScript("term = _index['float_payload_field']['c']; if (term != null) {term.df()}");
    expectedResults.put("1", 1L);
    expectedResults.put("2", 1L);
    expectedResults.put("3", 1L);
    expectedResults.put("4", 1L);
    expectedResults.put("5", 1L);
    expectedResults.put("6", 1L);
    checkValueInEachDoc(script, expectedResults, 6);
    expectedResults.clear();
    // check doc frequencies for term that does not exist
    script = createScript("term = _index['float_payload_field']['non_existent_term']; if (term != null) {term.df()}");
    expectedResults.put("1", 0L);
    expectedResults.put("2", 0L);
    expectedResults.put("3", 0L);
    expectedResults.put("4", 0L);
    expectedResults.put("5", 0L);
    expectedResults.put("6", 0L);
    checkValueInEachDoc(script, expectedResults, 6);
    expectedResults.clear();
    // check doc frequencies for term that does not exist
    script = createScript("term = _index['non_existent_field']['non_existent_term']; if (term != null) {term.tf()}");
    expectedResults.put("1", 0);
    expectedResults.put("2", 0);
    expectedResults.put("3", 0);
    expectedResults.put("4", 0);
    expectedResults.put("5", 0);
    expectedResults.put("6", 0);
    checkValueInEachDoc(script, expectedResults, 6);
    expectedResults.clear();
    // check total term frequencies for 'a'
    script = createScript("term = _index['float_payload_field']['a']; if (term != null) {term.ttf()}");
    expectedResults.put("1", 4L);
    expectedResults.put("2", 4L);
    expectedResults.put("3", 4L);
    expectedResults.put("4", 4L);
    expectedResults.put("5", 4L);
    expectedResults.put("6", 4L);
    checkValueInEachDoc(script, expectedResults, 6);
    expectedResults.clear();
    // check float payload for 'b'
    HashMap<String, List<Object>> expectedPayloadsArray = new HashMap<>();
    script = createPositionsArrayScript("float_payload_field", "b", INCLUDE_ALL, "payloadAsFloat(-1)");
    float missingValue = -1;
    List<Object> payloadsFor1 = new ArrayList<>();
    payloadsFor1.add(2f);
    payloadsFor1.add(missingValue);
    expectedPayloadsArray.put("1", payloadsFor1);
    List<Object> payloadsFor2 = new ArrayList<>();
    payloadsFor2.add(5f);
    payloadsFor2.add(missingValue);
    expectedPayloadsArray.put("3", payloadsFor2);
    expectedPayloadsArray.put("6", new ArrayList<>());
    expectedPayloadsArray.put("5", new ArrayList<>());
    expectedPayloadsArray.put("4", new ArrayList<>());
    expectedPayloadsArray.put("2", new ArrayList<>());
    checkArrayValsInEachDoc(script, expectedPayloadsArray, 6);
    // check string payload for 'b'
    expectedPayloadsArray.clear();
    payloadsFor1.clear();
    payloadsFor2.clear();
    script = createPositionsArrayScript("string_payload_field", "b", INCLUDE_ALL, "payloadAsString()");
    payloadsFor1.add("b");
    payloadsFor1.add(null);
    expectedPayloadsArray.put("2", payloadsFor1);
    payloadsFor2.add("a");
    payloadsFor2.add(null);
    expectedPayloadsArray.put("4", payloadsFor2);
    expectedPayloadsArray.put("6", new ArrayList<>());
    expectedPayloadsArray.put("5", new ArrayList<>());
    expectedPayloadsArray.put("3", new ArrayList<>());
    expectedPayloadsArray.put("1", new ArrayList<>());
    checkArrayValsInEachDoc(script, expectedPayloadsArray, 6);
    // check int payload for 'c'
    expectedPayloadsArray.clear();
    payloadsFor1.clear();
    payloadsFor2.clear();
    script = createPositionsArrayScript("int_payload_field", "c", INCLUDE_ALL, "payloadAsInt(-1)");
    payloadsFor1 = new ArrayList<>();
    payloadsFor1.add(1);
    expectedPayloadsArray.put("6", payloadsFor1);
    expectedPayloadsArray.put("5", new ArrayList<>());
    expectedPayloadsArray.put("4", new ArrayList<>());
    expectedPayloadsArray.put("3", new ArrayList<>());
    expectedPayloadsArray.put("2", new ArrayList<>());
    expectedPayloadsArray.put("1", new ArrayList<>());
    checkArrayValsInEachDoc(script, expectedPayloadsArray, 6);
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) Collections.emptyList(java.util.Collections.emptyList) List(java.util.List) Matchers.containsString(org.hamcrest.Matchers.containsString) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Example 37 with XContentBuilder

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

the class RestBuilderListenerTests method testXContentBuilderClosedInBuildResponse.

public void testXContentBuilderClosedInBuildResponse() throws Exception {
    AtomicReference<XContentBuilder> builderAtomicReference = new AtomicReference<>();
    RestBuilderListener<TransportResponse.Empty> builderListener = new RestBuilderListener<Empty>(new FakeRestChannel(new FakeRestRequest(), randomBoolean(), 1)) {

        @Override
        public RestResponse buildResponse(Empty empty, XContentBuilder builder) throws Exception {
            builderAtomicReference.set(builder);
            builder.close();
            return new BytesRestResponse(RestStatus.OK, BytesRestResponse.TEXT_CONTENT_TYPE, BytesArray.EMPTY);
        }
    };
    builderListener.buildResponse(Empty.INSTANCE);
    assertNotNull(builderAtomicReference.get());
    assertTrue(builderAtomicReference.get().generator().isClosed());
}
Also used : Empty(org.elasticsearch.transport.TransportResponse.Empty) BytesRestResponse(org.elasticsearch.rest.BytesRestResponse) AtomicReference(java.util.concurrent.atomic.AtomicReference) FakeRestChannel(org.elasticsearch.test.rest.FakeRestChannel) FakeRestRequest(org.elasticsearch.test.rest.FakeRestRequest) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Example 38 with XContentBuilder

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

the class RestBuilderListenerTests method testXContentBuilderNotClosedInBuildResponseAssertionsEnabled.

public void testXContentBuilderNotClosedInBuildResponseAssertionsEnabled() throws Exception {
    assumeTrue("tests are not being run with assertions", RestBuilderListener.class.desiredAssertionStatus());
    RestBuilderListener<TransportResponse.Empty> builderListener = new RestBuilderListener<Empty>(new FakeRestChannel(new FakeRestRequest(), randomBoolean(), 1)) {

        @Override
        public RestResponse buildResponse(Empty empty, XContentBuilder builder) throws Exception {
            return new BytesRestResponse(RestStatus.OK, BytesRestResponse.TEXT_CONTENT_TYPE, BytesArray.EMPTY);
        }
    };
    AssertionError error = expectThrows(AssertionError.class, () -> builderListener.buildResponse(Empty.INSTANCE));
    assertEquals("callers should ensure the XContentBuilder is closed themselves", error.getMessage());
}
Also used : Empty(org.elasticsearch.transport.TransportResponse.Empty) BytesRestResponse(org.elasticsearch.rest.BytesRestResponse) FakeRestChannel(org.elasticsearch.test.rest.FakeRestChannel) FakeRestRequest(org.elasticsearch.test.rest.FakeRestRequest) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Example 39 with XContentBuilder

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

the class ClusterStatsResponse method toString.

@Override
public String toString() {
    try {
        XContentBuilder builder = XContentFactory.jsonBuilder().prettyPrint();
        builder.startObject();
        toXContent(builder, EMPTY_PARAMS);
        builder.endObject();
        return builder.string();
    } catch (IOException e) {
        return "{ \"error\" : \"" + e.getMessage() + "\"}";
    }
}
Also used : IOException(java.io.IOException) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Example 40 with XContentBuilder

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

the class CreateIndexRequest method mapping.

/**
     * Adds mapping that will be added when the index gets created.
     *
     * @param type   The mapping type
     * @param source The mapping source
     */
@SuppressWarnings("unchecked")
public CreateIndexRequest mapping(String type, Map source) {
    if (mappings.containsKey(type)) {
        throw new IllegalStateException("mappings for type \"" + type + "\" were already defined");
    }
    // wrap it in a type map if its not
    if (source.size() != 1 || !source.containsKey(type)) {
        source = MapBuilder.<String, Object>newMapBuilder().put(type, source).map();
    }
    try {
        XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON);
        builder.map(source);
        return mapping(type, builder);
    } catch (IOException e) {
        throw new ElasticsearchGenerationException("Failed to generate [" + source + "]", e);
    }
}
Also used : IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) ElasticsearchGenerationException(org.elasticsearch.ElasticsearchGenerationException)

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