private void addIndex(String indexName) { try { elasticSearchClient.admin() .indices() .prepareGetIndex() .addIndices(indexName) .execute() .actionGet(); } catch (IndexNotFoundException infe) { try { elasticSearchClient.admin() .indices() .prepareCreate(indexName) .execute() .actionGet(); } catch (ResourceAlreadyExistsException done) { // no-op } } }
public void createIndexIfNotExists(String indexName, final int shards, final int replicas) { // create an index if not existent if (!this.elasticsearchClient.admin().indices().prepareExists(indexName).execute().actionGet().isExists()) { Settings.Builder settings = Settings.builder() .put("number_of_shards", shards) .put("number_of_replicas", replicas); this.elasticsearchClient.admin().indices().prepareCreate(indexName) .setSettings(settings) .setUpdateAllTypes(true) .execute().actionGet(); } else { //LOGGER.debug("Index with name {} already exists", indexName); } }
if (!esClient.admin().indices().prepareExists(definition.getIndexName()).get().isExists()) { esClient.admin().indices().prepareCreate(definition.getIndexName()).get(); logger.debug("Set explicit attachment mapping."); esClient.admin().indices().preparePutMapping(definition.getIndexName()).setType(definition.getTypeName()) .setSource(getGridFSMapping()).get(); } catch (Exception e) {
.setSettings(settings) .get(); if (!indexResponse.isAcknowledged()) { throw new IllegalStateException("Failed to create index " + index.getName());
CreateIndexResponse indexResponse = SHARED_NODE.client().admin().indices() .prepareCreate(index.getName()) .setSettings(settings) .get(); if (!indexResponse.isAcknowledged()) { throw new IllegalStateException("Failed to create index " + index.getName()); SHARED_NODE.client().admin().cluster().prepareHealth(index.getName()).setWaitForStatus(ClusterHealthStatus.YELLOW).get(); PutMappingResponse mappingResponse = SHARED_NODE.client().admin().indices().preparePutMapping(index.getName()) .setType(entry.getKey()) .setSource(entry.getValue().getAttributes())
public void testInvalidTargetField() throws Exception { assertAcked(prepareCreate("index1").addMapping("type", "id", "type=integer", "foreign_key", "type=integer")); assertAcked(prepareCreate("index2").addMapping("type", "id", "type=integer", "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") ); // Invalid target field SearchResponse searchResponse = new CoordinateSearchRequestBuilder(client()).setIndices("index1").setQuery( QueryBuilders.filterJoin("invalid_field").indices("index2").types("type").path("id").query( boolQuery().filter(termQuery("tag", "aaa")) ) ).get(); assertHitCount(searchResponse, 0L); }
Settings settings = Settings.settingsBuilder().put("number_of_shards", 1).build(); assertAcked(prepareCreate("index1").setSettings(settings).addMapping("type1", "id", "type=integer", "foreign_key", "type=integer")); assertAcked(prepareCreate("index2").setSettings(settings).addMapping("type2", "id", "type=integer", "tag", "type=string") .addMapping("type3", "id", "type=integer", "tag", "type=string") .addMapping("type4", "id", "type=integer", "tag", "type=string")); client().prepareIndex("index1", "type1", "1").setSource("id", "1", "foreign_key", new String[]{"1", "3"}), client().prepareIndex("index1", "type1", "2").setSource("id", "2", "foreign_key", new String[]{"3"}), client().prepareIndex("index1", "type1", "3").setSource("id", "3", "foreign_key", new String[]{"4"}), client().prepareIndex("index1", "type1", "4").setSource("id", "4", "foreign_key", new String[]{"4", "6"}), boolQuery().filter( QueryBuilders.filterJoin("foreign_key").indices("index2").types("type2", "type4").path("id").query( termQuery("tag", "aaa") ).execute().actionGet();
@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); }
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(); }
assertAcked(prepareCreate("test").setSettings(indexSettings)); client().prepareIndex("test", "type", "" + i) .setSource(jsonBuilder().startObject() .field("int", i) .field("text", "aaa") .endObject()) .execute().actionGet(); client().prepareIndex("test", "type", "" + i) .setSource(jsonBuilder().startObject() .field("int", i) .field("text", "aaa aaa") .endObject()) .execute().actionGet(); client().admin().indices().prepareRefresh("test").execute().actionGet(); .setQuery(QueryBuilders.termQuery("text", "aaa")) .setOrderBy(TermsByQueryRequest.Ordering.DOC_SCORE) .setMaxTermsPerShard(5)
@SuppressWarnings("unused") protected void createIndex(String indexName, boolean storeSourceData) throws IOException { CreateIndexResponse createResponse = client.admin().indices().prepareCreate(indexName).execute().actionGet(); LOGGER.debug(createResponse.toString()); ClusterHealthResponse health = client.admin().cluster().prepareHealth(indexName) .setWaitForGreenStatus() .execute().actionGet(); LOGGER.debug("Index status: " + health.toString()); if (health.isTimedOut()) { LOGGER.warn("timed out waiting for green index status, for index: " + indexName); } }
IndicesExistsResponse response = client.admin().indices() .exists(new IndicesExistsRequest(indexName)).actionGet(); client.admin().indices().prepareDelete(this.indexName).execute() .actionGet(); forceCreate = true; CreateIndexResponse create = client.admin().indices() .prepareCreate(indexName).execute().actionGet(); try { Thread.sleep(200); indexSettings.put("refresh_interval", -1); this.client.admin().indices().prepareUpdateSettings(this.indexName) .setSettings(indexSettings).execute().actionGet(); this.bulkRequest = this.client.prepareBulk();
@Before public void before() throws IOException { if(!client.admin().indices().prepareExists(INDEX).execute().actionGet().isExists()) { client.admin().indices().prepareCreate(INDEX).execute().actionGet(); } final BulkRequestBuilder bulk = client.prepareBulk(); final JsonSerializer<Person> serializer = mapper.serializer(Person.class); for (int i = 0; i < SIZE; i++) { final String name = UUID.randomUUID().toString(); final IndexRequest request = new IndexRequest(INDEX, TYPE); request.source(serializer.apply(Person.builder().id("").firstname(name).lastname(name).build()), JSON); bulk.add(request); } client.bulk(bulk.request()).actionGet(); flush(INDEX); }
public static void createIndex(String indexName, Client client) throws IOException { IndicesExistsRequest request = new IndicesExistsRequest(indexName); IndicesExistsResponse response = client.admin().indices().exists(request).actionGet(); if (!response.isExists()) { Settings settings = ImmutableSettings.settingsBuilder().put("index.analysis.analyzer.default.type", "keyword").build(); CreateIndexRequestBuilder createIndexRequestBuilder = client.admin().indices().prepareCreate(indexName).setSettings(settings); client.admin().indices().create(createIndexRequestBuilder.request()).actionGet(); } final ClusterHealthRequest clusterHealthRequest = new ClusterHealthRequest(indexName).timeout(TimeValue.timeValueSeconds(10)).waitForYellowStatus(); final ClusterHealthResponse clusterHealth = client.admin().cluster().health(clusterHealthRequest).actionGet(); if (clusterHealth.isTimedOut()) { throw new IOException(clusterHealth.getStatus() + " status returned from cluster '" + client.admin().cluster().toString() + "', index '" + indexName + "'"); } }
@Test public void testSimpleJoinInWrapperQuery() throws Exception { 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") ); String q = QueryBuilders.filterJoin("foreign_key").indices("index2").types("type").path("id").query( boolQuery().filter(termQuery("tag", "aaa")) ).toString(); // Joining index1.foreign_key with index2.id SearchResponse searchResponse = new CoordinateSearchRequestBuilder(client()).setIndices("index1").setQuery( filteredQuery(matchAllQuery(), wrapperQuery(q) ) ).get(); assertHitCount(searchResponse, 3L); assertSearchHits(searchResponse, "1", "3", "4"); }
.string(); assertAcked(prepareCreate("transactions").addMapping("stock", stockMapping, XContentType.JSON)); indexBuilders.add(client().prepareIndex("transactions", "stock", "1").setSource("type", "sale", "amount", 80)); indexBuilders.add(client().prepareIndex("transactions", "stock", "2").setSource("type", "cost", "amount", 10)); indexBuilders.add(client().prepareIndex("transactions", "stock", "3").setSource("type", "cost", "amount", 30)); indexBuilders.add(client().prepareIndex("transactions", "stock", "4").setSource("type", "sale", "amount", 130)); SearchResponse searchResponse = client().prepareSearch("transactions") .setTypes("stock") .setQuery(matchAllQuery()) .setSize(0) .addAggregation(scriptedMetric("profit") .combineScript(new Script(ScriptType.INLINE, "native", "stockaggs_combine", Collections.emptyMap())) .reduceScript(new Script(ScriptType.INLINE, "native", "stockaggs_reduce", Collections.emptyMap()))) .execute().actionGet();
assertAcked(prepareCreate("email").addMapping("email", "id", "type=string", "content_md5", "type=string")); assertAcked(prepareCreate("ciapioutput").addMapping("ciapioutput", "id", "type=string", "content_md5", "type=string")); client().prepareIndex("email", "email", "1").setSource("id", "1", "content_md5", "595b6b645b7d9548c8c462e889267477"), client().prepareIndex("email", "email", "2").setSource("id", "2", "content_md5", "6aa71033aef3f529926fe3840c6c0a7e"), client().prepareIndex("ciapioutput", "ciapioutput", "1").setSource("id", "1", "content_md5", "595b6b645b7d9548c8c462e889267477") ); String q = boolQuery().must( matchAllQuery() ).mustNot( filterJoin("content_md5").indices("ciapioutput").types("ciapioutput").path("content_md5").query( matchAllQuery() ).toString(); String body = "{ \"query\" : " + q + "}";
@Override public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception { LOG.debug("Creating the index {} settings", indexName); String source = IOUtils.toString(indexSettings.getInputStream(), "UTF-8"); CreateIndexRequestBuilder createIndexReq = esClient.admin().indices().prepareCreate(indexName); createIndexReq.setSettings(source, contentType); CreateIndexResponse response = createIndexReq.execute().actionGet(); if (!response.isAcknowledged()) { throw new RuntimeException("The index settings has not been acknowledged"); } esClient.admin().indices().refresh(new RefreshRequest(indexName)).actionGet(); LOG.info("Index {} settings created", indexName); return RepeatStatus.FINISHED; }
private void createIndexIfNeeded(IndexFactory indexFactory) { IndicesExistsResponse response = client.admin().indices().exists(new IndicesExistsRequest(indexFactory.createIndex())).actionGet(); if (response.isExists()) { return; } client.admin().indices().prepareCreate(indexFactory.createIndex()).execute().actionGet(); }
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") ); }