试想一下。若是咱们想找一下含有“月”的古诗,咱们会先去想哪些诗句包含月,例如“床前明月光”,包含月。这么在咱们脑海里就有“床前明月光”为KEY,“月”为Value的索引。html
什么是倒索引。若是咱们事先在脑海里创建了,“月”为KEY,“静夜思”为Value的索引,这个问题也就不假思索的回答。这就是倒索引。java
Lucene是如何作到的。创建索引分为一下几步:mysql
Near Realtime(NRT)git
近实时,es从数据写入到数据被搜索到有一个延时(大概1秒),基于es执行的搜索和分析能够达到妙级github
Clustersql
集群,包含多个节点,每一个节点属于哪一个集群是经过一个配置(集群名称)来决定,节点能够分散到各个机器上。chrome
Node数据库
节点,集群中的一个节点,若是默认启动1个或者多个节点,那么他们自动组成一个集群。一个elasticsearch实例即就是一个节点。每一个节点能够有多个shard,可是primary shard和对应replica shard 不能在同一个节点上。json
indexbash
索引,包含一堆具备类似结构的文档数据
type
类型,type是index中的一个逻辑数据分类,7.X版本只有一个
document
文档,es中最小的数据单元,有json串组成,里面包含多个field,每一个field便是一个数据字段。
mapping
映射,一旦创建,没法修改,只能删除索引,从新创建,或者冗余数据。text:当一个字段是要被全文搜索的。keyword:类型适用于索引结构化的字段。字段只能经过精确值搜索到。
聚合-桶 聚合-指标
分类成为桶,统计成为指标
ES的安装简单,chrome插件elasticsearch-head从git上找https://github.com/mobz/elasticsearch-head/tree/master/crx。crx文件,解压成文件夹,导入插件。
版本变更很大,全部的API查官网的一手资料才是最准确的, 官方Search APIs:www.elastic.co/guide/en/el…
/test
{
"settings": {
"number_of_shards": 3,
"number_of_replicas": 2
},
"mappings": {
"properties": {
"field1": {
"type": "text"
}
}
}
}
复制代码
分页
:9200/movie/adventure/_search?from=1&size=5
查询某列
:9200/movie/adventure/1?_source=name
字符串查询
:9200/movie/adventure/_search?q=name:life
DSL搜索
{
"query": {
"match": {
"tag": "中国"
}
}
}
复制代码
正浮点型,分数越高,文档越相关
GET /_search
{
"query": {
"bool": {
"must": [
{ "match": { "title": "Search" }},
{ "match": { "content": "Elasticsearch" }}
],
"filter": [
{ "term": { "status": "published" }},
{ "range": { "publish_date": { "gte": "2015-01-01" }}}
]
}
}
}
复制代码
##### 实现如下sql
SELECT product
FROM products
WHERE (price = 20 OR productID = "XHDK-A-1293-#fJ3")
AND (price != 30)
##### 布尔过滤器, 过滤查询已被弃用,并在ES 5.0中删除。
必须(must) 匹配
不能(must not) 匹配
至少有一个语句要匹配
{
"bool" : {
"must" : [],
"should" : [],
"must_not" : [],
}
}
##### 查询
GET /my_store/products/_search
{
"query" : {
"bool" : {
"should" : [
{ "term" : {"price" : 20}},
{ "term" : {"productID" : "XHDK-A-1293-#fJ3"}}
],
"must_not" : {
"term" : {"price" : 30}
}
}
}
}
复制代码
官方文档 www.elastic.co/guide/en/el… 2.X版本和5版本,中文官方 www.elastic.co/guide/cn/el…
Elasticsearch多表关联问题是讨论最多的问题之一。ES能够实现关联查询,可是不推荐使用,性能也会有影响。
三种解决方案:
参考链接:blog.csdn.net/laoyang360/…
创建索引
/lhhb-order
{
"settings": {
"number_of_shards": 3,
"number_of_replicas": 2
},
"mappings": {
"properties": {
"cashname": {
"type": "keyword"
},
"memo_no": {
"type": "keyword"
},
"payment": {
"type": "double"
},
"create":{
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
},
"remark":{
"type": "text"
}
}
}
}
添加数据
/lhhb-order/_doc/1
{
"cashname": "人民币",
"memo_no": "1233455754534",
"payment": 10000.12345,
"create":"2015-01-01 23:33:12",
"remark":"备注啊书法大赛打发士大夫"
}
基本查询
term是精确查询
match是模糊查询
gt : 大于
lt : 小于
gte : 大于等于
lte :小于等于
{
"query": {
"bool":{
"must": [
{ "range": { "create": {"gte": "2015-02-01"} }}
]
}
}
}
{
"query": {
"bool": {
"must": [
{ "match": { "title": "Search" }},
{ "match": { "content": "Elasticsearch" }}
],
"filter": [
{ "term": { "status": "published" }},
{ "range": { "publish_date": { "gte": "2015-01-01" }}}
]
}
}
}
聚合: 分类统计
{
"size" : 0,
"aggs" : {
"popular_colors" : {
"terms" : {
"field" : "color.keyword"
}
}
}
}
复制代码
logstash-7.3.2\bin 安装
logstash-plugin install logstash-input-jdbc
目录下放驱动文件
文件格式UTF-8
input {
jdbc {
jdbc_driver_library => "mysql-connector-java-5.1.33-bin.jar"
jdbc_driver_class => "com.mysql.jdbc.Driver"
jdbc_connection_string => "jdbc:mysql://IP:3306/test"
jdbc_user => "root"
jdbc_password => "****"
schedule => "*/1 * * * *"
jdbc_default_timezone => "Etc/UTC"
statement => "SELECT * FROM biz_transfer_info where id > :sql_last_value"
jdbc_paging_enabled => "true"
jdbc_page_size => "50000"
last_run_metadata_path => "E:\cyd\WorkPro\ELK\logstash-7.3.2\logstash-7.3.2\log\mysql_test.txt"
use_column_value => true
record_last_run => true
tracking_column => "id"
}
}
output {
stdout {
codec => json_lines
}
elasticsearch {
hosts => "127.0.0.1:9200"
index => "mysql_test"
}
}
复制代码
.\logstash.bat -f .\mysqldata.conf
最好参考官方文档
官网上的安装和运行都很简单。
kibana报表截图:elkguide.elasticsearch.cn/kibana/phan…