ElasticSesrch入门
标签(空格分隔):ElasticSearch
安装
- elasticsearch
官网下载地址,选择你想要的版本下载点击这里
1.8以上
\elasticsearch-5.5.3\bin\elasticsearch.bat
- elasticsearch-head
github 下载 地址
nodejs 8.0以上版本
cnpm install
cnpm run start
注意,由于elasticsearch和elasticsearch-head是两个产品,存在跨域操作,所以需要在elasticsearch的config路径下修改yml文件。
\elasticsearch-5.5.3\config
http.cors.enabled: true
http.cors.allow-origin: "*"
http.cors.allow-methods: OPTIONS,HEAD,GET,POST,PUT,DELETE
http.cors.allow-headers: "X-Requested-With,Content-Type,Content-Length,X-Use"
分布式
- 集群搭建
cluster.name: xiaojinzi
node.name: master
node.master:true
network.host: 127.0.0.1
cluster.name: xiaojinzi
node.name: slave1
network.host: 127.0.0.1
http.port: 8200
discovery.zen.ping.unicast.hosts: ["127.0.0.1"]
基础概念(这里引用之前所写python搭建es)
集群:一个或多个节点组织在一起
节点:一个节点是一个集群中的一个服务器,有表示,是漫v的名字
分片:将索引划分为多份的能力,允许水平分割和扩展容量,多个分片响应请求,提高性能和吞吐量
-
副本:数据的备份,一个节点宕机,另一个顶上
elasticsearch mysql index (索引) 数据库 type(类型) 表 document(文档) 行 fields 列 倒排索引源于实际应用中需要根据属性的值来查找记录。这种索引表中的每一项都包含一个属性值和具有该属性值得各记录的地址。由于不是由记录来确定属性值,而是由属性值来确定记录的位置,因而称为倒排索引,带有倒排索引的文件我们称为倒排索引文件,简称倒排文件。
TF-IDF
基本用法
- 索引创建
API基本格式: http://:/<索引>/<类型>/<文档id>
常用HTTP动词:GET/PUT/POST/DELETE
- 结合postman
127.0.0.1:9200/people
{
"settings":{
"number_of_shards":3,
"number_of_replicas":1
},
"mappings":{
"man":{
"properties":{
"name":{
"type":"text"
},
"age":{
"type":"integer"
},
"countary":{
"type":"keyword"
},
"date":{
"type":"date",
"format":"yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
}
}
}
}
}
- 插入操作
127.0.0.1:9200/people/man/1
{
"name":"xiaojinzi",
"age":23,
"countary":"China",
"date":"1994-12-12"
}
127.0.0.1:9200/people/man
{
"name":"bai_xiaojinzi",
"age":33,
"countary":"China",
"date":"1984-12-12"
}
- 修改操作
127.0.0.1:9200/people/man/1/_update
{
"doc":{
"name":"xiaojinziya"
}
}
127.0.0.1:9200/people/man/1/_update
{
"script":{
"lang":"painless",
"inline":"ctx._source.age +=10"
}
}
{
"script":{
"lang":"painless",
"inline":"ctx._source.age = params.age"
"params":{
"age":100
}
}
}
- 删除操作
127.0.0.1:9200/people
127.0.0.1:9200/people/man/1
- 查询
127.0.0.1:9200/people/man/1
127.0.0.1:9200/people/_search
{
"query":{
"match_all":{}
},
"from":1,
"size":1
}
{
"query":{
"match":{
"name":"xiao"
}
},
"sort":[
{
"age":{
"order":"desc"
}
}
]
}
127.0.0.1:9200/people/_search
{
"aggs":{
"groud_by_name_count":{
"terms":{
"filed":"name"
}
}
}
}
{
"aggs":{
"grads_by_age":{
"stats":{
"filed":"age"
}
}
}
}
高级查询
子条件查询-特定字段查询所指特定值,符合条件查询-以一定的逻辑组合条件查询
- query
生成一个_score来标识字段匹配的程度,用于匹配结果与查询条件匹配的耦合度。
127.0.0.1:9200/people/_search
{
"query":{
"match":{
"name":"xiao"
}
}
}
{
"query":{
"match_phrase":{
"name":"xiao"
}
}
}
{
"query":{
"muilt_match":{
"query":"xiao"
},
"filed":["name","countary"]
}
}
{
"query":{
"query_string":{
"query":"(xiao AND jin) OR zi"
}
}
}
{
"query":{
"term":{
"age":23
}
}
}
{
"query":{
"range":{
"age":{
"gte":20,
"lte":25
}
}
}
}
- filter
查询满足条件,返回是否。
127.0.0.1:9200/people/_search
{
"query":{
"bool":{
"filter":{
"term":{
"age":23
}
}
}
}
}
- 符合查询
127.0.0.1:9200/people/_search
{
"query":{
"constant_score":{
"filter":{
"match":{
"name":"xiao"
}
},
"boost":2
}
}
}
{
"query":{
"bool":{
"should":[{
"match":{
"name":"xiao"
},
"match":{
"date":"1994-12-12"
}
}]
}
}
}
{
"query":{
"bool":{
"must":[{
"match":{
"name":"xiao"
},
"match":{
"date":"1994-12-12"
}
}],
"filter":{
"age":23
}
}
}
}
{
"query":{
"bool":{
must_not:{
"term":{
"name":"xiao"
}
}
}
}
}
springboot 集成elasticsearch
- 依赖
5.5.3
org.elasticsearch.client
transport
${elasticsearch.version}
org.apache.logging.log4j
log4j-core
2.7
org.projectlombok
lombok
- 日志文件配置
appender.console.type = Console
appender.console.name = Console
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = [%t] %-5p $c -%m%n
rootLogger.level = info
rootLogger.appenderRef.console.ref = console
- 配置类
server:
port: 8090
context-path: /esdemo
es:
url_ip: 127.0.0.1
url_port: 9300
node_cluster: cluster.name
node_name: xiaojinzi
@ConfigurationProperties(prefix = "es")
@Data
@Component
public class EsClientConfig {
/** 链接ip .*/
private String url_ip;
/** 端口 .*/
private Integer url_port;
/** 集群命名 .*/
private String node_cluster;
/** 节点名称 .*/
private String node_name;
}
@Configuration
public class EsClientBeanConfig {
@Autowired
private EsClientConfig esClientConfig;
@Bean
public TransportClient client() throws UnknownHostException{
/** 节点设置 .*/
InetSocketTransportAddress node = new InetSocketTransportAddress(
InetAddress.getByName(esClientConfig.getUrl_ip()),
esClientConfig.getUrl_port()
);
/** 参数设置 .*/
Settings settings = Settings.builder().put(esClientConfig.getNode_cluster(),esClientConfig.getNode_name()).build();
TransportClient client = new PreBuiltTransportClient(settings);
/** 节点导入 .*/
client.addTransportAddress(node);
return client;
}
}
- 查询接口编写
@RestController
@RequestMapping("/es")
public class EsApiController {
@Autowired
private TransportClient client;
/**
* 查询
* @param id
* @return
*/
@GetMapping("/get")
public ResponseEntity get(@RequestParam("id")String id){
if(id.isEmpty()){
return new ResponseEntity(HttpStatus.NOT_FOUND);
}
GetResponse result = this.client.prepareGet("people","man",id).get();
if(result.getSource().isEmpty()){
return new ResponseEntity(HttpStatus.NOT_FOUND);
}
return new ResponseEntity(result.getSource(),HttpStatus.OK);
}
}
- 增加接口
@Data
public class ManForm {
/** 主键 .*/
private String id;
/** 名字 .*/
private String name;
/** 国家 .*/
private String countary;
/** 出生日期 .*/
@DateTimeFormat(pattern = "yyyy-MM-dd")
private Date date;
/** 年龄 .*/
private Integer age;
}
@PostMapping("/add")
public ResponseEntity add(@Valid ManForm manForm, BindingResult bindingResult){
if (bindingResult.hasErrors()){
return new ResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR);
}
try {
/** json 封装 .*/
XContentBuilder content = XContentFactory.jsonBuilder().startObject().
field("name", manForm.getName()).
field("age", manForm.getAge()).
field("date", manForm.getDate().getTime()).
field("countary", manForm.getCountary()).
endObject();
/** 添加文档 .*/
IndexResponse result = client.prepareIndex("people","man").setSource(content).get();
return new ResponseEntity(result.getId(),HttpStatus.OK);
}catch (IOException e){
e.printStackTrace();
return new ResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR);
}
}
- 删除操作
/**
* 根据id删除
* @param id
* @return
*/
@DeleteMapping("/delete")
public ResponseEntity delete(@RequestParam("id")String id){
DeleteResponse result = client.prepareDelete("people","man",id).get();
return new ResponseEntity(result.getResult().toString(),HttpStatus.OK);
}
- 更新操作
@PutMapping("/update")
public ResponseEntity update(@Valid ManForm manForm,BindingResult bindingResult){
if(bindingResult.hasErrors()){
return new ResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR);
}
/** 构建更新 .*/
UpdateRequest request = new UpdateRequest("people","man",manForm.getId());
try {
/** json .*/
XContentBuilder builder = XContentFactory.jsonBuilder().startObject();
if(manForm.getName()!= null){
builder.field("name",manForm.getName());
}
if(manForm.getAge()>0){
builder.field("age",manForm.getAge());
}
if(manForm.getCountary()!=null){
builder.field("countary",manForm.getCountary());
}
if(manForm.getDate()!=null){
builder.field("date",manForm.getDate().getTime());
}
builder.endObject();
request.doc(builder);
}catch (IOException e){
e.printStackTrace();
return new ResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR);
}
try {
/** 更新操作 .*/
UpdateResponse result = client.update(request).get();
return new ResponseEntity(result.getResult().toString(),HttpStatus.OK);
}catch (Exception e){
e.printStackTrace();
return new ResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR);
}
}
- 复合查询
@PostMapping("/query")
public ResponseEntity query(@RequestParam(name="name",required = false)String name,
@RequestParam(name = "countary",required = false)String countary,
@RequestParam(name = "age_lte",required = false)Integer lte,
@RequestParam(name = "age_gte",defaultValue = "0")int gte){
/** bool查询构建 .*/
BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
if(name!=null){
/** 条件匹配 .*/
boolQuery.must(QueryBuilders.matchQuery("name",name));
}
if(countary!=null){
boolQuery.must(QueryBuilders.matchQuery("countary",countary));
}
/** 范围构建 .*/
RangeQueryBuilder range = QueryBuilders.rangeQuery("age").from(gte);
if(lte!= null && lte>0 ){
range.to(lte);
}
/** filter条件添加 .*/
boolQuery.filter(range);
/** 搜索条件构建 .*/
SearchRequestBuilder builder = client.prepareSearch("people").
setTypes("man").
setSearchType(SearchType.DFS_QUERY_THEN_FETCH).
serQuery(boolQuery).
setFrom(0).
setSize(10);
System.out.println(builder.get());
/** 结果返回 .*/
SearchResponse response = builder.get();
/** 遍历hits .*/
List
- 原视频UP主慕课网(ElasticSearch入门)
- 项目代码上传Git 期待你的star
- 本篇博客撰写人: XiaoJinZi 转载请注明出处
- 学生能力有限 附上邮箱: [email protected] 不足以及误处请大佬指责