Search in sources :

Example 1 with AliasActions

use of org.springframework.data.elasticsearch.core.index.AliasActions in project spring-data-elasticsearch by spring-projects.

the class RequestFactory method indicesAliasesRequest.

public IndicesAliasesRequest indicesAliasesRequest(AliasActions aliasActions) {
    IndicesAliasesRequest request = new IndicesAliasesRequest();
    aliasActions.getActions().forEach(aliasAction -> {
        IndicesAliasesRequest.AliasActions aliasActionsES = null;
        if (aliasAction instanceof AliasAction.Add) {
            AliasAction.Add add = (AliasAction.Add) aliasAction;
            IndicesAliasesRequest.AliasActions addES = IndicesAliasesRequest.AliasActions.add();
            AliasActionParameters parameters = add.getParameters();
            addES.indices(parameters.getIndices());
            addES.aliases(parameters.getAliases());
            addES.routing(parameters.getRouting());
            addES.indexRouting(parameters.getIndexRouting());
            addES.searchRouting(parameters.getSearchRouting());
            addES.isHidden(parameters.getHidden());
            addES.writeIndex(parameters.getWriteIndex());
            Query filterQuery = parameters.getFilterQuery();
            if (filterQuery != null) {
                elasticsearchConverter.updateQuery(filterQuery, parameters.getFilterQueryClass());
                QueryBuilder queryBuilder = getFilter(filterQuery);
                if (queryBuilder == null) {
                    queryBuilder = getQuery(filterQuery);
                }
                addES.filter(queryBuilder);
            }
            aliasActionsES = addES;
        } else if (aliasAction instanceof AliasAction.Remove) {
            AliasAction.Remove remove = (AliasAction.Remove) aliasAction;
            IndicesAliasesRequest.AliasActions removeES = IndicesAliasesRequest.AliasActions.remove();
            AliasActionParameters parameters = remove.getParameters();
            removeES.indices(parameters.getIndices());
            removeES.aliases(parameters.getAliases());
            aliasActionsES = removeES;
        } else if (aliasAction instanceof AliasAction.RemoveIndex) {
            AliasAction.RemoveIndex removeIndex = (AliasAction.RemoveIndex) aliasAction;
            IndicesAliasesRequest.AliasActions removeIndexES = IndicesAliasesRequest.AliasActions.removeIndex();
            AliasActionParameters parameters = removeIndex.getParameters();
            removeIndexES.indices(parameters.getIndices()[0]);
            aliasActionsES = removeIndexES;
        }
        if (aliasActionsES != null) {
            request.addAliasAction(aliasActionsES);
        }
    });
    return request;
}
Also used : MoreLikeThisQueryBuilder(org.elasticsearch.index.query.MoreLikeThisQueryBuilder) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) AliasActions(org.springframework.data.elasticsearch.core.index.AliasActions) AliasAction(org.springframework.data.elasticsearch.core.index.AliasAction) IndicesAliasesRequest(org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest) AliasActionParameters(org.springframework.data.elasticsearch.core.index.AliasActionParameters)

Example 2 with AliasActions

use of org.springframework.data.elasticsearch.core.index.AliasActions in project spring-data-elasticsearch by spring-projects.

the class RequestFactory method putIndexTemplateRequest.

public PutIndexTemplateRequest putIndexTemplateRequest(PutTemplateRequest putTemplateRequest) {
    PutIndexTemplateRequest request = new PutIndexTemplateRequest(putTemplateRequest.getName()).patterns(Arrays.asList(putTemplateRequest.getIndexPatterns()));
    if (putTemplateRequest.getSettings() != null) {
        request.settings(putTemplateRequest.getSettings());
    }
    if (putTemplateRequest.getMappings() != null) {
        request.mapping(putTemplateRequest.getMappings());
    }
    request.order(putTemplateRequest.getOrder()).version(putTemplateRequest.getVersion());
    AliasActions aliasActions = putTemplateRequest.getAliasActions();
    if (aliasActions != null) {
        aliasActions.getActions().forEach(aliasAction -> {
            AliasActionParameters parameters = aliasAction.getParameters();
            String[] parametersAliases = parameters.getAliases();
            if (parametersAliases != null) {
                for (String aliasName : parametersAliases) {
                    Alias alias = new Alias(aliasName);
                    if (parameters.getRouting() != null) {
                        alias.routing(parameters.getRouting());
                    }
                    if (parameters.getIndexRouting() != null) {
                        alias.indexRouting(parameters.getIndexRouting());
                    }
                    if (parameters.getSearchRouting() != null) {
                        alias.searchRouting(parameters.getSearchRouting());
                    }
                    if (parameters.getHidden() != null) {
                        alias.isHidden(parameters.getHidden());
                    }
                    if (parameters.getWriteIndex() != null) {
                        alias.writeIndex(parameters.getWriteIndex());
                    }
                    Query filterQuery = parameters.getFilterQuery();
                    if (filterQuery != null) {
                        elasticsearchConverter.updateQuery(filterQuery, parameters.getFilterQueryClass());
                        QueryBuilder queryBuilder = getFilter(filterQuery);
                        if (queryBuilder == null) {
                            queryBuilder = getQuery(filterQuery);
                        }
                        alias.filter(queryBuilder);
                    }
                    request.alias(alias);
                }
            }
        });
    }
    return request;
}
Also used : Alias(org.elasticsearch.action.admin.indices.alias.Alias) PutIndexTemplateRequest(org.elasticsearch.client.indices.PutIndexTemplateRequest) AliasActionParameters(org.springframework.data.elasticsearch.core.index.AliasActionParameters) MoreLikeThisQueryBuilder(org.elasticsearch.index.query.MoreLikeThisQueryBuilder) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) AliasActions(org.springframework.data.elasticsearch.core.index.AliasActions)

Example 3 with AliasActions

use of org.springframework.data.elasticsearch.core.index.AliasActions in project spring-data-elasticsearch by spring-projects.

the class ReactiveIndexOperationsTest method shouldGetTemplate.

// DATAES-612
@Test
void shouldGetTemplate() throws JSONException {
    ReactiveIndexOperations indexOps = operations.indexOps(Entity.class);
    org.springframework.data.elasticsearch.core.document.Document mapping = indexOps.createMapping(TemplateClass.class).block();
    Settings settings = indexOps.createSettings(TemplateClass.class).block();
    AliasActions aliasActions = new AliasActions(new AliasAction.Add(AliasActionParameters.builderForTemplate().withAliases("alias1", "alias2").build()));
    PutTemplateRequest putTemplateRequest = // 
    PutTemplateRequest.builder("test-template", "log-*").withSettings(// 
    settings).withMappings(// 
    mapping).withAliasActions(// 
    aliasActions).withOrder(// 
    11).withVersion(// 
    42).build();
    Boolean acknowledged = indexOps.putTemplate(putTemplateRequest).block();
    assertThat(acknowledged).isTrue();
    GetTemplateRequest getTemplateRequest = new GetTemplateRequest(putTemplateRequest.getName());
    TemplateData templateData = indexOps.getTemplate(getTemplateRequest).block();
    assertThat(templateData).isNotNull();
    assertThat(templateData.getIndexPatterns()).containsExactlyInAnyOrder(putTemplateRequest.getIndexPatterns());
    assertEquals(settings.toJson(), templateData.getSettings().toJson(), false);
    assertEquals(mapping.toJson(), templateData.getMapping().toJson(), false);
    Map<String, AliasData> aliases = templateData.getAliases();
    assertThat(aliases).hasSize(2);
    AliasData alias1 = aliases.get("alias1");
    assertThat(alias1.getAlias()).isEqualTo("alias1");
    AliasData alias2 = aliases.get("alias2");
    assertThat(alias2.getAlias()).isEqualTo("alias2");
    assertThat(templateData.getOrder()).isEqualTo(putTemplateRequest.getOrder());
    assertThat(templateData.getVersion()).isEqualTo(putTemplateRequest.getVersion());
}
Also used : PutTemplateRequest(org.springframework.data.elasticsearch.core.index.PutTemplateRequest) AliasActions(org.springframework.data.elasticsearch.core.index.AliasActions) AliasData(org.springframework.data.elasticsearch.core.index.AliasData) TemplateData(org.springframework.data.elasticsearch.core.index.TemplateData) AliasAction(org.springframework.data.elasticsearch.core.index.AliasAction) GetTemplateRequest(org.springframework.data.elasticsearch.core.index.GetTemplateRequest) Settings(org.springframework.data.elasticsearch.core.index.Settings) Test(org.junit.jupiter.api.Test) SpringIntegrationTest(org.springframework.data.elasticsearch.junit.jupiter.SpringIntegrationTest)

Example 4 with AliasActions

use of org.springframework.data.elasticsearch.core.index.AliasActions in project spring-data-elasticsearch by spring-projects.

the class RequestFactoryTests method shouldBuildIndicesAliasRequest.

@Test
// DATAES-864
void shouldBuildIndicesAliasRequest() throws IOException, JSONException {
    AliasActions aliasActions = new AliasActions();
    aliasActions.add(new AliasAction.Add(AliasActionParameters.builder().withIndices("index1", "index2").withAliases("alias1").build()));
    aliasActions.add(new AliasAction.Remove(AliasActionParameters.builder().withIndices("index3").withAliases("alias1").build()));
    aliasActions.add(new AliasAction.RemoveIndex(AliasActionParameters.builder().withIndices("index3").build()));
    aliasActions.add(new AliasAction.Add(AliasActionParameters.builder().withIndices("index4").withAliases("alias4").withRouting("routing").withIndexRouting("indexRouting").withSearchRouting("searchRouting").withIsHidden(true).withIsWriteIndex(true).build()));
    Query query = new CriteriaQuery(new Criteria("lastName").is("Smith"));
    aliasActions.add(new AliasAction.Add(AliasActionParameters.builder().withIndices("index5").withAliases("alias5").withFilterQuery(query, Person.class).build()));
    String expected = // 
    "{\n" + // 
    "  \"actions\": [\n" + // 
    "    {\n" + // 
    "      \"add\": {\n" + // 
    "        \"indices\": [\n" + // 
    "          \"index1\",\n" + // 
    "          \"index2\"\n" + // 
    "        ],\n" + // 
    "        \"aliases\": [\n" + // 
    "          \"alias1\"\n" + // 
    "        ]\n" + // 
    "      }\n" + // 
    "    },\n" + // 
    "    {\n" + // 
    "      \"remove\": {\n" + // 
    "        \"indices\": [\n" + // 
    "          \"index3\"\n" + // 
    "        ],\n" + // 
    "        \"aliases\": [\n" + // 
    "          \"alias1\"\n" + // 
    "        ]\n" + // 
    "      }\n" + // 
    "    },\n" + // 
    "    {\n" + // 
    "      \"remove_index\": {\n" + // 
    "        \"indices\": [\n" + // 
    "          \"index3\"\n" + // 
    "        ]\n" + // 
    "      }\n" + // 
    "    },\n" + // 
    "    {\n" + // 
    "      \"add\": {\n" + // 
    "        \"indices\": [\n" + // 
    "          \"index4\"\n" + // 
    "        ],\n" + // 
    "        \"aliases\": [\n" + // 
    "          \"alias4\"\n" + // 
    "        ],\n" + // 
    "        \"routing\": \"routing\",\n" + // 
    "        \"index_routing\": \"indexRouting\",\n" + // 
    "        \"search_routing\": \"searchRouting\",\n" + // 
    "        \"is_write_index\": true,\n" + // 
    "        \"is_hidden\": true\n" + // 
    "      }\n" + // 
    "    },\n" + // 
    "    {\n" + // 
    "      \"add\": {\n" + // 
    "        \"indices\": [\n" + // 
    "          \"index5\"\n" + // 
    "        ],\n" + // 
    "        \"aliases\": [\n" + // 
    "          \"alias5\"\n" + // 
    "        ],\n" + // 
    "        \"filter\": {\n" + // 
    "          \"bool\": {\n" + // 
    "            \"must\": [\n" + // 
    "              {\n" + // 
    "                \"query_string\": {\n" + // 
    "                  \"query\": \"Smith\",\n" + // 
    "                  \"fields\": [\n" + // 
    "                    \"last-name^1.0\"\n" + // 
    "                  ],\n" + // 
    "                  \"type\": \"best_fields\",\n" + // 
    "                  \"default_operator\": \"and\",\n" + // 
    "                  \"max_determinized_states\": 10000,\n" + // 
    "                  \"enable_position_increments\": true,\n" + // 
    "                  \"fuzziness\": \"AUTO\",\n" + // 
    "                  \"fuzzy_prefix_length\": 0,\n" + // 
    "                  \"fuzzy_max_expansions\": 50,\n" + // 
    "                  \"phrase_slop\": 0,\n" + // 
    "                  \"escape\": false,\n" + // 
    "                  \"auto_generate_synonyms_phrase_query\": true,\n" + // 
    "                  \"fuzzy_transpositions\": true,\n" + // 
    "                  \"boost\": 1.0\n" + // 
    "                }\n" + // 
    "              }\n" + // 
    "            ],\n" + // 
    "            \"adjust_pure_negative\": true,\n" + // 
    "            \"boost\": 1.0\n" + // 
    "          }\n" + // 
    "        }\n" + // 
    "      }\n" + // 
    "    }\n" + // 
    "  ]\n" + // 
    "}";
    IndicesAliasesRequest indicesAliasesRequest = requestFactory.indicesAliasesRequest(aliasActions);
    String json = requestToString(indicesAliasesRequest);
    assertEquals(expected, json, false);
}
Also used : FunctionScoreQuery(org.elasticsearch.common.lucene.search.function.FunctionScoreQuery) AliasAction(org.springframework.data.elasticsearch.core.index.AliasAction) IndicesAliasesRequest(org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest) AliasActions(org.springframework.data.elasticsearch.core.index.AliasActions) Test(org.junit.jupiter.api.Test)

Example 5 with AliasActions

use of org.springframework.data.elasticsearch.core.index.AliasActions in project spring-data-elasticsearch by spring-projects.

the class RequestFactoryTests method shouldCreatePutIndexTemplateRequest.

@Test
// DATAES-612
void shouldCreatePutIndexTemplateRequest() throws JSONException, IOException {
    String expected = // 
    "{\n" + // 
    "  \"index_patterns\": [\n" + // 
    "    \"test-*\"\n" + // 
    "  ],\n" + // 
    "  \"order\": 42,\n" + // 
    "  \"version\": 7,\n" + // 
    "  \"settings\": {\n" + // 
    "    \"index\": {\n" + // 
    "      \"number_of_replicas\": \"2\",\n" + // 
    "      \"number_of_shards\": \"3\",\n" + // 
    "      \"refresh_interval\": \"7s\",\n" + // 
    "      \"store\": {\n" + // 
    "        \"type\": \"oops\"\n" + // 
    "      }\n" + // 
    "    }\n" + // 
    "  },\n" + // 
    "  \"mappings\": {\n" + // 
    "    \"properties\": {\n" + // 
    "      \"price\": {\n" + // 
    "        \"type\": \"double\"\n" + // 
    "      }\n" + // 
    "    }\n" + // 
    "  },\n" + // 
    "  \"aliases\":{\n" + // 
    "    \"alias1\": {},\n" + // 
    "    \"alias2\": {},\n" + // 
    "    \"alias3\": {\n" + // 
    "      \"routing\": \"11\"\n" + // 
    "    }\n" + // 
    "  }\n" + // 
    "}\n";
    org.springframework.data.elasticsearch.core.document.Document settings = org.springframework.data.elasticsearch.core.document.Document.create();
    settings.put("index.number_of_replicas", 2);
    settings.put("index.number_of_shards", 3);
    settings.put("index.refresh_interval", "7s");
    settings.put("index.store.type", "oops");
    org.springframework.data.elasticsearch.core.document.Document mappings = org.springframework.data.elasticsearch.core.document.Document.parse("{\"properties\":{\"price\":{\"type\":\"double\"}}}");
    AliasActions aliasActions = new AliasActions(new AliasAction.Add(AliasActionParameters.builderForTemplate().withAliases("alias1", "alias2").build()), new AliasAction.Add(AliasActionParameters.builderForTemplate().withAliases("alias3").withRouting("11").build()));
    PutTemplateRequest putTemplateRequest = // 
    PutTemplateRequest.builder("test-template", "test-*").withSettings(// 
    settings).withMappings(// 
    mappings).withAliasActions(// 
    aliasActions).withOrder(// 
    42).withVersion(// 
    7).build();
    PutIndexTemplateRequest putIndexTemplateRequest = requestFactory.putIndexTemplateRequest(putTemplateRequest);
    String json = requestToString(putIndexTemplateRequest);
    assertEquals(expected, json, false);
}
Also used : PutTemplateRequest(org.springframework.data.elasticsearch.core.index.PutTemplateRequest) AliasAction(org.springframework.data.elasticsearch.core.index.AliasAction) PutIndexTemplateRequest(org.elasticsearch.client.indices.PutIndexTemplateRequest) AliasActions(org.springframework.data.elasticsearch.core.index.AliasActions) Test(org.junit.jupiter.api.Test)

Aggregations

AliasActions (org.springframework.data.elasticsearch.core.index.AliasActions)9 AliasAction (org.springframework.data.elasticsearch.core.index.AliasAction)8 Test (org.junit.jupiter.api.Test)7 AliasActionParameters (org.springframework.data.elasticsearch.core.index.AliasActionParameters)5 PutTemplateRequest (org.springframework.data.elasticsearch.core.index.PutTemplateRequest)5 SpringIntegrationTest (org.springframework.data.elasticsearch.junit.jupiter.SpringIntegrationTest)5 AliasData (org.springframework.data.elasticsearch.core.index.AliasData)4 Settings (org.springframework.data.elasticsearch.core.index.Settings)4 LocalDate (java.time.LocalDate)3 Map (java.util.Map)3 UUID (java.util.UUID)3 Assertions (org.assertj.core.api.Assertions)3 JSONException (org.json.JSONException)3 BeforeEach (org.junit.jupiter.api.BeforeEach)3 JSONAssert (org.skyscreamer.jsonassert.JSONAssert)3 Autowired (org.springframework.beans.factory.annotation.Autowired)3 Configuration (org.springframework.context.annotation.Configuration)3 Import (org.springframework.context.annotation.Import)3 Id (org.springframework.data.annotation.Id)3 Document (org.springframework.data.elasticsearch.annotations.Document)3