这里操作的工具是postman。
Elasticsearch 是一个分布式、RESTful 风格的搜索和数据分析引擎,能够解决不断涌现出的各种用例。作为 Elastic Stack 的核心,它集中存储您的数据,帮助你发现意料之中以及意料之外的情况。
elasticsearch是面向文档的,文档是索引和搜索的基本单位。文档以类型来分组,类型包含若干文档。一个或多个类型存在于同一索引中,索引是更大的容器。
可以将elasticsearch的索引比作数据库,类型就是表,文档比作的行。
下载地址:https://www.elastic.co/cn/downloads/elasticsearch
解压后把config/elasticsearch.yml中的下面内容改为false,否则会连接不上。
执行bin\elasticsearch.bat,启动服务。
在浏览器地址中输入localhost:9200测试是否能连接启动,出现串JSON数据就表示成功启动了。
下载分词器:https://github.com/medcl/elasticsearch-analysis-ik/releases
在plugins目录下新建ik目录,下载后解压到ik目录,重启elasticsearch.bat。
对books数据库进行基本的增删查。
作用 | 请求类型 | 请求语句 |
---|---|---|
创建 | PUT | http://localhost:9200/books |
查询 | PUT | http://localhost:9200/books |
删除 | DELETE | http://localhost:9200/books |
请求类型PUT,请求路径:http://localhost:9200/books
请求body
- {
- "mappings":{
- "properties":{
- "id":{
- "type":"keyword"
- },
- "name":{
- "type":"text",
- "analyzer":"ik_max_word",
- "copy_to":"all"
- },
- "type":{
- "type":"keyword"
- },
- "description":{
- "type":"text",
- "analyzer":"ik_max_word",
- "copy_to":"all"
- },
- "all":{
- "type":"text",
- "analyzer":"ik_max_word"
- }
- }
- }
- }
url语句:
作用 | 请求类型 | 请求语句 |
---|---|---|
使用系统生成的id | PUST | http://localhost:9200/books/_doc |
使用指定id | POST | http://localhost:9200/books/_create/1 |
使用指定id,不存在创建,存在更新(版本递增) | POST | http://localhost:9200/books/_doc/1 |
body中的语句:
- {
- 'name':"springboot",
- 'type':"计算机系类书籍",
- 'description':"springboot这个框架很好用"
- }
条件查询:地址/数据库名/_search?q=字段名:关键字
例如:http://localhost:9200/books/_search?q=name:springboot
查询文档时默认有分页的效果,每10条分一页。
全量修改(全量修改)
put类型,url为:http://localhost:9200/books/_doc/1,修改id为1的数据。
body中的数据
- {
- 'name':"springbootnew",
- 'type':"springbootnew类型",
- 'description':"springbootnew很好"
- }
全量修改(部分修改)
post类型,url为:http://localhost:9200/books/_update/1,修改id为1的数据。
body中的数据
- {
- "doc":{
- 'name':"springbootnew"
- }
- }
创建springBoot项目,勾选elasticsearch。
在application中配置elasticsearch。
- spring:
- elasticsearch:
- rest:
- uris: http://localhost:9200
- @SpringBootTest
- class JavaProApplicationTests {
-
- @Autowired
- private BookDao bookDao;
-
- @Autowired
- private RestHighLevelClient client;
-
- @Test
- //创建一个books的索引(数据库)
- void createIndex() throws IOException {
- CreateIndexRequest request = new CreateIndexRequest("books");
- client.indices().create(request, RequestOptions.DEFAULT);
- }
-
- @Test
- //创建一个books的索引(数据库)并指定规则
- void createIndexByIK() throws IOException {
- CreateIndexRequest request = new CreateIndexRequest("books");
- //把写在body中的json写在这里
- String json = "{\"mappings\":{\"properties\":{\"id\":{\"type\":\"keyword\"},\"name\":{\"type\":\"text\",\"analyzer\":\"ik_max_word\",\"copy_to\":\"all\"},\"type\":{\"type\":\"keyword\"},\"description\":{\"type\":\"text\",\"analyzer\":\"ik_max_word\",\"copy_to\":\"all\"},\"all\":{\"type\":\"text\",\"analyzer\":\"ik_max_word\"}}}}";
- request.source(json, XContentType.JSON);
- client.indices().create(request, RequestOptions.DEFAULT);
- }
-
- @Test
- //创建文档
- void createDoc() {
- IndexRequest request = new IndexRequest("books").id("9");
- //把写在body中的json写在这里
- String json = "{\"name\":\"李义新999\",\"type\":\"person\",\"description\":\"很棒\"}";
- request.source(json, XContentType.JSON);
- try {
- client.index(request, RequestOptions.DEFAULT);
- } catch (IOException e) {
- }
- }
-
- @Test
- //创建全文档,把mysql中读取的数据取出来放到elasticsearch中
- void createDocAll() throws IOException {
- List
books = bookDao.selectList(null); - BulkRequest bulk = new BulkRequest();
- for (Book book : books) {
- IndexRequest request = new IndexRequest("books").id(book.getId().toString());
- String json = JSON.toJSONString(book);
- request.source(json, XContentType.JSON);
- bulk.add(request);
- }
- try {
- client.bulk(bulk, RequestOptions.DEFAULT);
- } catch (Exception e) {
-
- }
- }
-
- @Test
- //查询索引(数据库)中的全部内容
- void GetAll() throws IOException {
- SearchRequest request = new SearchRequest();
- request.indices("books");
-
- request.source(new SearchSourceBuilder().query(QueryBuilders.matchAllQuery()));
-
- SearchResponse response = client.search(request, RequestOptions.DEFAULT);
- SearchHits hits = response.getHits();
- System.out.println(hits.getTotalHits()); //查询条数
- System.out.println(response.getTook()); //查询时长
-
- for (SearchHit hit : hits) {
- System.out.println(hit.getSourceAsString());
- }
- }
-
- @Test
- //按条件查询索引(数据库)中的内容
- void Get() throws IOException {
- SearchRequest request = new SearchRequest("books");
-
- SearchSourceBuilder builder = new SearchSourceBuilder();
- builder.query(QueryBuilders.termQuery("name", ""));
- request.source(builder);
-
- SearchResponse search = client.search(request, RequestOptions.DEFAULT);
- SearchHits hits = search.getHits();
- SearchHit[] sh = hits.getHits();
- for (SearchHit h : sh) {
- String source = h.getSourceAsString();
- System.out.println(source);
- }
- }
- }
比如输入url语句按条件查询: http://localhost:9200/books/_search?q=name:计算机
要查询_search中的数据,先获取hits,再通过hits获取下一级的hits数组,通过hist数组获取里面search中的数据。