Search in sources :

Example 91 with IndexResponse

use of org.elasticsearch.action.index.IndexResponse in project tutorials by eugenp.

the class GeoQueriesTest method givenGeoShapeData_whenExecutedGeoShapeQuery_thenResultNonEmpty.

@Test
public void givenGeoShapeData_whenExecutedGeoShapeQuery_thenResultNonEmpty() {
    String jsonObject = "{\"name\":\"Agra\",\"region\":{\"type\":\"envelope\",\"coordinates\":[[75,25],[80.1,30.2]]}}";
    IndexResponse response = client.prepareIndex(WONDERS_OF_WORLD, WONDERS).setSource(jsonObject).get();
    String tajMahalId = response.getId();
    client.admin().indices().prepareRefresh(WONDERS_OF_WORLD).get();
    QueryBuilder qb = QueryBuilders.geoShapeQuery("region", ShapeBuilder.newEnvelope().topLeft(74.00, 24.0).bottomRight(81.1, 31.2)).relation(ShapeRelation.WITHIN);
    SearchResponse searchResponse = client.prepareSearch(WONDERS_OF_WORLD).setTypes(WONDERS).setQuery(qb).execute().actionGet();
    List<String> ids = Arrays.stream(searchResponse.getHits().getHits()).map(SearchHit::getId).collect(Collectors.toList());
    assertTrue(ids.contains(tajMahalId));
}
Also used : IndexResponse(org.elasticsearch.action.index.IndexResponse) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse) Test(org.junit.Test)

Example 92 with IndexResponse

use of org.elasticsearch.action.index.IndexResponse in project tutorials by eugenp.

the class GeoQueriesTest method givenGeoPointData_whenExecutedGeoBoundingBoxQuery_thenResultNonEmpty.

@Test
public void givenGeoPointData_whenExecutedGeoBoundingBoxQuery_thenResultNonEmpty() {
    String jsonObject = "{\"name\":\"Pyramids of Giza\",\"location\":[31.131302,29.976480]}";
    IndexResponse response = client.prepareIndex(WONDERS_OF_WORLD, WONDERS).setSource(jsonObject).get();
    String pyramidsOfGizaId = response.getId();
    client.admin().indices().prepareRefresh(WONDERS_OF_WORLD).get();
    QueryBuilder qb = QueryBuilders.geoBoundingBoxQuery("location").bottomLeft(28, 30).topRight(31, 32);
    SearchResponse searchResponse = client.prepareSearch(WONDERS_OF_WORLD).setTypes(WONDERS).setQuery(qb).execute().actionGet();
    List<String> ids = Arrays.stream(searchResponse.getHits().getHits()).map(SearchHit::getId).collect(Collectors.toList());
    assertTrue(ids.contains(pyramidsOfGizaId));
}
Also used : IndexResponse(org.elasticsearch.action.index.IndexResponse) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse) Test(org.junit.Test)

Example 93 with IndexResponse

use of org.elasticsearch.action.index.IndexResponse in project arctic-sea by 52North.

the class ElasticsearchAdminHandlerIT method connectTransportMode.

@Test
public void connectTransportMode() throws InterruptedException {
    settings.setNodeConnectionMode(ElasticsearchSettingsKeys.CONNECTION_MODE_TRANSPORT_CLIENT);
    adminHandler.init();
    Map<String, Object> data = new HashMap<>();
    data.put("test", "test-string");
    IndexResponse idx = dataHandler.persist(data);
    Thread.sleep(2000);
    String ret = getEmbeddedClient().prepareGet(idx.getIndex(), idx.getType(), idx.getId()).get().getSourceAsString();
    Assert.assertNotNull(ret);
}
Also used : HashMap(java.util.HashMap) IndexResponse(org.elasticsearch.action.index.IndexResponse) ElasticsearchAwareTest(org.n52.iceland.statistics.basetests.ElasticsearchAwareTest) Test(org.junit.Test)

Example 94 with IndexResponse

use of org.elasticsearch.action.index.IndexResponse in project arctic-sea by 52North.

the class ElasticsearchAdminHandlerIT method connectNodeMode.

@Test
public void connectNodeMode() throws Exception {
    settings.setNodeConnectionMode(ElasticsearchSettingsKeys.CONNECTION_MODE_NODE);
    adminHandler.init();
    Map<String, Object> data = new HashMap<>();
    data.put("test", "test-string");
    IndexResponse idx = dataHandler.persist(data);
    Thread.sleep(2000);
    String ret = getEmbeddedClient().prepareGet(idx.getIndex(), idx.getType(), idx.getId()).get().getSourceAsString();
    Assert.assertNotNull(ret);
}
Also used : HashMap(java.util.HashMap) IndexResponse(org.elasticsearch.action.index.IndexResponse) ElasticsearchAwareTest(org.n52.iceland.statistics.basetests.ElasticsearchAwareTest) Test(org.junit.Test)

Example 95 with IndexResponse

use of org.elasticsearch.action.index.IndexResponse in project tutorials-java by Artister.

the class BookController method add.

@PostMapping("add/book/novel")
@ResponseBody
public ResponseEntity add(@RequestParam(name = "title") @NotBlank(message = "标题不可以为空") String title, @RequestParam(name = "author") @NotBlank(message = "作者不可以为空") String author, @RequestParam(name = "word_count") @NotBlank(message = "字数不可以为空") String wordCount, @RequestParam(name = "publish_date") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date publishDate) {
    try {
        XContentBuilder content = XContentFactory.jsonBuilder().startObject().field("title", title).field("author", author).field("word_count", wordCount).field("publish_date", publishDate).endObject();
        IndexResponse response = client.prepareIndex("book", "novel").setSource(content).get();
        return new ResponseEntity(response.getId(), HttpStatus.OK);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}
Also used : ResponseEntity(org.springframework.http.ResponseEntity) IndexResponse(org.elasticsearch.action.index.IndexResponse) IOException(java.io.IOException) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Aggregations

IndexResponse (org.elasticsearch.action.index.IndexResponse)103 Test (org.junit.Test)26 SearchResponse (org.elasticsearch.action.search.SearchResponse)18 IOException (java.io.IOException)15 CreateIndexResponse (org.elasticsearch.action.admin.indices.create.CreateIndexResponse)15 HashMap (java.util.HashMap)13 DeleteResponse (org.elasticsearch.action.delete.DeleteResponse)13 IndexRequest (org.elasticsearch.action.index.IndexRequest)13 ElasticsearchException (org.elasticsearch.ElasticsearchException)11 CountDownLatch (java.util.concurrent.CountDownLatch)10 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)8 BulkResponse (org.elasticsearch.action.bulk.BulkResponse)7 Settings (org.elasticsearch.common.settings.Settings)7 ArrayList (java.util.ArrayList)6 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)6 DeleteIndexResponse (org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse)6 ExecutionException (java.util.concurrent.ExecutionException)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5 AtomicReference (java.util.concurrent.atomic.AtomicReference)5 BulkItemResponse (org.elasticsearch.action.bulk.BulkItemResponse)5