全文搜索,拆分,分词--获得id,获取部分数据装载进去,简化版数据,用一种状态展示出来,点击详情走的是数据库查询查看显示详情
倒排索引(特征),创建文档,使用文档--加速查询
下载Elasticsearch 7.16.2 | Elastic
打开,目录E:\soft\elasticsearch-7.16.2\bin中的elasticsearch.bat文件,双击即可,首次打开较慢,时间还行,里面两个端口,9300与9200,9200是对外访问端口,启动成功,直接浏览器访问http://localhost:9200/即可
IK分词器Releases · medcl/elasticsearch-analysis-ik · GitHub
下载好的ik分词器东西放置在这里E:\soft\elasticsearch-7.16.2\plugins\ik,重启
http://localhost:9200/books Body raw JSON
{
"mappings":{
"properties":{
"id":{
"type":"keyword"//关键字
},
"name":{
"type":"text",//文本
"analyzer":"ik_max_word",//分词器
"copy_to":"all"//拷贝到虚拟字段all中
},
"type":{
"type":"keyword"//关键字
},
"description":{
"type":"text",//文本
"analyzer":"ik_max_word",//分词器
"copy_to":"all"//拷贝到虚拟字段all中
},
"all":{
"type":"text",
"analyzer":"ik_max_word"
}
}
}
}
主要的几个方法
private RestHighLevelClient client;
//创建索引 @Test void testCreateIndexByIk() throws IOException { //客户端操作 CreateIndexRequest request = new CreateIndexRequest("books"); //设置请求中的参数 String json = "{\n" + " \"mappings\":{\n" + " \"properties\":{\n" + " \"id\":{\n" + " \"type\":\"keyword\"\n" + " },\n" + " \"name\":{\n" + " \"type\":\"text\",\n" + " \"analyzer\":\"ik_max_word\",\n" + " \"copy_to\":\"all\"\n" + " },\n" + " \"type\":{\n" + " \"type\":\"keyword\"\n" + " },\n" + " \"description\":{\n" + " \"type\":\"text\",\n" + " \"analyzer\":\"ik_max_word\",\n" + " \"copy_to\":\"all\"\n" + " },\n" + " \"all\":{\n" + " \"type\":\"text\",\n" + " \"analyzer\":\"ik_max_word\"\n" + " }\n" + " }\n" + " }\n" + "}"; request.source(json, XContentType.JSON); //获取操作索引的客户端对象,调用创建索引操作 client.indices().create(request, RequestOptions.DEFAULT); }
//添加全部文档 @Test void testCreateDocAll() throws IOException { ListbookList = bookMapper.selectList(null); BulkRequest bulk = new BulkRequest(); for (Book book : bookList) { IndexRequest request = new IndexRequest("books").id(book.getId().toString()); //对象转json String json = JSON.toJSONString(book); request.source(json,XContentType.JSON); bulk.add(request); } client.bulk(bulk,RequestOptions.DEFAULT); }
//按id查询文档 @Test void testGrtById() throws IOException { GetRequest request = new GetRequest("books","1"); GetResponse documentFields = client.get(request, RequestOptions.DEFAULT); String sourceAsString = documentFields.getSourceAsString(); System.out.println(sourceAsString); } //按条件查询文档 @Test void testSearch() throws IOException { //创建查books的索引 SearchRequest searchRequest = new SearchRequest("books"); //设置查询条件 SearchSourceBuilder builder = new SearchSourceBuilder(); builder.query(QueryBuilders.termQuery("all","java")); searchRequest.source(builder); SearchResponse search = client.search(searchRequest, RequestOptions.DEFAULT); SearchHits hits = search.getHits();//命中的数据 for (SearchHit hit : hits) { String sourceAsString = hit.getSourceAsString(); //System.out.println(sourceAsString); //json转对象 Book book = JSON.parseObject(sourceAsString, Book.class); System.out.println(book); } }