public void createIndexWithMapping(String indexName, String mappingType, String mappingSource) throws IOException { CreateIndexResponse cir = client.admin().indices().prepareCreate(indexName) .addMapping(mappingType, mappingSource) .get(); if (!cir.isAcknowledged()) { throw new IOException("Create index was not acknowledged"); } }
for(Mapping m : Optional.ofNullable(mappings).orElse(new ArrayList<>())) { client.admin().indices().prepareCreate(m.index) .addMapping(m.docType, m.mapping).get();
/** * Create a new index on the singleton node with the provided index settings. */ protected IndexService createIndex(String index, Settings settings, String type, Object... mappings) { CreateIndexRequestBuilder createIndexRequestBuilder = client().admin().indices().prepareCreate(index).setSettings(settings); if (type != null && mappings != null) { createIndexRequestBuilder.addMapping(type, mappings); } return createIndex(index, createIndexRequestBuilder); }
/** * Create a new index on the singleton node with the provided index settings. */ protected IndexService createIndex(String index, Settings settings, String type, XContentBuilder mappings) { CreateIndexRequestBuilder createIndexRequestBuilder = client().admin().indices().prepareCreate(index).setSettings(settings); if (type != null && mappings != null) { createIndexRequestBuilder.addMapping(type, mappings); } return createIndex(index, createIndexRequestBuilder); }
// Index (and mapping) do not exist CreateIndexRequestBuilder createIndexRequestBuilder = client.admin().indices().prepareCreate("indexName"); createIndexRequestBuilder.addMapping("typeName", mapping); response = createIndexRequestBuilder.execute().actionGet(); // Index exists but not mapping PutMappingRequestBuilder preparePutMappingBuilder = client.admin().indices().preparePutMapping("indexName"); preparePutMappingBuilder.setSource(mapping).setType("typeName"); response = preparePutMappingBuilder.execute().actionGet();
/** * Create the .zentity-models index. * * @param client The client that will communicate with Elasticsearch. * @param numberOfShards The value of index.number_of_shards. * @param numberOfReplicas The value of index.number_of_replicas. * @return */ public static CreateIndexResponse createIndex(NodeClient client, int numberOfShards, int numberOfReplicas) { return client.admin().indices().prepareCreate(ModelsAction.INDEX_NAME) .setSettings(Settings.builder() .put("index.number_of_shards", numberOfShards) .put("index.number_of_replicas", numberOfReplicas) ) .addMapping("doc", INDEX_MAPPING, XContentType.JSON) .get(); }
private void setMapping(final String index) throws IOException { // Create index (if needed) final IndicesAdminClient indices = esResource.getClient().admin().indices(); if (!indices.exists(Requests.indicesExistsRequest(index)).actionGet().isExists()) { // We must submit all the mappings at once for parent child indices .prepareCreate(index) .addMapping(documentType, createDocumentMapping(XContentFactory.jsonBuilder())) .addMapping(mentionType, createMentionMapping(XContentFactory.jsonBuilder())) .addMapping(entityType, createEntityMapping(XContentFactory.jsonBuilder())) .addMapping(relationType, createRelationMapping(XContentFactory.jsonBuilder())) .execute() .actionGet(); } }
@Override public void createTable(ConnectorTableMetadata tableMetadata) { XContentBuilder mapping = getMapping(tableMetadata.getColumns()); String index = tableMetadata.getTable().getTableName(); try { //TODO: default type value is presto client.admin().indices().prepareCreate(index) .addMapping("presto", mapping) .execute().actionGet(); } catch (MapperParsingException e) { throw new PrestoException(ES_MAPPING_ERROR, "Failed create index:" + index, e); } }
@Override public void createTable(ConnectorTableMetadata tableMetadata) { XContentBuilder mapping = getMapping(tableMetadata.getColumns()); String index = tableMetadata.getTable().getTableName(); try { //TODO: default type value is presto client.admin().indices().prepareCreate(index) .addMapping("presto", mapping) .execute().actionGet(); } catch (MapperParsingException e) { throw new PrestoException(ES_MAPPING_ERROR, "Failed create index:" + index, e); } }
@Override public void createTable(ConnectorTableMetadata tableMetadata) { XContentBuilder mapping = getMapping(tableMetadata.getColumns()); String index = tableMetadata.getTable().getTableName(); try { //TODO: default type value is presto client.admin().indices().prepareCreate(index) .addMapping("presto", mapping) .execute().actionGet(); } catch (MapperParsingException e) { throw new PrestoException(ES_MAPPING_ERROR, "Failed create index:" + index, e); } }
void initNestedIndexAndPercolation(Client client) throws IOException, ExecutionException, InterruptedException { XContentBuilder mapping = XContentFactory.jsonBuilder(); mapping.startObject().startObject("properties").startObject("companyname").field("type", "string").endObject() .startObject("employee").field("type", "nested").startObject("properties") .startObject("name").field("type", "string").endObject().endObject().endObject().endObject() .endObject(); client.admin().indices().prepareCreate("nestedindex").addMapping("company", mapping).execute().actionGet(); waitForYellowStatus(client); client.prepareIndex("nestedindex", BatchPercolatorService.TYPE_NAME, "Q").setSource(jsonBuilder().startObject() .field("query", QueryBuilders.nestedQuery("employee", QueryBuilders.matchQuery("employee.name", "virginia potts").operator(MatchQueryBuilder.Operator.AND)).scoreMode("avg")).endObject()).get(); client.admin().indices().prepareRefresh().execute().actionGet(); }
@Test public void testUkrainianAnalyzerInMapping() throws IOException { final XContentBuilder mapping = jsonBuilder().startObject() .startObject("type") .startObject("properties") .startObject("foo") .field("type", "string") .field("analyzer", "ukrainian") .endObject() .endObject() .endObject() .endObject(); client().admin().indices().prepareCreate("test").addMapping("type", mapping).get(); index("test", "type", "1", "foo", "б'ючи іменинника"); ensureYellow(); }
@Test public void testUpdateDelete() throws Exception { assertAcked(prepareCreate("index1").addMapping("type", "id", "type=integer")); GetIndicesVersionResponse rsp = new GetIndicesVersionRequestBuilder(client(), GetIndicesVersionAction.INSTANCE).get(); long version = rsp.getVersion(); indexRandom(true, client().prepareIndex("index1", "type", "1").setSource("id", "1")); rsp = new GetIndicesVersionRequestBuilder(client(), GetIndicesVersionAction.INSTANCE).get(); assertThat(rsp.getVersion(), is(not(equalTo(version)))); version = rsp.getVersion(); client().prepareDelete("index1", "type", "1").execute().get(); rsp = new GetIndicesVersionRequestBuilder(client(), GetIndicesVersionAction.INSTANCE).get(); assertThat(rsp.getVersion(), is(not(equalTo(version)))); version = rsp.getVersion(); rsp = new GetIndicesVersionRequestBuilder(client(), GetIndicesVersionAction.INSTANCE).get(); assertThat(rsp.getVersion(), is(equalTo(version))); }
@Before public void setup() throws ExecutionException, InterruptedException { Settings.Builder builder = Settings.builder().put("index.number_of_shards", 1); builder.put("index.number_of_replicas", 0); assertAcked(prepareCreate("index1").addMapping("type", "id", "type=integer", "foreign_key", "type=integer").setSettings(builder)); assertAcked(prepareCreate("index2").addMapping("type", "id", "type=integer", "tag", "type=string").setSettings(builder)); ensureGreen(); indexRandom(true, client().prepareIndex("index1", "type", "1").setSource("id", "1", "foreign_key", new String[]{"1", "3"}), client().prepareIndex("index1", "type", "2").setSource("id", "2"), client().prepareIndex("index1", "type", "3").setSource("id", "3", "foreign_key", new String[]{"2"}), client().prepareIndex("index1", "type", "4").setSource("id", "4", "foreign_key", new String[]{"1", "4"}), client().prepareIndex("index2", "type", "1").setSource("id", "1", "tag", "aaa"), client().prepareIndex("index2", "type", "2").setSource("id", "2", "tag", "aaa"), client().prepareIndex("index2", "type", "3").setSource("id", "3", "tag", "bbb"), client().prepareIndex("index2", "type", "4").setSource("id", "4", "tag", "ccc")); }
private void loadData() throws ExecutionException, InterruptedException { assertAcked(prepareCreate("index1").addMapping("type", "id", "type=string", "foreign_key", "type=string")); assertAcked(prepareCreate("index2").addMapping("type", "id", "type=string", "tag", "type=string")); ensureGreen(); indexRandom(true, client().prepareIndex("index1", "type", "1").setSource("id", "1", "foreign_key", new String[]{"1", "3"}), client().prepareIndex("index1", "type", "2").setSource("id", "2"), client().prepareIndex("index1", "type", "3").setSource("id", "3", "foreign_key", new String[]{"2"}), client().prepareIndex("index1", "type", "4").setSource("id", "4", "foreign_key", new String[]{"1", "4"}), client().prepareIndex("index2", "type", "1").setSource("id", "1", "tag", "aaa"), client().prepareIndex("index2", "type", "2").setSource("id", "2", "tag", "aaa"), client().prepareIndex("index2", "type", "3").setSource("id", "3", "tag", "bbb"), client().prepareIndex("index2", "type", "4").setSource("id", "4", "tag", "ccc") ); }
private void loadData() throws ExecutionException, InterruptedException { assertAcked(prepareCreate("index1").addMapping("type", "id", "type=string", "foreign_key", "type=string")); assertAcked(prepareCreate("index2").addMapping("type", "id", "type=string", "tag", "type=string")); ensureGreen(); indexRandom(true, client().prepareIndex("index1", "type", "1").setSource("id", "1", "foreign_key", new String[]{"1", "3"}), client().prepareIndex("index1", "type", "2").setSource("id", "2"), client().prepareIndex("index1", "type", "3").setSource("id", "3", "foreign_key", new String[]{"2"}), client().prepareIndex("index1", "type", "4").setSource("id", "4", "foreign_key", new String[]{"1", "4"}), client().prepareIndex("index2", "type", "1").setSource("id", "1", "tag", "aaa"), client().prepareIndex("index2", "type", "2").setSource("id", "2", "tag", "aaa"), client().prepareIndex("index2", "type", "3").setSource("id", "3", "tag", "bbb"), client().prepareIndex("index2", "type", "4").setSource("id", "4", "tag", "ccc") ); }
private void loadData() throws ExecutionException, InterruptedException { assertAcked(prepareCreate("index1").addMapping("type", "id", "type=string", "foreign_key", "type=string")); assertAcked(prepareCreate("index2").addMapping("type", "id", "type=string", "tag", "type=string")); ensureGreen(); indexRandom(true, client().prepareIndex("index1", "type", "1").setSource("id", "1", "foreign_key", new String[]{"1", "3"}), client().prepareIndex("index1", "type", "2").setSource("id", "2"), client().prepareIndex("index1", "type", "3").setSource("id", "3", "foreign_key", new String[]{"2"}), client().prepareIndex("index1", "type", "4").setSource("id", "4", "foreign_key", new String[]{"1", "4"}), client().prepareIndex("index2", "type", "1").setSource("id", "1", "tag", "aaa"), client().prepareIndex("index2", "type", "2").setSource("id", "2", "tag", "aaa"), client().prepareIndex("index2", "type", "3").setSource("id", "3", "tag", "bbb"), client().prepareIndex("index2", "type", "4").setSource("id", "4", "tag", "ccc") ); }
@Test public void testSimpleFilter() throws Exception { assertAcked(prepareCreate("index1").addMapping("type", "id", "type=integer")); ensureGreen(); indexRandom(true, client().prepareIndex("index1", "type", "1").setSource("id", "1"), client().prepareIndex("index1", "type", "3").setSource("id", "3"), client().prepareIndex("index1", "type", "7").setSource("id", "7")); SearchResponse searchResponse = client().prepareSearch("index1").setQuery( boolQuery().filter(fieldDataTermsQuery("id", new long[] { 1, 2, 4, 8, 10, 7, 6, 11, 5 }, CACHE_KEY)) ).get(); assertHitCount(searchResponse, 2L); }
@Test public void testLongFilter() throws Exception { assertAcked(prepareCreate("index1").addMapping("type", "id", "type=long")); ensureGreen(); indexRandom(true, client().prepareIndex("index1", "type", "1").setSource("id", "1"), client().prepareIndex("index1", "type", "3").setSource("id", "3"), client().prepareIndex("index1", "type", "7").setSource("id", "7")); SearchResponse searchResponse = client().prepareSearch("index1").setQuery( boolQuery().filter(termsEnumTermsQuery("id", toBytesRefs(new long[] {1, 2, 4, 8, 10, 7, 6, 11, 5}), CACHE_KEY)) ).get(); assertHitCount(searchResponse, 2L); }
@Test public void testStringFilter() throws Exception { assertAcked(prepareCreate("index1").addMapping("type", "id", "type=string")); ensureGreen(); indexRandom(true, client().prepareIndex("index1", "type", "1").setSource("id", "1"), client().prepareIndex("index1", "type", "3").setSource("id", "3"), client().prepareIndex("index1", "type", "7").setSource("id", "7")); SearchResponse searchResponse = client().prepareSearch("index1").setQuery( boolQuery().filter(termsEnumTermsQuery("id", new BytesRef[] { new BytesRef("1"), new BytesRef("2"), new BytesRef("4"), new BytesRef("8"), new BytesRef("10"), new BytesRef("7"), new BytesRef("6"), new BytesRef("11"), new BytesRef("5") }, CACHE_KEY)) ).get(); assertHitCount(searchResponse, 2L); }