Spring数据elasticsearch根据请求参数动态创建索引,percolator支持并通过Elasticsearch操作创建索引 [英] Spring data elasticsearch to create index dynamically based on request parameter, percolator support and create index via Elasticsearch Operations

查看:68
本文介绍了Spring数据elasticsearch根据请求参数动态创建索引,percolator支持并通过Elasticsearch操作创建索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通读了https://docs.spring.io/spring-data/elasticsearch/docs/current/reference/html/#reference

开头

我的要求

  1. 我想使用过滤器.spring数据elasticsearch中是否有任何支持?我在上面的链接中没有看到任何内容,尽管我知道渗透与索引相同(技术上从使用 spring 数据弹性搜索的角度来看).所以我可以使用 spring 数据弹性搜索的索引部分,但只是检查是否有任何特定于 percolator 的内容.
  2. 我想动态创建一个索引.我知道我可以使用 https://docs.spring.io/spring-data/elasticsearch/docs/current/reference/html/#elasticsearch.mapping.meta-model.annotations 但是我的情况略有不同,我将通过 RequestParam 作为 API 调用的一部分获取索引名称.所以这意味着据我所知,我不能使用 SpEL 或尝试类似 https://stackoverflow.com/a/33520421/4068218
  3. 我知道我可以使用 ElasticsearchOperations 或 ElasticsearchRepository 来创建索引.由于#2(即通过请求参数的索引名称),我认为 ElasticsearchOperations 更适合,但我看到 IndexOperations 促进了 createMapping、createSettings,但不能同时使用.我也看到 putMapping 但我没有看到任何说明映射和设置的内容.我想要两者的原因是我想创建像下面这样的东西

<块引用>

 设置":{索引":{number_of_shards": 1,number_of_replicas": 0}},映射":{属性":{消息":{类型":文本"},查询":{类型":渗透器"}}}

底线:- 如何使用 ElasticsearchOperations 使用映射和设置创建索引(索引名称将通过请求参数动态变化)?
非常感谢任何线索/帮助

首先非常感谢@P.J.Meisch.为您的评论点赞以示感谢.

以下为我工作.以下可能会在未来帮助其他人

 文档映射 = Document.create().fromJson("{\n" +\n"+"\"属性\":{\n"+"\"消息\":{\n"+"\"类型\":\"文本\"\n"+"},\n"+"\"查询\":{\n";+"\"类型\":\"percolator\"\n"+"}\n"+"}\n"+\n"+"}");映射<字符串,对象>settings = ImmutableMap.of(number_of_shards",2,number_of_replicas",1);elasticsearchOperations.indexOps(IndexCoordinates.of("whatever-indexname-you-need")).create(settings,mapping);

I read through https://docs.spring.io/spring-data/elasticsearch/docs/current/reference/html/#reference to begin with

My requirements

  1. I want to use percolator. Is there any support for it in spring data elasticsearch? I don't see any in the above link although I understand that percolating is same as indexing (technically from using spring data elasticsearch's perspective). So I can use the indexing part of spring data elasticsearch but just checking if there are any that are specific to percolator.
  2. I want to create an index dynamically. I do understand I can achieve that using SpEL template expression as mentioned in https://docs.spring.io/spring-data/elasticsearch/docs/current/reference/html/#elasticsearch.mapping.meta-model.annotations but my case is slightly different, I will get the index name via the RequestParam as part of the API call. So this means as of my knowledge I cannot use SpEL or try something like https://stackoverflow.com/a/33520421/4068218
  3. I see I can use ElasticsearchOperations or ElasticsearchRepository to create Index. Because of #2 (i.e index name via request parameter) I think ElasticsearchOperations better suits but I see IndexOperations facilitating createMapping, createSettings but not both together. I see putMapping too but I dont see anything that says both mapping and settings. The reason I want both is I want to create something like below to begin with

  "settings" : {
                "index" : {
                  "number_of_shards" : 1,
                  "number_of_replicas" : 0
                }
              },
            "mappings": {
                "properties": {
                  "message": {
                    "type": "text"
                  },
                  "query": {
                    "type": "percolator"
                  }
                }
              }

Bottom line :- How do I create an index (name of the index will be dynamic via request param) with mappings, settings using ElasticsearchOperations?
Any lead/help is much appreciated

解决方案

First of all thank you very much @P.J.Meisch. Upvoted both your comments as a token of gratitude.

Below worked for me. Below might help others in future

 Document mapping = Document.create().fromJson("{\n" +
                "\n" +
                "    \"properties\": {\n" +
                "      \"message\": {\n" +
                "        \"type\": \"text\"\n" +
                "      },\n" +
                "      \"query\": {\n" +
                "        \"type\": \"percolator\"\n" +
                "      }\n" +
                "    }\n" +
                "\n" +
                "}");
 Map<String, Object> settings = ImmutableMap.of( "number_of_shards" ,2,"number_of_replicas",1);
 elasticsearchOperations.indexOps(IndexCoordinates.of("whatever-indexname-you-need")).create(settings,mapping);

这篇关于Spring数据elasticsearch根据请求参数动态创建索引,percolator支持并通过Elasticsearch操作创建索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆