org.elasticsearch.common.xcontent.XContentType Java Examples

The following examples show how to use org.elasticsearch.common.xcontent.XContentType. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ElasticSearchRestDAOV5.java    From conductor with Apache License 2.0 7 votes vote down vote up
private void indexObject(final String index, final String docType, final String docId, final Object doc) {

        byte[] docBytes;
        try {
            docBytes = objectMapper.writeValueAsBytes(doc);
        } catch (JsonProcessingException e) {
            logger.error("Failed to convert {} '{}' to byte string", docType, docId);
            return;
        }

        IndexRequest request = new IndexRequest(index, docType, docId);
        request.source(docBytes, XContentType.JSON);

        if(bulkRequests.get(docType) == null) {
            bulkRequests.put(docType, new BulkRequests(System.currentTimeMillis(), new BulkRequest()));
        }

        bulkRequests.get(docType).getBulkRequest().add(request);
        if (bulkRequests.get(docType).getBulkRequest().numberOfActions() >= this.indexBatchSize) {
            indexBulkRequest(docType);
        }
    }
 
Example #2
Source File: JoinRequestBuilder.java    From elasticsearch-sql with Apache License 2.0 6 votes vote down vote up
@Override
public String explain() {
    try {
        XContentBuilder firstBuilder = XContentFactory.contentBuilder(XContentType.JSON).prettyPrint();
        firstTable.getRequestBuilder().request().source().toXContent(firstBuilder, ToXContent.EMPTY_PARAMS);

        XContentBuilder secondBuilder = XContentFactory.contentBuilder(XContentType.JSON).prettyPrint();
        secondTable.getRequestBuilder().request().source().toXContent(secondBuilder, ToXContent.EMPTY_PARAMS);
        String explained = String.format(" first query:\n%s\n second query:\n%s", BytesReference.bytes(firstBuilder).utf8ToString(), BytesReference.bytes(secondBuilder).utf8ToString());

        return explained;
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}
 
Example #3
Source File: Sql4EsBase.java    From sql4es with Apache License 2.0 6 votes vote down vote up
/**
 * Creates the index with optionally the mapping and a number of docs
 * @param index
 * @param type
 * @param withMapping
 * @param nrDocs
 * @throws IOException
 */
protected void createIndexTypeWithDocs(String index, String type, boolean withMapping, int nrDocs) throws IOException{
	if(withMapping){
		String mapping = AccessController.doPrivileged(new PrivilegedAction<String>(){
			@Override
			public String run() {
				try {
					return new String(Files.readAllBytes(Paths.get("src/test/resources/TestDocumentMapping.json")));
				} catch (IOException e) {
					return null;
				}
			}
		});
		if(mapping == null) throw new IOException("Unable to read TestDocumentMapping.json");
		client().admin().indices().prepareCreate(index).addMapping(type, mapping, XContentType.JSON).get(); //.execute().actionGet();
	}else{
		createIndex(index);
	}
	if(nrDocs > 0) addDocs(index, type, nrDocs);
	refresh();
}
 
Example #4
Source File: BaseTransportCoordinateSearchAction.java    From siren-join with GNU Affero General Public License v3.0 6 votes vote down vote up
protected Tuple<XContentType, Map<String, Object>> parseSource(BytesReference source) {
  // nothing to parse...
  if (source == null || source.length() == 0) {
    return null;
  }

  try {
    Tuple<XContentType, Map<String, Object>> parsedSource = XContentHelper.convertToMap(source, false);
    logger.debug("{}: Parsed source: {}", Thread.currentThread().getName(), parsedSource);
    return parsedSource;
  }
  catch (Throwable e) {
      String sSource = "_na_";
      try {
          sSource = XContentHelper.convertToJson(source, false);
      }
      catch (Throwable e1) { /* ignore  */ }
      throw new ElasticsearchParseException("Failed to parse source [" + sSource + "]", e);
  }
}
 
Example #5
Source File: ESNestedSearchService.java    From search-spring-boot-starter with Apache License 2.0 6 votes vote down vote up
/**
 * 获取新增请求
 *
 * @param esObject 更新请求参数
 * @return UpdateRequestBuilder 更新请求
 */
private IndexRequestBuilder getIndexRequest(SaveESObject esObject) {
    String dataId = getId(esObject.getUkMap());

    byte[] dataBytes = null;
    try {
        dataBytes = OBJECT_MAPPER.writeValueAsBytes(esObject.getDataMap());
    } catch (JsonProcessingException e) {
        // never hapened
        SearchLogger.error("", e);
    }

    IndexRequestBuilder indexRequestBuilder = transportClient.prepareIndex().setIndex(esObject.getIndexName())
            .setType(esObject.getTypeName());
    if (StringUtils.isNotBlank(dataId)) {
        indexRequestBuilder.setId(dataId);
    }
    // TODO 替换
    // indexRequestBuilder.setSource(esObject.getDataMap());
    indexRequestBuilder.setSource(dataBytes, XContentType.JSON);
    return indexRequestBuilder;
}
 
Example #6
Source File: SearchService.java    From jakduk-api with MIT License 6 votes vote down vote up
public void indexDocumentBoardComment(EsComment esComment) {

		String id = esComment.getId();
		String parentBoardId = esComment.getArticle().getId();

		try {
			IndexResponse response = client.prepareIndex()
					.setIndex(elasticsearchProperties.getIndexBoard())
					.setType(Constants.ES_TYPE_COMMENT)
					.setId(id)
					.setParent(parentBoardId)
					.setSource(ObjectMapperUtils.writeValueAsString(esComment), XContentType.JSON)
					.get();

		} catch (IOException e) {
			throw new ServiceException(ServiceError.ELASTICSEARCH_INDEX_FAILED, e.getCause());
		}
	}
 
Example #7
Source File: ElasticsearchNamespaceStore.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public void init() {
	boolean indexExistsAlready = clientProvider.getClient()
			.admin()
			.indices()
			.exists(new IndicesExistsRequest(index))
			.actionGet()
			.isExists();

	if (!indexExistsAlready) {
		CreateIndexRequest request = new CreateIndexRequest(index);
		request.mapping(ELASTICSEARCH_TYPE, MAPPING, XContentType.JSON);
		clientProvider.getClient().admin().indices().create(request).actionGet();
	}

}
 
Example #8
Source File: XContentTestUtilsTests.java    From crate with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
public void testInsertIntoXContent() throws IOException {
    XContentBuilder builder = JsonXContent.contentBuilder();
    builder.startObject();
    builder.endObject();
    builder = XContentTestUtils.insertIntoXContent(XContentType.JSON.xContent(), BytesReference.bytes(builder),
            Collections.singletonList(""), () -> "inn.er1", () -> new HashMap<>());
    builder = XContentTestUtils.insertIntoXContent(XContentType.JSON.xContent(), BytesReference.bytes(builder),
            Collections.singletonList(""), () -> "field1", () -> "value1");
    builder = XContentTestUtils.insertIntoXContent(XContentType.JSON.xContent(), BytesReference.bytes(builder),
            Collections.singletonList("inn\\.er1"), () -> "inner2", () -> new HashMap<>());
    builder = XContentTestUtils.insertIntoXContent(XContentType.JSON.xContent(), BytesReference.bytes(builder),
            Collections.singletonList("inn\\.er1"), () -> "field2", () -> "value2");
    try (XContentParser parser = XContentHelper.createParser(NamedXContentRegistry.EMPTY,
        DeprecationHandler.THROW_UNSUPPORTED_OPERATION, BytesReference.bytes(builder), builder.contentType())) {
        Map<String, Object> map = parser.map();
        assertEquals(2, map.size());
        assertEquals("value1", map.get("field1"));
        assertThat(map.get("inn.er1"), instanceOf(Map.class));
        Map<String, Object> innerMap = (Map<String, Object>) map.get("inn.er1");
        assertEquals(2, innerMap.size());
        assertEquals("value2", innerMap.get("field2"));
        assertThat(innerMap.get("inner2"), instanceOf(Map.class));
        assertEquals(0, ((Map<String, Object>) innerMap.get("inner2")).size());
    }
}
 
Example #9
Source File: ElasticsearchAssertions.java    From crate with Apache License 2.0 6 votes vote down vote up
/**
 * Asserts that the provided {@link BytesReference}s created through
 * {@link org.elasticsearch.common.xcontent.ToXContent#toXContent(XContentBuilder, ToXContent.Params)} hold the same content.
 * The comparison is done by parsing both into a map and comparing those two, so that keys ordering doesn't matter.
 * Also binary values (byte[]) are properly compared through arrays comparisons.
 */
public static void assertToXContentEquivalent(BytesReference expected, BytesReference actual, XContentType xContentType)
        throws IOException {
    //we tried comparing byte per byte, but that didn't fly for a couple of reasons:
    //1) whenever anything goes through a map while parsing, ordering is not preserved, which is perfectly ok
    //2) Jackson SMILE parser parses floats as double, which then get printed out as double (with double precision)
    //Note that byte[] holding binary values need special treatment as they need to be properly compared item per item.
    Map<String, Object> actualMap = null;
    Map<String, Object> expectedMap = null;
    try (XContentParser actualParser = xContentType.xContent()
            .createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, actual.streamInput())) {
        actualMap = actualParser.map();
        try (XContentParser expectedParser = xContentType.xContent()
                .createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, expected.streamInput())) {
            expectedMap = expectedParser.map();
            try {
                assertMapEquals(expectedMap, actualMap);
            } catch (AssertionError error) {
                NotEqualMessageBuilder message = new NotEqualMessageBuilder();
                message.compareMaps(actualMap, expectedMap);
                throw new AssertionError("Error when comparing xContent.\n" + message.toString(), error);
            }
        }
    }
}
 
Example #10
Source File: ElasticBulkService.java    From mongolastic with MIT License 6 votes vote down vote up
@Override
public void proceed(List content) {
    try {
        logger.info("Transferring data began to elasticsearch.");
        final String indexName = config.getMisc().getDindex().getAs();
        final String typeName = config.getMisc().getCtype().getAs();
        for (Object o : content) {
            Document doc = (Document) o;
            Object id = doc.get("_id");
            IndexRequest indexRequest = new IndexRequest(indexName, typeName, String.valueOf(id));
            doc.remove("_id");
            indexRequest.source(doc.toJson(encoder), XContentType.JSON);
            bulkProcessor.add(indexRequest);
        }
    } catch (Exception ex) {
        logger.debug(ex.getMessage(), ex);
    }
}
 
Example #11
Source File: MainTestSuite.java    From elasticsearch-sql with Apache License 2.0 6 votes vote down vote up
private static void prepareAccountsIndex() {
    String dataMapping = "{  \"account\": {" +
            " \"properties\": {\n" +
            "          \"gender\": {\n" +
            "            \"type\": \"text\",\n" +
            "            \"fielddata\": true\n" +
            "          }," +
            "          \"address\": {\n" +
            "            \"type\": \"text\",\n" +
            "            \"fielddata\": true\n" +
            "          }," +
            "          \"state\": {\n" +
            "            \"type\": \"text\",\n" +
            "            \"fielddata\": true\n" +
            "          }" +
            "       }"+
            "   }" +
            "}";
    client.admin().indices().preparePutMapping(TEST_INDEX_ACCOUNT).setType("account").setSource(dataMapping, XContentType.JSON).execute().actionGet();
}
 
Example #12
Source File: DefaultElasticSearchAdminService.java    From vertx-elasticsearch-service with Apache License 2.0 6 votes vote down vote up
@Override
public void putTemplate(String name, JsonObject source, TemplateOptions options, Handler<AsyncResult<Void>> resultHandler) {

    final PutIndexTemplateRequestBuilder builder = new PutIndexTemplateRequestBuilder(service.getClient(), PutIndexTemplateAction.INSTANCE, name)
            .setSource(source.encode().getBytes(Charsets.UTF_8), XContentType.JSON);

    builder.execute(new ActionListener<PutIndexTemplateResponse>() {
        @Override
        public void onResponse(PutIndexTemplateResponse putIndexTemplateResponse) {
            resultHandler.handle(Future.succeededFuture());
        }

        @Override
        public void onFailure(Exception e) {
            resultHandler.handle(Future.failedFuture(e));
        }
    });
}
 
Example #13
Source File: CsvXContentGenerator.java    From elasticsearch-rest-command with The Unlicense 6 votes vote down vote up
@Override
public final void writeRawField(String fieldName, BytesReference content, OutputStream bos) throws IOException {
    XContentType contentType = XContentFactory.xContentType(content);
    if (contentType != null) {
        writeObjectRaw(fieldName, content, bos);
    } else {
        writeFieldName(fieldName);
        // we could potentially optimize this to not rely on exception logic...
        String sValue = content.toUtf8();
        try {
            writeNumber(Long.parseLong(sValue));
        } catch (NumberFormatException e) {
            try {
                writeNumber(Double.parseDouble(sValue));
            } catch (NumberFormatException e1) {
                writeString(sValue);
            }
        }
    }
}
 
Example #14
Source File: GeneratedColsFromRawInsertSource.java    From crate with Apache License 2.0 6 votes vote down vote up
@Override
public Map<String, Object> generateSourceAndCheckConstraints(Object[] values) {
    String rawSource = (String) values[0];
    Map<String, Object> source = XContentHelper.toMap(new BytesArray(rawSource), XContentType.JSON);
    mixinDefaults(source, defaults);
    for (int i = 0; i < expressions.size(); i++) {
        expressions.get(i).setNextRow(source);
    }
    for (Map.Entry<Reference, Input<?>> entry : generatedCols.entrySet()) {
        var reference = entry.getKey();
        var value = entry.getValue().value();
        var valueForInsert = reference
            .valueType()
            .valueForInsert(value);
        source.putIfAbsent(reference.column().fqn(), valueForInsert);
    }
    return source;
}
 
Example #15
Source File: LogSink2ES.java    From flink-learning with Apache License 2.0 6 votes vote down vote up
public static void sink2es(SingleOutputStreamOperator<LogEvent> logDataStream, ParameterTool parameterTool) {
    List<HttpHost> esAddresses;
    try {
         esAddresses = ESSinkUtil.getEsAddresses(parameterTool.get(ELASTICSEARCH_HOSTS));
    } catch (MalformedURLException e) {
        log.error("get es address has an error", e);
        return;
    }
    int bulkSize = parameterTool.getInt(ELASTICSEARCH_BULK_FLUSH_MAX_ACTIONS, 40);
    int sinkParallelism = parameterTool.getInt(STREAM_SINK_PARALLELISM, 5);

    ESSinkUtil.addSink(esAddresses, bulkSize, sinkParallelism, logDataStream,
            (LogEvent logEvent, RuntimeContext runtimeContext, RequestIndexer requestIndexer) -> {
                requestIndexer.add(Requests.indexRequest()
                        .index("zhisheng_log")
                        .type(ZHISHENG)
                        .source(GsonUtil.toJSONBytes(logEvent), XContentType.JSON));
            },
            parameterTool);
}
 
Example #16
Source File: ElasticSearchDAOV6.java    From conductor with Apache License 2.0 6 votes vote down vote up
@Override
public void addMessage(String queue, Message message) {
    try {
        long startTime = Instant.now().toEpochMilli();
        Map<String, Object> doc = new HashMap<>();
        doc.put("messageId", message.getId());
        doc.put("payload", message.getPayload());
        doc.put("queue", queue);
        doc.put("created", System.currentTimeMillis());

        String docType = StringUtils.isBlank(docTypeOverride) ? MSG_DOC_TYPE : docTypeOverride;
        UpdateRequest req = new UpdateRequest(messageIndexName, docType, message.getId());
        req.doc(doc, XContentType.JSON);
        req.upsert(doc, XContentType.JSON);
        indexObject(req, MSG_DOC_TYPE);
        long endTime = Instant.now().toEpochMilli();
        LOGGER.debug("Time taken {} for  indexing message: {}", endTime - startTime, message.getId());
        Monitors.recordESIndexTime("add_message", MSG_DOC_TYPE, endTime - startTime);
    } catch (Exception e) {
        LOGGER.error("Failed to index message: {}", message.getId(), e);
    }
}
 
Example #17
Source File: Job.java    From zentity with Apache License 2.0 6 votes vote down vote up
/**
 * Submit a search query to Elasticsearch.
 *
 * @param indexName The name of the index to search.
 * @param query     The query to search.
 * @return The search response returned by Elasticsearch.
 * @throws IOException
 */
private SearchResponse search(String indexName, String query) throws IOException {
    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    SearchModule searchModule = new SearchModule(Settings.EMPTY, false, Collections.emptyList());
    try (XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(new NamedXContentRegistry(searchModule
            .getNamedXContents()), DeprecationHandler.THROW_UNSUPPORTED_OPERATION, query)) {
        searchSourceBuilder.parseXContent(parser);
    }
    SearchRequestBuilder searchRequestBuilder = new SearchRequestBuilder(client, SearchAction.INSTANCE);
    searchRequestBuilder.setIndices(indexName).setSource(searchSourceBuilder);
    if (this.searchAllowPartialSearchResults != null)
        searchRequestBuilder.setAllowPartialSearchResults(this.searchAllowPartialSearchResults);
    if (this.searchBatchedReduceSize != null)
        searchRequestBuilder.setBatchedReduceSize(this.searchBatchedReduceSize);
    if (this.searchMaxConcurrentShardRequests != null)
        searchRequestBuilder.setMaxConcurrentShardRequests(this.searchMaxConcurrentShardRequests);
    if (this.searchPreFilterShardSize != null)
        searchRequestBuilder.setPreFilterShardSize(this.searchPreFilterShardSize);
    if (this.searchPreference != null)
        searchRequestBuilder.setPreference(this.searchPreference);
    if (this.searchRequestCache != null)
        searchRequestBuilder.setRequestCache(this.searchRequestCache);
    if (this.maxTimePerQuery != null)
        searchRequestBuilder.setTimeout(TimeValue.parseTimeValue(this.maxTimePerQuery, "timeout"));
    return searchRequestBuilder.execute().actionGet();
}
 
Example #18
Source File: Indexer.java    From scava with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Maps document Object to Json and creates and new index request
 * 
 * @param indexName
 *            - index name
 * @param documentType
 *            - document type
 * @param uid
 *            - unique identifier
 * @param object
 *            - object that represents the structure of a document for indexing
 * @return
 * @return IndexResponse
 * @throws IOException
 */
private static void index(String indexName, String documentType, String uid, String document) {

	try {
		
		GetRequest getRequest = new GetRequest(indexName, documentType, uid).fetchSourceContext(fetchSourceContext).storedFields("_none_");
		
		if(highLevelClient.exists(getRequest, getReadHeaders()))
		{
			UpdateRequest request = new UpdateRequest(indexName, documentType, uid);
			request.doc(document, XContentType.JSON);
			logger.info("Document (uid: " + uid + ") has been updated");
		}
		else
		{
			IndexRequest indexRequest = new IndexRequest();
			indexRequest .index(indexName).type(documentType).id(uid).source(document,XContentType.JSON);
				
			logger.info("Document (uid: " + uid + ") has been "	+
										highLevelClient.index(indexRequest, getWriteHeaders()).getResult().toString().toLowerCase());
		}
		
	} catch (IOException io) {
		logger.error("Method index has experienced an IO error\n" + io);
	}
}
 
Example #19
Source File: DlsTest.java    From deprecated-security-advanced-modules with Apache License 2.0 6 votes vote down vote up
@Override
protected void populateData(TransportClient tc) {

    tc.index(new IndexRequest("deals").type("deals").id("0").setRefreshPolicy(RefreshPolicy.IMMEDIATE)
            .source("{\"amount\": 10}", XContentType.JSON)).actionGet();
    tc.index(new IndexRequest("deals").type("deals").id("1").setRefreshPolicy(RefreshPolicy.IMMEDIATE)
            .source("{\"amount\": 1500}", XContentType.JSON)).actionGet();

    try {
        Thread.sleep(3000);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    System.out.println("q");
    System.out.println(Strings.toString(tc.search(new SearchRequest().indices(".opendistro_security")).actionGet()));
    tc.search(new SearchRequest().indices("deals")).actionGet();
}
 
Example #20
Source File: LineContext.java    From crate with Apache License 2.0 5 votes vote down vote up
@Nullable
Map<String, Object> sourceAsMap() {
    if (parsedSource == null) {
        if (rawSource != null) {
            try {
                parsedSource = XContentHelper.toMap(new BytesArray(rawSource), XContentType.JSON);
            } catch (ElasticsearchParseException | NotXContentException e) {
                throw new RuntimeException("JSON parser error: " + e.getMessage(), e);
            }
        }
    }
    return parsedSource;
}
 
Example #21
Source File: PutIndexTemplateRequest.java    From crate with Apache License 2.0 5 votes vote down vote up
/**
 * The settings to create the index template with (either json or yaml format).
 */
public PutIndexTemplateRequest settings(Map<String, Object> source) {
    try {
        XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON);
        builder.map(source);
        settings(Strings.toString(builder), XContentType.JSON);
    } catch (IOException e) {
        throw new ElasticsearchGenerationException("Failed to generate [" + source + "]", e);
    }
    return this;
}
 
Example #22
Source File: ToXContentToBytes.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a {@link org.elasticsearch.common.bytes.BytesReference}
 * containing the {@link ToXContent} output in binary format.
 * Builds the request as the provided <code>contentType</code>
 */
public final BytesReference buildAsBytes(XContentType contentType) {
    try {
        XContentBuilder builder = XContentFactory.contentBuilder(contentType);
        toXContent(builder, ToXContent.EMPTY_PARAMS);
        return builder.bytes();
    } catch (Exception e) {
        throw new ElasticsearchException("Failed to build ToXContent", e);
    }
}
 
Example #23
Source File: ElasticsearchResource.java    From newsleak with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Creates a new elasticsearch index and adds a mapping for the document type.
 * Previously existing indexes will be removed.
 *
 * @throws Exception
 *             the exception
 */
private void createIndex() throws Exception {

	boolean exists = client.admin().indices().prepareExists(mIndex).execute().actionGet().isExists();

	// remove preexisting index
	if (exists) {
		logger.log(Level.INFO, "Preexisting index " + mIndex + " will be removed.");
		DeleteIndexResponse deleteResponse = client.admin().indices().delete(new DeleteIndexRequest(mIndex))
				.actionGet();
		if (deleteResponse.isAcknowledged()) {
			logger.log(Level.INFO, "Preexisting index " + mIndex + " successfully removed.");
			exists = false;
		}
	}

	// create schema mapping from file
	logger.log(Level.INFO, "Index " + mIndex + " will be created.");
	String docMapping = new String(Files.readAllBytes(Paths.get(documentMappingFile)));

	XContentBuilder builder = XContentFactory.jsonBuilder();
	XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(docMapping.getBytes());
	parser.close();
	builder.copyCurrentStructure(parser);

	CreateIndexRequestBuilder createIndexRequestBuilder = client.admin().indices().prepareCreate(mIndex);
	createIndexRequestBuilder.addMapping(DOCUMENT_TYPE, builder);
	createIndexRequestBuilder.execute().actionGet();

}
 
Example #24
Source File: UserApiTest.java    From deprecated-security-advanced-modules with Apache License 2.0 5 votes vote down vote up
@Test
public void testUserApiWithDots() throws Exception {

	setup();

	rh.keystore = "restapi/kirk-keystore.jks";
	rh.sendHTTPClientCertificate = true;

	// initial configuration, 5 users
	HttpResponse response = rh
			.executeGetRequest("_opendistro/_security/api/" + CType.INTERNALUSERS.toLCString());
	Assert.assertEquals(HttpStatus.SC_OK, response.getStatusCode());
	Settings settings = Settings.builder().loadFromSource(response.getBody(), XContentType.JSON).build();
	Assert.assertEquals(35, settings.size());

	addUserWithPassword(".my.dotuser0", "$2a$12$n5nubfWATfQjSYHiWtUyeOxMIxFInUHOAx8VMmGmxFNPGpaBmeB.m",
			HttpStatus.SC_CREATED);

	addUserWithPassword(".my.dot.user0", "12345678",
			HttpStatus.SC_CREATED);

	addUserWithHash(".my.dotuser1", "$2a$12$n5nubfWATfQjSYHiWtUyeOxMIxFInUHOAx8VMmGmxFNPGpaBmeB.m",
			HttpStatus.SC_CREATED);

	addUserWithPassword(".my.dot.user2", "12345678",
			HttpStatus.SC_CREATED);

}
 
Example #25
Source File: ElasticsearchUtil.java    From adaptive-alerting with Apache License 2.0 5 votes vote down vote up
public IndexResponse index(IndexRequest indexRequest, String json) {
    try {
        indexRequest.source(json, XContentType.JSON);
        return legacyElasticSearchClient.index(indexRequest, RequestOptions.DEFAULT);
    } catch (IOException e) {
        log.error(String.format("Indexing mapping %s failed", json), e);
        throw new RuntimeException(e);
    }
}
 
Example #26
Source File: ElasticsearchTemplate.java    From summerframework with Apache License 2.0 5 votes vote down vote up
public int addBatchData(ESBasicInfo esBasicInfo, Object object) throws IOException {
    BulkRequestBuilder bulkRequest = esClient.prepareBulk();

    for (String id : esBasicInfo.getIds()) {
        bulkRequest.add(esClient.prepareIndex(esBasicInfo.getIndex(), esBasicInfo.getType(), id)
            .setSource(mapper.writeValueAsString(object), XContentType.JSON));
    }
    bulkRequest.execute().actionGet();

    return bulkRequest.numberOfActions();
}
 
Example #27
Source File: MainTestSuite.java    From elasticsearch-sql with Apache License 2.0 5 votes vote down vote up
private static void prepareDogsIndex() {
    String dataMapping = "{  \"dog\": {" +
            " \"properties\": {\n" +
            "          \"dog_name\": {\n" +
            "            \"type\": \"text\",\n" +
            "            \"fielddata\": true\n" +
            "          }"+
            "       }"+
            "   }" +
            "}";
    client.admin().indices().preparePutMapping(TEST_INDEX_DOG).setType("dog").setSource(dataMapping, XContentType.JSON).execute().actionGet();
}
 
Example #28
Source File: ElasticsearchWebClient.java    From staccato with Apache License 2.0 5 votes vote down vote up
/**
 * Utility method to deserialize responses from Elasticsearch into the response objects provided by Elasticsearch's
 * Java library.
 *
 * @param bytes The byte array response from the Elasticsearch request
 * @param clazz Class class for the Elasticsearch response class that the method should attempt to deserialize to
 * @param <T> The Elasticsearch response class that the method should attempt to deserialize to
 * @return The deserialized response
 */
private <T extends ActionResponse> T deserializeResponse(byte[] bytes, Class<T> clazz) {
    try {

        Method fromXContent = clazz.getMethod("fromXContent", XContentParser.class);
        XContentParser parser = XContentFactory
                .xContent(XContentType.JSON)
                .createParser(NamedXContentRegistry.EMPTY, null, bytes);
        return (T)fromXContent.invoke(null, parser);
    } catch (Exception e) {
        log.error("Error deserializing Elasticsearch SearchResponse.", e);
        throw new ItemException("An error occurred processing the result from the database.  Please contact an administrator.");
    }
}
 
Example #29
Source File: ArrayMapperTest.java    From crate with Apache License 2.0 5 votes vote down vote up
@Test
public void testParseNull() throws Exception {
    // @formatter: off
    String mapping = Strings.toString(XContentFactory.jsonBuilder()
        .startObject()
            .startObject(TYPE)
                .startObject("properties")
                    .startObject("array_field")
                        .field("type", ArrayMapper.CONTENT_TYPE)
                        .startObject(ArrayMapper.INNER_TYPE)
                            .field("type", "double")
                        .endObject()
                    .endObject()
                .endObject()
            .endObject()
        .endObject());
    // @formatter: on
    DocumentMapper mapper = mapper(INDEX, mapping);
    BytesReference bytesReference = BytesReference.bytes(XContentFactory.jsonBuilder()
        .startObject()
        .nullField("array_field")
        .endObject());
    SourceToParse sourceToParse = new SourceToParse(INDEX, "abc", bytesReference, XContentType.JSON);
    ParsedDocument parsedDoc = mapper.parse(sourceToParse);
    assertThat(parsedDoc.docs().size(), is(1));
    assertThat(parsedDoc.docs().get(0).getField("array_field"), is(nullValue()));
}
 
Example #30
Source File: AbstractXContentTestCase.java    From crate with Apache License 2.0 5 votes vote down vote up
static BytesReference insertRandomFieldsAndShuffle(BytesReference xContent, XContentType xContentType,
        boolean supportsUnknownFields, String[] shuffleFieldsExceptions, Predicate<String> randomFieldsExcludeFilter,
        CheckedBiFunction<XContent, BytesReference, XContentParser, IOException> createParserFunction) throws IOException {
    BytesReference withRandomFields;
    if (supportsUnknownFields) {
        // add a few random fields to check that the parser is lenient on new fields
        withRandomFields = XContentTestUtils.insertRandomFields(xContentType, xContent, randomFieldsExcludeFilter, random());
    } else {
        withRandomFields = xContent;
    }
    XContentParser parserWithRandonFields = createParserFunction.apply(XContentFactory.xContent(xContentType), withRandomFields);
    return BytesReference.bytes(ESTestCase.shuffleXContent(parserWithRandonFields, false, shuffleFieldsExceptions));
}