Search in sources :

Example 26 with GetResponse

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));
}
Also used : DeleteResponse(org.elasticsearch.action.delete.DeleteResponse) GetResponse(org.elasticsearch.action.get.GetResponse) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 27 with GetResponse

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);
}
Also used : GetRequest(org.elasticsearch.action.get.GetRequest) GetResponse(org.elasticsearch.action.get.GetResponse)

Example 28 with GetResponse

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());
}
Also used : GetResponse(org.elasticsearch.action.get.GetResponse) Test(org.junit.Test)

Example 29 with GetResponse

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;
}
Also used : GetResponse(org.elasticsearch.action.get.GetResponse)

Example 30 with GetResponse

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());
}
Also used : DeleteResponse(org.elasticsearch.action.delete.DeleteResponse) HashMap(java.util.HashMap) GetResponse(org.elasticsearch.action.get.GetResponse) MultiGetResponse(org.elasticsearch.action.get.MultiGetResponse) Test(org.junit.Test)

Aggregations

GetResponse (org.elasticsearch.action.get.GetResponse)149 Test (org.junit.Test)41 Map (java.util.Map)27 MultiGetResponse (org.elasticsearch.action.get.MultiGetResponse)25 ArrayList (java.util.ArrayList)22 HashMap (java.util.HashMap)18 ESSyncConfig (com.alibaba.otter.canal.client.adapter.es.core.config.ESSyncConfig)17 Dml (com.alibaba.otter.canal.client.adapter.support.Dml)17 Date (java.util.Date)17 LinkedHashMap (java.util.LinkedHashMap)17 DataSource (javax.sql.DataSource)14 GetRequestBuilder (org.elasticsearch.action.get.GetRequestBuilder)14 SearchResponse (org.elasticsearch.action.search.SearchResponse)13 DeleteResponse (org.elasticsearch.action.delete.DeleteResponse)12 IOException (java.io.IOException)11 GetRequest (org.elasticsearch.action.get.GetRequest)11 ElasticsearchException (org.elasticsearch.ElasticsearchException)9 Settings (org.elasticsearch.common.settings.Settings)9 Alias (org.elasticsearch.action.admin.indices.alias.Alias)7 UpdateResponse (org.elasticsearch.action.update.UpdateResponse)7