一 .下载ElasticSearch 5.6.8版本 https://www.elastic.co/downloads/past-releases/elasticsearch-5-6-8 二 .在命令提示符下,进入ElasticSearch安装目录下的bin目录,执行命令
elasticsearch
即可启动。 我们打开浏览器,在地址栏输入http://127.0.0.1:9200/ 即可看到输出结果
代码语言:javascript复制{
"name" : "y65yr7D",
"cluster_name" : "elasticsearch",
"cluster_uuid" : "hDRgt56QTvu-Ihv8LqZvcg",
"version" : {
"number" : "5.6.8",
"build_hash" : "688ecce",
"build_date" : "2018-02-16T16:46:30.010Z",
"build_snapshot" : false,
"lucene_version" : "6.6.1"
},
"tagline" : "You Know, for Search"
如果想用可视化的head,可以进入head目录启动head,在命令提示符下输入命令 grunt server 打开浏览器,输入 http://localhost:9100
Postman调用RestAPI
1 新建索引 例如我们要创建一个叫articleindex的索引 ,就以put方式提交
http://127.0.0.1:9200/articleindex/
2 新建文档
新建文档: 以post方式提交 http://127.0.0.1:9200/articleindex/article body:
代码语言:javascript复制{
"title":"SpringBoot2.0",
"content":"区别和练习"
}
返回结果如下:
代码语言:javascript复制{
"_index": "articleindex",
"_type": "article",
"_id": "AWPKsdh0FdLZnId5S_F9",
"_version": 1,
"result": "created",
"_shards": { "total": 2, "successful": 1, "failed": 0 },
"created": true
}
_id是由系统自动生成的。 为了方便之后的演示,我们再次录入几条测试数据
3 查询全部文档 ,在type后加_search查询所有文档
查询某索引某类型的全部数据,以get方式请求 http://127.0.0.1:9200/tensquare_article/article/_search 返回结果如下:
4 修改文档 以put形式提交以下地址:
http://192.168.184.134:9200/articleindex/article/AWPKrI4pFdLZnId5S_F7
Java中我们使用步骤为:
elasticsearch配置
(1)创建模块tensquare_search,pom.xml引入依赖
代码语言:javascript复制 <dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring‐data‐elasticsearch</artifactId> </dependency>
<dependency>
<groupId>com.tensquare</groupId>
<artifactId>tensquare_common</artifactId>
<version>1.0‐SNAPSHOT</version>
</dependency>
</dependencies>
(2)application.yml配置
代码语言:javascript复制service:
port: 9007
spring:
application:
name: tensquare‐search #指定服务名 data:
elasticsearch:
cluster‐nodes: 127.0.0.1:9300
#注意elasticsearch使用中 java代码访问elasticsearch时候用9300端口,网站和其他应用访问时候用9200端口
(3)创建包com.tensquare.search,包下创建启动类
代码语言:javascript复制 @SpringBootApplication
public class RecruitApplication {
public static void main(String[] args) {
SpringApplication.run(RecruitApplication.class, args); }
@Bean
public IdWorker idWorkker(){
return new IdWorker(1, 1); }
}
使用elasticsearch做添加文章功能模块
(1)创建实体类 创建com.tensquare.search.pojo包,包下建立类
代码语言:javascript复制/*
* 文章实体类
*/
@Document(indexName="tensquare",type="article")
public class Article implements Serializable{
@Id
private String id;//ID
@Field(index= true ,analyzer="ik_max_word",searchAnalyzer="ik_max_word")
private String title;//标题
@Field(index= true ,analyzer="ik_max_word",searchAnalyzer="ik_max_word") private String content;//文章正文
private String state;//审核状态
//getter and setter ......
(2)创建数据访问接口 创建com.tensquare.search.dao包,包下建立接口
代码语言:javascript复制 * 文章数据访问层接口
*/
public interface ArticleSearchDao extends ElasticsearchRepository<Article,String> {
}
(3)创建业务逻辑类 创建com.tensquare.search.service包,包下建立类
代码语言:javascript复制 @Service
public class ArticleSearchService {
@Autowired
private ArticleSearchDao articleSearchDao;
/**
* 增加文章
* @param article
*/
public void add(Article article){
articleSearchDao.save(article);
}
}
(4)创建控制器类 创建com.tensquare.search.controller包,包下建立类
代码语言:javascript复制@RestController
@CrossOrigin
@RequestMapping("/article")
public class ArticleSearchController {
@Autowired
private ArticleSearchService articleSearchService;
@RequestMapping(method= RequestMethod.POST)
public Result save(@RequestBody Article article){
articleSearchService.save(article);
return new Result(true, StatusCode.OK, "操作成功");
}
}
5.2.3文章搜索 (1)ArticleSearchRepository新增方法定义
代码语言:javascript复制 * 检索
* @param
* @return
*/
public Page<Article> findByTitleOrContentLike(String title,String content,Pageable pageable);
(2)ArticleSearchService新增方法
代码语言:javascript复制 public Page<Article> findByTitleLike(String keywords, int page, int size){
PageRequest pageRequest = PageRequest.of(page‐1, size);
return articleSearchRepository.findByTitleOrContentLike(keywords,keywords, pageRequest);
}
(3)ArticleSearchController方法
代码语言:javascript复制@RequestMapping(value="/search/{keywords}/{page}/{size}",method= RequestMethod.GET)
public Result findByTitleLike(@PathVariable String keywords, @PathVariable int page, @PathVariable int size){
Page<Article> articlePage = articleSearchService.findByTitleLike(keywords,page,size);
return new Result(true, StatusCode.OK, "查询成功",
new PageResult<Article>(articlePage.getTotalElements(), articlePage.getContent()));
}