[TOC] 本连接器只提供了基本的数据上传、查询和删除操作,但ElasticSearch是一个庞大而复杂的搜索引擎,要完全理解、掌握和使用也不是一蹴而就的事情。它提供了丰富的REST API,我们的连接器不可能完全满足您的需求,所以再此前提下,我们提供了该接入点用于执行elastic的API。 ElasticSearch的REST APIs,请参考: [https://www.elastic.co/guide/en/elasticsearch/reference/current/rest-apis.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/rest-apis.html "https://www.elastic.co/guide/en/elasticsearch/reference/current/rest-apis.html") 在本接入点下,我们演示一个常用的分页查询+高亮显示的例子来给大家说明,如果使用我们的“执行ES语句”接入点。 # 1.插入数据: 利用2.2中说明的保存操作,我们插入以下内容(java代码): ```java String res1=new ShowApiRequest("http://route.showapi.com/2161-2","my_appId","my_appSecret") .addTextPara("data","{\"id_card\":\"1\",\"name\":\"张三\",\"age\":23,\"hobbies\":\"足球 看书\"}") .addTextPara("id_field","id_card") .post(); //============================================= String res2=new ShowApiRequest("http://route.showapi.com/2161-2","my_appId","my_appSecret") .addTextPara("data","{\"id_card\":\"2\",\"name\":\"李四\",\"age\":21,\"hobbies\":\"画画、音乐、足球\"}") .addTextPara("id_field","id_card") .post(); //============================================= String res3=new ShowApiRequest("http://route.showapi.com/2161-2","my_appId","my_appSecret") .addTextPara("data","{\"id_card\":\"3\",\"name\":\"王五\",\"age\":28,\"hobbies\":\"足球、钢琴、武术\"}") .addTextPara("id_field","id_card") .post(); //============================================= String res4=new ShowApiRequest("http://route.showapi.com/2161-2","my_appId","my_appSecret") .addTextPara("data","{\"id_card\":\"4\",\"name\":\"隔壁老王\",\"age\":36,\"hobbies\":\"足球、看书、书法、象棋、炒股\"}") .addTextPara("id_field","id_card") .post(); //============================================= String res5=new ShowApiRequest("http://route.showapi.com/2161-2","my_appId","my_appSecret") .addTextPara("data","{\"id_card\":\"5\",\"name\":\"小明\",\"age\":17,\"hobbies\":\"足球、篮球、唱歌、书法\"}") .addTextPara("id_field","id_card") .post(); ``` # 2.编写语句 首先查询一个简单的语句,比如我们来查询一下年纪小于等于22岁,大于等于10岁的人有哪些,那这个语句怎么写呢?参考以下语句: ```css GET /common_index/_search { "query": { "range": { "age": { "gte": 10, "lte": 22 } } } } ``` 那么我们的连接器接口怎么操作呢?请看如下(JAVA)代码: ```java String script = "{\n" + " \"query\": {\n" + " \"range\": {\n" + " \"age\": {\n" + " \"gte\": 10,\n" + " \"lte\": 22\n" + " }\n" + " }\n" + " }\n" + " }"; String res=new ShowApiRequest("http://route.showapi.com/2161-4","my_appId","my_appSecret") .addTextPara("exec_json",script) .addTextPara("op_type","1")//数据提交的HTTP方式,比如删除操作用3,具体取值如下:1:get 2:post 3:delete 4:put 5:head .addTextPara("url","/common_index/_search") .post(); System.out.println(res); ``` 返回的数据: ```css { "showapi_res_error": "", "showapi_res_code": 0, "showapi_res_id": "5894eabdd8a043d1bda4ddb813d84441", "showapi_res_body": { "_shards": { "total": 1, "skipped": 0, "failed": 0, "successful": 1 }, "hits": { "total": { "value": 2, "relation": "eq" }, "hits": [ { "_type": "_doc", "_id": "2", "_source": { "age": 21, "name": "李四", "hobbies": "画画、音乐、足球" }, "_index": "common_index", "_score": 1 }, { "_type": "_doc", "_id": "5", "_source": { "age": 17, "name": "小明", "hobbies": "足球、篮球、唱歌、书法" }, "_index": "common_index", "_score": 1 } ], "max_score": 1 }, "ret_code": 0, "timed_out": false, "remark": "操作已执行,结果请参考相应返回信息。", "took": 2 } } ``` ------------ 上述例子较为简单,我们现在说一个稍微复杂点的例子。 我们经常有查询+分页显示的需求,比如我们想了解下爱好足球但是不喜欢武术和炒股的人有哪些。根据这个需求,我们来编写elastic的查询语句,语句如下: ```css GET /common_index/_search { "query": { "simple_query_string" : { "fields" : ["hobbies"], "query" : "+足球 -武术 -炒股", "default_operator": "AND" } }, "from": 0, "size": 10 } ``` 代码参考第一个例子,如下: ```java String script = "{\n" + " \"query\": {\n" + " \"simple_query_string\" : {\n" + " \"fields\" : [\"hobbies\"],\n" + " \"query\" : \"+足球 -武术 -炒股\",\n" + " \"default_operator\": \"AND\"\n" + " }\n" + " },\n" + " \"from\": 0,\n" + " \"size\": 10\n" + "}"; String res=new ShowApiRequest("http://route.showapi.com/2161-4","my_appId","my_appSecret") .addTextPara("exec_json",script) .addTextPara("op_type","1")//数据提交的HTTP方式,比如删除操作用3,具体取值如下:1:get 2:post 3:delete 4:put 5:head .addTextPara("url","/common_index/_search") .post(); System.out.println(res); ``` 返回结果: ```css { "showapi_res_error": "", "showapi_res_code": 0, "showapi_res_id": "18837d4ff9d744bda43d6f926f0c4177", "showapi_res_body": { "_shards": { "total": 1, "skipped": 0, "failed": 0, "successful": 1 }, "hits": { "total": { "value": 3, "relation": "eq" }, "hits": [ { "_type": "_doc", "_id": "1", "_source": { "age": 23, "name": "张三", "hobbies": "足球 看书" }, "_index": "common_index", "_score": 2.209275 }, { "_type": "_doc", "_id": "5", "_source": { "age": 17, "name": "小明", "hobbies": "足球、篮球、唱歌、书法" }, "_index": "common_index", "_score": 2.195136 }, { "_type": "_doc", "_id": "2", "_source": { "age": 21, "name": "李四", "hobbies": "画画、音乐、足球" }, "_index": "common_index", "_score": 2.1828218 } ], "max_score": 2.209275 }, "ret_code": 0, "timed_out": false, "remark": "操作已执行,结果请参考相应返回信息。", "took": 2 } } ``` ------------ 再说一个常用的需求"高亮显示"。根据以上的说明,我们实现一个查询+分页+代码高亮显示的代码,需求如下:查询喜欢书籍或者足球的用户有哪些,请分页显示,每页最多5条数据? 由于高亮显示中需要对字符进行分词、长度计算,而我们提供的测试用的ElasticSearch7.4.2是没有安装中文分词器的,所以在查询中文信息的时候进行高亮会执行错误。为避免错误的发生,所以在演示代码操作之前,我们把数据调整一下,主要是把爱好的中文文字调整成英文的,其他信息不变,具体调整如下: ```css [ { "_id": "1", "name": "张三", "age": 23, "hobbies": "football book" }, { "_id": "2", "name": "李四", "age": 21, "hobbies": "art music football" }, { "_id": "3", "name": "王五", "age": 28, "hobbies": "football piano kongfu" }, { "_id": "4", "name": "隔壁老王", "age": 36, "hobbies": "football book calligraphy chess stocks" }, { "_id": "5", "name": "小明", "age": 17, "hobbies": "football basketball sing calligraphy" } ] ``` 根据这个需求,我们来编写elastic的查询语句,语句如下: ```css GET /common_index/_search { "query": { "match": { "hobbies": "book football" } }, "highlight": { "pre_tags": ["<h3>"], "post_tags": ["</h3>"], "fields": { "hobbies": {} } }, "from": 0, "size": 5 } ``` Java操作的代码如下: ```java String script = "{\n" + " \"query\": {\n" + " \"match\": {\n" + " \"hobbies\": \"book football\"\n" + " }\n" + " },\n" + " \"highlight\": {\n" + " \"pre_tags\": [\"<h3>\"],\n" + " \"post_tags\": [\"</h3>\"],\n" + " \"fields\": {\n" + " \"hobbies\": {}\n" + " }\n" + " },\n" + " \"from\": 0,\n" + " \"size\": 5\n" + "}"; String res=new ShowApiRequest("http://route.showapi.com/2161-4","my_appId","my_appSecret") .addTextPara("exec_json",script) .addTextPara("op_type","1") //1:get 2:post 3:delete 4:put 5:head .addTextPara("url","/common_index/_search") .post(); System.out.println(res); ``` 返回的数据: ```css { "showapi_res_error": "", "showapi_res_code": 0, "showapi_res_id": "a1f9de9ed6df44ec9c5d1f12496f6d0c", "showapi_res_body": { "_shards": { "total": 1, "skipped": 0, "failed": 0, "successful": 1 }, "hits": { "total": { "value": 5, "relation": "eq" }, "hits": [ { "_type": "_doc", "_id": "1", "_source": { "age": 23, "name": "张三", "hobbies": "football book" }, "_index": "common_index", "_score": 1.7232791, "highlight": { "hobbies": [ "<h3>football</h3> <h3>book</h3>" ] } }, { "_type": "_doc", "_id": "4", "_source": { "age": 36, "name": "隔壁老王", "hobbies": "football book calligraphy chess stocks" }, "_index": "common_index", "_score": 1.2436035, "highlight": { "hobbies": [ "<h3>football</h3> <h3>book</h3> calligraphy chess stocks" ] } }, { "_type": "_doc", "_id": "3", "_source": { "age": 28, "name": "王五", "hobbies": "football piano Kongfu" }, "_index": "common_index", "_score": 0.2312945, "highlight": { "hobbies": [ "<h3>football</h3> piano Kongfu" ] } }, { "_type": "_doc", "_id": "2", "_source": { "age": 21, "name": "李四", "hobbies": "art music football" }, "_index": "common_index", "_score": 0.2312945, "highlight": { "hobbies": [ "art music <h3>football</h3>" ] } }, { "_type": "_doc", "_id": "5", "_source": { "age": 17, "name": "小明", "hobbies": "football basketball sing calligraphy" }, "_index": "common_index", "_score": 0.20763937, "highlight": { "hobbies": [ "<h3>football</h3> basketball sing calligraphy" ] } } ], "max_score": 1.7232791 }, "ret_code": 0, "timed_out": false, "remark": "操作已执行,结果请参考相应返回信息。", "took": 6 } } ``` 根据以上的返回我们可以看到“爱好”字段中,只要包含book和football两项之一的,都会进行返回并高亮显示,并有打分_score,完全满足了我们的需求。 以上的操作是针对查询的,因为search是我们最常用、最关注的操作。您也可以进行其他操作,如删除数据、更改映射、滚动查询、统计等等。