博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ElasticSearch入门
阅读量:6574 次
发布时间:2019-06-24

本文共 4990 字,大约阅读时间需要 16 分钟。

  hot3.png

安装

单实例安装

mkdir imooccd imoocbrew install wgetwget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.0.0-darwin-x86_64.tar.gzlstar -vxf elasticsearch-7.0.0-darwin-x86_64.tar.gzcd elasticsearch-7.0.0java -versionsh ./bin/elasticsearchlocalhost:9200
cd elasticsearch-7.0.0lsvim config/elasticsearch.ymlhttp.cors.enabled: truehttp.cors.allow-origin: "*"./bin/elasticsearch -d

插件安装

github -> mobz/elasticsearch-head

cd imoocwget https://github.com/mobz/elasticsearch-head/archive/master.ziplsunzip master.ziplscd elasticsearch-head-masterlsnode -vnpm installnpm run startlocalhost:9100

分布式安装

cd elasticsearch-7.0.0vim config/elasticsearch.ymlcluster.name: bingnode.name: masternode.master: truenetwork.host: 127.0.0.1ps -ef | grep `pwd`kill -9 5531./bin/elasticsearch -dclear
cd imoocmkdir es_slavecp elasticsearch-7.0.0-darwin-x86_64.tar.gz es_slavecd es_slavelstar -vxf elasticsearch-7.0.0-darwin-x86_64.tar.gzcp -r elasticsearch-7.0.0 es_slave1cp -r elasticsearch-7.0.0 es_slave2cd es_slave1vim config/elasticsearch.ymlcluster.name: bingnode.name: slave1network.host: 127.0.0.1http.port: 10200discovery.zen.ping.unicast.hosts: ["127.0.0.1"]./bin/elasticsearch -dcd es_slave2vim config/elasticsearch.ymlcluster.name: bingnode.name: slave2network.host: 127.0.0.1http.port: 11200discovery.zen.ping.unicast.hosts: ["127.0.0.1"]./bin/elasticsearch -d

基础概念

集群/节点/索引(数据库)/类型(表)/文档(记录)/分片/备份

基本用法

索引创建

api格式:http://<ip>:<port>/<索引>/<类型>/<文档id> 非格式化索引: -> 索引 -> 新建索引 格式化索引:postman->put: -> body -> raw

{	"settings": {		"number_of_shards": 3,		"number_of_replicas": 1	},	"mappings": {		"properties": {			"name": {				"type": "text"			},			"country": {				"type": "keyword"			},			"age": {				"type": "integer"			},			"date": {				"type": "date",				"format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"			}		}	}}

插入

post: put:

{	"name": "walili",	"country": "China",	"age": 23,	"date": "1988-08-08" }

修改

put:

删除

  • 删除文档 delete:
  • 删除索引 delete:

查询

  • 简单查询 get:
  • 条件查询 get:
{	"query": {		"match_all": {}	}}
{	"query": {		"match_all": {}	},	"from": 0,	"size": 1}
{	"query": {		"match": {			"name": "walili"		}	}}
{	"query": {		"match": {			"country": "China"		}	},	"sort": [		{			"date": {"order": "desc"}		}	]}
  • 聚合查询
{	"aggs": {		"group_by_age": {			"terms": {				"field": "age"			}		},		"group_by_date": {			"terms": {				"field": "date"			}		}	}}
{	"aggs": {		"grades_age": {			"stats": {				"field": "age"			}		}	}}
{	"aggs": {		"grades_age": {			"min": {				"field": "age"			}		}	}}

高级查询

query

get:

  • 全文本查询:准对文本型数据
  • 字段级别查询:针对结构化数据

模糊查询

{	"query": {		"match": {			"name": "wali大师"		}	}}

短语查询

{	"query": {		"match_phrase": {			"name": "wali大师"		}	}}

多个字段模糊查询

{	"query": {		"multi_match": {			"query": "wali",			"fields": ["name", "country"]		}	}}

语法查询

{	"query": {		"query_string": {			"query": "lili OR 大师",			"fields": ["name", "country"]		}	}}

字段查询

{	"query": {		"range": {			"age": {				"gte": "30",				"lte": "100"			}		}	}}

filter

{	"query": {		"bool": {			"filter": {				"term": {					"age": "77"				}			}		}	}}

复合查询

get:

{	"query": {		"match": {			"name": "wali"		}	}}

固定分数查询

{	"query": {		"constant_score": {			"filter": {				"match": {					"age": "77"				}			},			"boost": 2		}	}}

布尔查询

{	"query": {		"bool": {			"should": [				{					"match": {						"name": "wali"					}				},				{					"match": {						"country": "大师"					}				}			]		}	}}
{	"query": {		"bool": {			"must": [				{					"match": {						"name": "wali"					}				},				{					"match": {						"country": "大师"					}				}			]		}	}}
{	"query": {		"bool": {			"must_not": [				{					"term": {						"name": "wali"					}				}			]		}	}}

SB 集成 ES

SB 集成 ES

1.8
5.5.2
...
org.elasticsearch.client
transport
org.apache.logging.log4j
log4j-core
2.7
package com.qianbill.admin_api;import org.elasticsearch.client.transport.TransportClient;import org.elasticsearch.common.settings.Settings;import org.elasticsearch.common.transport.InetSocketTransportAddress;import org.elasticsearch.transport.client.PreBuiltTransportClient;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import java.net.InetAddress;import java.net.UnknownHostException;/** * @Auther: beanho * @Date: 2019/4/17 15:51 * @Description: */@Configurationpublic class ElasticSearch {    @Bean    public TransportClient client() throws UnknownHostException {        InetSocketTransportAddress node = new InetSocketTransportAddress(                InetAddress.getByName("localhost"),                9300        );        Settings settings = Settings.builder()                .put("cluster.name", "bing")                .build();        TransportClient client = new PreBuiltTransportClient(settings);        client.addTransportAddress(node);        return client;    }}

查询接口开发

增加接口开发

删除接口开发

更新接口开发

复合查询接口开发

总结

转载于:https://my.oschina.net/beanho/blog/3037577

你可能感兴趣的文章
创建触发器的基本语法
查看>>
2015.1.15 利用Oracle函数返回表结果 重大技术进步!
查看>>
2015.3.2 VC++6制作非MFC dll以及VS2005、VS2010调用
查看>>
转:模态对话框的支持 (IE,Firefox,Chrome)
查看>>
让您的电脑在任意目录可以支持图片的粘贴,试试看呗~
查看>>
Jenkins+QTP自动化测试框架
查看>>
《Node.js In Action》笔记之流程控制
查看>>
C++类和对象
查看>>
3518EV200 SDK学习1
查看>>
JavaScript初学者应注意的七个细节
查看>>
1163: 零起点学算法70——Yes,I can!
查看>>
zookeeper原理及作用
查看>>
[ZJOI2015]诸神眷顾的幻想乡
查看>>
2018-2019-2 网络对抗技术 20165318 Exp1 PC平台逆向破解
查看>>
关于图片或者文件在数据库的存储方式归纳
查看>>
存储过程和SQL语句比较及存储过程在C#中调用方法
查看>>
hihocoder 1014 Trie树
查看>>
ADO.NET笔记——使用DataSet返回数据
查看>>
【机器学习】--关联规则算法从初识到应用
查看>>
windows 下nginx php安装
查看>>