Search in sources :

Example 31 with GetResponse

use of org.elasticsearch.action.get.GetResponse in project elasticsearch by elastic.

the class SimpleRoutingIT method testRequiredRoutingCrudApis.

public void testRequiredRoutingCrudApis() throws Exception {
    client().admin().indices().prepareCreate("test").addAlias(new Alias("alias")).addMapping("type1", XContentFactory.jsonBuilder().startObject().startObject("type1").startObject("_routing").field("required", true).endObject().endObject().endObject()).execute().actionGet();
    ensureGreen();
    logger.info("--> indexing with id [1], and routing [0]");
    client().prepareIndex(indexOrAlias(), "type1", "1").setRouting("0").setSource("field", "value1").setRefreshPolicy(RefreshPolicy.IMMEDIATE).get();
    logger.info("--> verifying get with no routing, should fail");
    logger.info("--> indexing with id [1], with no routing, should fail");
    try {
        client().prepareIndex(indexOrAlias(), "type1", "1").setSource("field", "value1").get();
        fail("index with missing routing when routing is required should fail");
    } catch (ElasticsearchException e) {
        assertThat(e.unwrapCause(), instanceOf(RoutingMissingException.class));
    }
    logger.info("--> verifying get with routing, should find");
    for (int i = 0; i < 5; i++) {
        assertThat(client().prepareGet(indexOrAlias(), "type1", "1").setRouting("0").execute().actionGet().isExists(), equalTo(true));
    }
    logger.info("--> deleting with no routing, should fail");
    try {
        client().prepareDelete(indexOrAlias(), "type1", "1").get();
        fail("delete with missing routing when routing is required should fail");
    } catch (ElasticsearchException e) {
        assertThat(e.unwrapCause(), instanceOf(RoutingMissingException.class));
    }
    for (int i = 0; i < 5; i++) {
        try {
            client().prepareGet(indexOrAlias(), "type1", "1").execute().actionGet().isExists();
            fail("get with missing routing when routing is required should fail");
        } catch (RoutingMissingException e) {
            assertThat(e.status(), equalTo(RestStatus.BAD_REQUEST));
            assertThat(e.getMessage(), equalTo("routing is required for [test]/[type1]/[1]"));
        }
        assertThat(client().prepareGet(indexOrAlias(), "type1", "1").setRouting("0").execute().actionGet().isExists(), equalTo(true));
    }
    try {
        client().prepareUpdate(indexOrAlias(), "type1", "1").setDoc(Requests.INDEX_CONTENT_TYPE, "field", "value2").execute().actionGet();
        fail("update with missing routing when routing is required should fail");
    } catch (ElasticsearchException e) {
        assertThat(e.unwrapCause(), instanceOf(RoutingMissingException.class));
    }
    client().prepareUpdate(indexOrAlias(), "type1", "1").setRouting("0").setDoc(Requests.INDEX_CONTENT_TYPE, "field", "value2").get();
    client().admin().indices().prepareRefresh().execute().actionGet();
    for (int i = 0; i < 5; i++) {
        try {
            client().prepareGet(indexOrAlias(), "type1", "1").execute().actionGet().isExists();
            fail();
        } catch (RoutingMissingException e) {
            assertThat(e.status(), equalTo(RestStatus.BAD_REQUEST));
            assertThat(e.getMessage(), equalTo("routing is required for [test]/[type1]/[1]"));
        }
        GetResponse getResponse = client().prepareGet(indexOrAlias(), "type1", "1").setRouting("0").execute().actionGet();
        assertThat(getResponse.isExists(), equalTo(true));
        assertThat(getResponse.getSourceAsMap().get("field"), equalTo("value2"));
    }
    client().prepareDelete(indexOrAlias(), "type1", "1").setRouting("0").setRefreshPolicy(RefreshPolicy.IMMEDIATE).get();
    for (int i = 0; i < 5; i++) {
        try {
            client().prepareGet(indexOrAlias(), "type1", "1").execute().actionGet().isExists();
            fail();
        } catch (RoutingMissingException e) {
            assertThat(e.status(), equalTo(RestStatus.BAD_REQUEST));
            assertThat(e.getMessage(), equalTo("routing is required for [test]/[type1]/[1]"));
        }
        assertThat(client().prepareGet(indexOrAlias(), "type1", "1").setRouting("0").execute().actionGet().isExists(), equalTo(false));
    }
}
Also used : Alias(org.elasticsearch.action.admin.indices.alias.Alias) ElasticsearchException(org.elasticsearch.ElasticsearchException) GetResponse(org.elasticsearch.action.get.GetResponse) MultiGetResponse(org.elasticsearch.action.get.MultiGetResponse) RoutingMissingException(org.elasticsearch.action.RoutingMissingException)

Example 32 with GetResponse

use of org.elasticsearch.action.get.GetResponse in project elasticsearch by elastic.

the class SimpleNestedIT method testMultiNested.

public void testMultiNested() throws Exception {
    assertAcked(prepareCreate("test").addMapping("type1", jsonBuilder().startObject().startObject("type1").startObject("properties").startObject("nested1").field("type", "nested").startObject("properties").startObject("nested2").field("type", "nested").endObject().endObject().endObject().endObject().endObject().endObject()));
    ensureGreen();
    client().prepareIndex("test", "type1", "1").setSource(jsonBuilder().startObject().field("field", "value").startArray("nested1").startObject().field("field1", "1").startArray("nested2").startObject().field("field2", "2").endObject().startObject().field("field2", "3").endObject().endArray().endObject().startObject().field("field1", "4").startArray("nested2").startObject().field("field2", "5").endObject().startObject().field("field2", "6").endObject().endArray().endObject().endArray().endObject()).execute().actionGet();
    // flush, so we fetch it from the index (as see that we filter nested docs)
    flush();
    GetResponse getResponse = client().prepareGet("test", "type1", "1").execute().actionGet();
    assertThat(getResponse.isExists(), equalTo(true));
    waitForRelocation(ClusterHealthStatus.GREEN);
    // check the numDocs
    assertDocumentCount("test", 7);
    // do some multi nested queries
    SearchResponse searchResponse = client().prepareSearch("test").setQuery(nestedQuery("nested1", termQuery("nested1.field1", "1"), ScoreMode.Avg)).execute().actionGet();
    assertNoFailures(searchResponse);
    assertThat(searchResponse.getHits().getTotalHits(), equalTo(1L));
    searchResponse = client().prepareSearch("test").setQuery(nestedQuery("nested1.nested2", termQuery("nested1.nested2.field2", "2"), ScoreMode.Avg)).execute().actionGet();
    assertNoFailures(searchResponse);
    assertThat(searchResponse.getHits().getTotalHits(), equalTo(1L));
    searchResponse = client().prepareSearch("test").setQuery(nestedQuery("nested1", boolQuery().must(termQuery("nested1.field1", "1")).must(nestedQuery("nested1.nested2", termQuery("nested1.nested2.field2", "2"), ScoreMode.Avg)), ScoreMode.Avg)).execute().actionGet();
    assertNoFailures(searchResponse);
    assertThat(searchResponse.getHits().getTotalHits(), equalTo(1L));
    searchResponse = client().prepareSearch("test").setQuery(nestedQuery("nested1", boolQuery().must(termQuery("nested1.field1", "1")).must(nestedQuery("nested1.nested2", termQuery("nested1.nested2.field2", "3"), ScoreMode.Avg)), ScoreMode.Avg)).execute().actionGet();
    assertNoFailures(searchResponse);
    assertThat(searchResponse.getHits().getTotalHits(), equalTo(1L));
    searchResponse = client().prepareSearch("test").setQuery(nestedQuery("nested1", boolQuery().must(termQuery("nested1.field1", "1")).must(nestedQuery("nested1.nested2", termQuery("nested1.nested2.field2", "4"), ScoreMode.Avg)), ScoreMode.Avg)).execute().actionGet();
    assertNoFailures(searchResponse);
    assertThat(searchResponse.getHits().getTotalHits(), equalTo(0L));
    searchResponse = client().prepareSearch("test").setQuery(nestedQuery("nested1", boolQuery().must(termQuery("nested1.field1", "1")).must(nestedQuery("nested1.nested2", termQuery("nested1.nested2.field2", "5"), ScoreMode.Avg)), ScoreMode.Avg)).execute().actionGet();
    assertNoFailures(searchResponse);
    assertThat(searchResponse.getHits().getTotalHits(), equalTo(0L));
    searchResponse = client().prepareSearch("test").setQuery(nestedQuery("nested1", boolQuery().must(termQuery("nested1.field1", "4")).must(nestedQuery("nested1.nested2", termQuery("nested1.nested2.field2", "5"), ScoreMode.Avg)), ScoreMode.Avg)).execute().actionGet();
    assertNoFailures(searchResponse);
    assertThat(searchResponse.getHits().getTotalHits(), equalTo(1L));
    searchResponse = client().prepareSearch("test").setQuery(nestedQuery("nested1", boolQuery().must(termQuery("nested1.field1", "4")).must(nestedQuery("nested1.nested2", termQuery("nested1.nested2.field2", "2"), ScoreMode.Avg)), ScoreMode.Avg)).execute().actionGet();
    assertNoFailures(searchResponse);
    assertThat(searchResponse.getHits().getTotalHits(), equalTo(0L));
}
Also used : GetResponse(org.elasticsearch.action.get.GetResponse) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 33 with GetResponse

use of 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 34 with GetResponse

use of 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 35 with GetResponse

use of 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)

Aggregations

GetResponse (org.elasticsearch.action.get.GetResponse)84 MultiGetResponse (org.elasticsearch.action.get.MultiGetResponse)22 Test (org.junit.Test)18 SearchResponse (org.elasticsearch.action.search.SearchResponse)12 HashMap (java.util.HashMap)11 GetRequest (org.elasticsearch.action.get.GetRequest)11 DeleteResponse (org.elasticsearch.action.delete.DeleteResponse)10 Script (org.elasticsearch.script.Script)9 Settings (org.elasticsearch.common.settings.Settings)8 Matchers.containsString (org.hamcrest.Matchers.containsString)8 IOException (java.io.IOException)7 Map (java.util.Map)7 Alias (org.elasticsearch.action.admin.indices.alias.Alias)7 CompiledScript (org.elasticsearch.script.CompiledScript)7 ExecutableScript (org.elasticsearch.script.ExecutableScript)7 SearchScript (org.elasticsearch.script.SearchScript)7 UpdateResponse (org.elasticsearch.action.update.UpdateResponse)6 IndexResponse (org.elasticsearch.action.index.IndexResponse)5 VersionConflictEngineException (org.elasticsearch.index.engine.VersionConflictEngineException)5 Path (java.nio.file.Path)4