use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.get.GetResponse in project elasticsearch by elastic.
the class SimpleNestedIT method testSimpleNested.
public void testSimpleNested() throws Exception {
assertAcked(prepareCreate("test").addMapping("type1", "nested1", "type=nested").addMapping("type2", "nested1", "type=nested"));
ensureGreen();
// check on no data, see it works
SearchResponse searchResponse = client().prepareSearch("test").execute().actionGet();
assertThat(searchResponse.getHits().getTotalHits(), equalTo(0L));
searchResponse = client().prepareSearch("test").setQuery(termQuery("n_field1", "n_value1_1")).execute().actionGet();
assertThat(searchResponse.getHits().getTotalHits(), equalTo(0L));
client().prepareIndex("test", "type1", "1").setSource(jsonBuilder().startObject().field("field1", "value1").startArray("nested1").startObject().field("n_field1", "n_value1_1").field("n_field2", "n_value2_1").endObject().startObject().field("n_field1", "n_value1_2").field("n_field2", "n_value2_2").endObject().endArray().endObject()).execute().actionGet();
waitForRelocation(ClusterHealthStatus.GREEN);
// flush, so we fetch it from the index (as see that we filter nested docs)
flush();
GetResponse getResponse = client().prepareGet("test", "type1", "1").get();
assertThat(getResponse.isExists(), equalTo(true));
assertThat(getResponse.getSourceAsBytes(), notNullValue());
// check the numDocs
assertDocumentCount("test", 3);
searchResponse = client().prepareSearch("test").setQuery(termQuery("n_field1", "n_value1_1")).execute().actionGet();
assertThat(searchResponse.getHits().getTotalHits(), equalTo(0L));
// search for something that matches the nested doc, and see that we don't find the nested doc
searchResponse = client().prepareSearch("test").setQuery(matchAllQuery()).get();
assertThat(searchResponse.getHits().getTotalHits(), equalTo(1L));
searchResponse = client().prepareSearch("test").setQuery(termQuery("n_field1", "n_value1_1")).get();
assertThat(searchResponse.getHits().getTotalHits(), equalTo(0L));
// now, do a nested query
searchResponse = client().prepareSearch("test").setQuery(nestedQuery("nested1", termQuery("nested1.n_field1", "n_value1_1"), ScoreMode.Avg)).get();
assertNoFailures(searchResponse);
assertThat(searchResponse.getHits().getTotalHits(), equalTo(1L));
searchResponse = client().prepareSearch("test").setQuery(nestedQuery("nested1", termQuery("nested1.n_field1", "n_value1_1"), ScoreMode.Avg)).setSearchType(SearchType.DFS_QUERY_THEN_FETCH).get();
assertNoFailures(searchResponse);
assertThat(searchResponse.getHits().getTotalHits(), equalTo(1L));
// add another doc, one that would match if it was not nested...
client().prepareIndex("test", "type1", "2").setSource(jsonBuilder().startObject().field("field1", "value1").startArray("nested1").startObject().field("n_field1", "n_value1_1").field("n_field2", "n_value2_2").endObject().startObject().field("n_field1", "n_value1_2").field("n_field2", "n_value2_1").endObject().endArray().endObject()).execute().actionGet();
waitForRelocation(ClusterHealthStatus.GREEN);
// flush, so we fetch it from the index (as see that we filter nested docs)
flush();
assertDocumentCount("test", 6);
searchResponse = client().prepareSearch("test").setQuery(nestedQuery("nested1", boolQuery().must(termQuery("nested1.n_field1", "n_value1_1")).must(termQuery("nested1.n_field2", "n_value2_1")), ScoreMode.Avg)).execute().actionGet();
assertNoFailures(searchResponse);
assertThat(searchResponse.getHits().getTotalHits(), equalTo(1L));
// filter
searchResponse = client().prepareSearch("test").setQuery(boolQuery().must(matchAllQuery()).mustNot(nestedQuery("nested1", boolQuery().must(termQuery("nested1.n_field1", "n_value1_1")).must(termQuery("nested1.n_field2", "n_value2_1")), ScoreMode.Avg))).execute().actionGet();
assertNoFailures(searchResponse);
assertThat(searchResponse.getHits().getTotalHits(), equalTo(1L));
// check with type prefix
searchResponse = client().prepareSearch("test").setQuery(nestedQuery("nested1", boolQuery().must(termQuery("nested1.n_field1", "n_value1_1")).must(termQuery("nested1.n_field2", "n_value2_1")), ScoreMode.Avg)).execute().actionGet();
assertNoFailures(searchResponse);
assertThat(searchResponse.getHits().getTotalHits(), equalTo(1L));
// check delete, so all is gone...
DeleteResponse deleteResponse = client().prepareDelete("test", "type1", "2").execute().actionGet();
assertEquals(DocWriteResponse.Result.DELETED, deleteResponse.getResult());
// flush, so we fetch it from the index (as see that we filter nested docs)
flush();
assertDocumentCount("test", 3);
searchResponse = client().prepareSearch("test").setQuery(nestedQuery("nested1", termQuery("nested1.n_field1", "n_value1_1"), ScoreMode.Avg)).execute().actionGet();
assertNoFailures(searchResponse);
assertThat(searchResponse.getHits().getTotalHits(), equalTo(1L));
searchResponse = client().prepareSearch("test").setTypes("type1", "type2").setQuery(nestedQuery("nested1", termQuery("nested1.n_field1", "n_value1_1"), ScoreMode.Avg)).execute().actionGet();
assertNoFailures(searchResponse);
assertThat(searchResponse.getHits().getTotalHits(), equalTo(1L));
}
use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.get.GetResponse in project graylog2-server by Graylog2.
the class Messages method get.
public ResultMessage get(String messageId, String index) throws DocumentNotFoundException {
final GetRequest request = c.prepareGet(index, IndexMapping.TYPE_MESSAGE, messageId).request();
final GetResponse r = c.get(request).actionGet();
if (!r.isExists()) {
throw new DocumentNotFoundException(index, messageId);
}
return ResultMessage.parseFromSource(r);
}
use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.get.GetResponse in project jena by apache.
the class TextIndexESIT method testDeleteEntity.
@Test
public void testDeleteEntity() {
testAddEntity();
String labelKey = "label";
String labelValue = "this is a sample Label";
//Now Delete the entity
classToTest.deleteEntity(entity("http://example/x3", labelKey, labelValue));
//Try to find it
GetResponse response = transportClient.prepareGet(INDEX_NAME, DOC_TYPE, "http://example/x3").get();
//It Should Exist
Assert.assertTrue(response.isExists());
//But the field value should now be empty
Assert.assertEquals("http://example/x3", response.getId());
Assert.assertTrue(response.getSource().containsKey(labelKey));
Assert.assertEquals(0, ((List<?>) response.getSource().get(labelKey)).size());
}
use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.get.GetResponse in project jena by apache.
the class TextIndexESIT method addEntity.
private GetResponse addEntity(Entity entityToAdd) {
classToTest.addEntity(entityToAdd);
GetResponse response = transportClient.prepareGet(INDEX_NAME, DOC_TYPE, entityToAdd.getId()).get();
Assert.assertNotNull(response);
Assert.assertEquals(entityToAdd.getId(), response.getId());
return response;
}
use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.get.GetResponse in project camel by apache.
the class ElasticsearchGetSearchDeleteExistsUpdateTest method testDeleteWithHeaders.
@Test
public void testDeleteWithHeaders() throws Exception {
//first, INDEX a value
Map<String, String> map = createIndexedData();
Map<String, Object> headers = new HashMap<String, Object>();
headers.put(ElasticsearchConstants.PARAM_OPERATION, ElasticsearchOperation.INDEX);
headers.put(ElasticsearchConstants.PARAM_INDEX_NAME, "twitter");
headers.put(ElasticsearchConstants.PARAM_INDEX_TYPE, "tweet");
String indexId = template.requestBodyAndHeaders("direct:start", map, headers, String.class);
//now, verify GET
headers.put(ElasticsearchConstants.PARAM_OPERATION, ElasticsearchOperation.GET_BY_ID);
GetResponse response = template.requestBodyAndHeaders("direct:start", indexId, headers, GetResponse.class);
assertNotNull("response should not be null", response);
assertNotNull("response source should not be null", response.getSource());
//now, perform DELETE
headers.put(ElasticsearchConstants.PARAM_OPERATION, ElasticsearchOperation.DELETE);
DeleteResponse deleteResponse = template.requestBodyAndHeaders("direct:start", indexId, headers, DeleteResponse.class);
assertNotNull("response should not be null", deleteResponse);
//now, verify GET fails to find the indexed value
headers.put(ElasticsearchConstants.PARAM_OPERATION, ElasticsearchOperation.GET_BY_ID);
response = template.requestBodyAndHeaders("direct:start", indexId, headers, GetResponse.class);
assertNotNull("response should not be null", response);
assertNull("response source should be null", response.getSource());
}
Aggregations