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));
}
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));
}
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);
}
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);
}
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;
}
Aggregations