Elasticsearch 查询
1.新建一个数据库,将 在Redis模糊查询案例中的分词,导入到新的数据库
2. 新建springboot工程,逆向工程连接数据库生成controller,service,dao层
spring版本为1.5
3. 下载Elasticsearch 并运行
Elasticsearch版本号为2.4.6
解压后,在bin目录下,点击elasticsearch.bat 运行
在浏览器输入http://localhost:9200/
4. 把数据从数据库导入到Elasticsearch
- extends ElasticsearchRepository(本文使用)
- 注入elasticsearchTemplate
新建一个package
public interface PoetryRepository extends ElasticsearchRepository<EsPoetry, Integer{
//按content做模糊查询
public List<EsPoetry> findByContentIn(String word);
}
@Document(indexName = "test", type = "poetry")
public class EsPoetry implements Serializable {
@Id
private Integer id;
private String title;
private String author;
private String kind;
private String intro;
@Field(type = FieldType.String, analyzer = "ik_smart")//后续加入了ik分词器在serch里
private String content;
private static final long serialVersionUID = 1L;
/**
* @return id
*/
public Integer getId() {
return id;
}
/**
* @param id
*/
public void setId(Integer id) {
this.id = id;
}
/**
* @return title
*/
public String getTitle() {
return title;
}
/**
* @param title
*/
public void setTitle(String title) {
this.title = title;
}
/**
* @return author
*/
public String getAuthor() {
return author;
}
/**
* @param author
*/
public void setAuthor(String author) {
this.author = author;
}
/**
* @return kind
*/
public String getKind() {
return kind;
}
/**
* @param kind
*/
public void setKind(String kind) {
this.kind = kind;
}
/**
* @return intro
*/
public String getIntro() {
return intro;
}
/**
* @param intro
*/
public void setIntro(String intro) {
this.intro = intro;
}
/**
* @return content
*/
public String getContent() {
return content;
}
/**
* @param content
*/
public void setContent(String content) {
this.content = content;
}
}
@Document(indexName = “knowledge”,type=“poetry”)
这个注解是ElasticsearchRepository要求管理的实体类需要打上这个注解,
-
indexName是索引名,对应关系型数据库的db名
-
type在非关系型数据库叫数据类型,对应关系型数据库的table名
在controller层写逻辑,将数据库的数据导入Elastic
@Controller
@RequestMapping("/moon/Poetry")
public class PoetryController extends BaseController {
@Autowired
private PoetryService poetryService;
@Autowired
private PoetryRepository poetryRepository;
@RequestMapping("/initdata")
public String initdata() {
List<Poetry> list = poetryService.getPoetryMapper().selectAll();
list.forEach(o -> {
EsPoetry esPoetry = new EsPoetry();
BeanUtils.copyProperties(o, esPoetry);
poetryRepository.save(esPoetry);
System.out.println("导入到es:" + esPoetry.getTitle());
});
return "demo/success";
}
}
运行成功后
打印
导入到es:感遇·其一
导入到es:塞下曲·其一
导入到es:古从军行
导入到es:感遇·其二
...
5. 写一个模糊查询
一个搜索的页面代码
<div class="form-inline mt-2">
<input id="word" type="text" class="form-control form-control-sm" placeholder="_输入关键词"/>
<button class="btn btn-sm btn-outline-success ml-1" @click="showdata();">查询</button>
</div>
<div>
<div class="card border-light m-1" v-for="p in poetrydata">
<div class="card-header font-weight-bold">{{p.title}}</div>
<div class="card-body">{{p.content}}</div>
</div>
</div>
<script th:inline="javascript">
//app
var app = new Vue({
el: "#app",
data: {
poetrydata: []
},
methods: {
showdata: function () {
$.post("/moon/Poetry/selectByWord", {word: $("#word").val()}, function (data) {
app.poetrydata = data;
});
}
}
});
</script>
controller
//模糊查询
@RequestMapping("/selectByWord")
@ResponseBody
public List<EsPoetry> selectByWord(String word) {
if (word == null) {
return null;
}
return poetryRepository.findByContentIn(word);
//return null;
}
运行后
6.加入ik分词器
略