elasticsearch 第一讲

启动es的方式 sh bin/elasticsearchgit

es插件

查看插件 sh bin/elasticsearch-plugin -h 查看帮忙文档
sh bin/elasticsearch-plugingithub

  • list 查看插件列表
  • install 安装插件
  • remove 移除插件

查看插件 http://localhost:9200/_cat/pluginsjson

多实例方式运行es
安装logstash

下载 https://www.elastic.co/cn/downloads/logstash
下载 导入数据 https://grouplens.org/datasets/movielens/api

es 使用方式

es index => table document => 行数据
get /movies/_count 获取总数量
get /movies/ 获取 mapping和 setting信息数组

es 增删改查
es新增 当前id为自动生成
POST users/_doc
{
  "user":"Mike",
  "post_date":"2019-04-15T14:12:12",
  "message": "tring out kibana"
}
es新增若是存在则不建立
PUT users/_doc/1?op_type=create
{
  "user" : "Jack",
  "post_date" : "2019-04-15T14:12:12",
  "message" : "trying out Elasticsearch"
}
es新增 当前id为指定, 若是存在则覆盖 version + 1
PUT users/_doc/1
{
  "user":"Mike"
}
es 修改 指定当前id,若是当前字段存在则修改,若是不存在则追加
POST users/_update/1
{
    "doc":{
        "user":"test",
        "post_date":"2019-04-15T14:12:12",
        "message":"trying out Elasticsearch"
    }
}
获取对象的信息
GET users/_doc/1
删除数据
DELETE users/_doc/1
删除索引
DELETE users
查看分词信息
standard 分词器

按照 空格 转换成小写 中文 非 _ 分割
包含数字 英文app

simple 分词器

按照 空格 不包含数字 转换大小写为小写curl

whitespace 分词器

按照 空格分割 不转换大小写 - 符号也不进行分割elasticsearch

stop 分词器

按照 空格 - 分割 不包含数字和 副词(in the)post

keyword 分词器

不进行分词学习

中文分词

standard 分词的时候 分解成 单独的词语
icu_analyzer 中文分词
ik 中文分词 安装方式 链接地址
ik_max_word 最细粒度 分词
ik_smart 粗粒度 分词

POST _analyze
{
    "analyzer":"standard",
    "text":"2 running Quick brown-foxes leap over lazy dogs in the summer evening"
}
search api的使用
URI search 在url中使用参数查询
/kibana_sample_data_ecommerce/_search?q=customer_first_name:Eddie

q 表示查询的意思 使用的是 Query String syntax
df 表示指定默认查询字段 默认是 全部字段
sort 排序字段
profile 能够查看 查询是如何执行的
() 的做用是表示或者,例如 q=title:(a b) 表示title为a 或者 b,若是为 q=title:a b 则表示 title 为a 其余的字段只要包含 b就算搜索的数据

表示全部字段中包含 2012的数据
/movies/_search?q=2012
{
    "profile":true
}

表示title中包含2012的数据
/movies/_search?q=2012&df=title 
{
    "profile":true
}

/movies/_search?q=title:2012
{
    "profile":true
}

//表示 title 字段包含 beautiful 的或者 其余的字段中存在 mind的数据
/movies/_search?q=title:beautiful mind
{
    "profile":true
}

//表示 title 字段包含 beautiful mind的数据。当前为严格查询 必须的包含 beautiful mind 的数据; PhraseQuery
/movies/_search?q=title:"beautiful mind"
{
    "profile":true
}

//表示的是 title中包含 beautiful或者 title中包含 mind的数据
/movies/_search?q=title:(beautiful mind)
{
    "profile":true
}

//表示 获取包含mind的数据,当前 包含beautiful的数据排名靠前,可是具体还须要再肯定
/movies/_search?q=title:(beautiful %2Bmind)
{
    "profile": true
}

//表示字段 titile中既要包含 beautiful 又要包含 mind 不分先后,这个是和 "beautiful mind" 的区别
/movies/_search?q=title:(beautiful AND mind)
{
    "profile": true
}

//表示字段 title中包含 beautiful可是不包含mind的数据
/movies/_search?q=title:(beautiful NOT mind)
{
    "profile":true
}

//范围查找 区间查找 TODO 

//通配符查询和模糊匹配 TOTO
request body search 基于 json格式的更加完备的 查询方式

此处为重点学习内容,不少东西都只能在request body中使用

curl -X GET "http://xx/kibana_sample_data_ecommerce/_search" -H 'Content-Type:application/json' -d '{"query":{"match_all":{}}}'

prifle 表示的是 展现查询 如何执行
分页 from size from 表示从第几个开始,size表示返回多少条数据
排序 sort
显示字段

// _source 表示显示的字段只能为数组的方式
// sort 表示排序,还能够写成 "sort":{"year":"desc"} 或 "sort":[{"year":{"order":"desc"}}]  
// _source 表示的显示字段

GET /movies/_search
{
    "profile":true,
    "from": 0,
    "size": 10,
    "sort": [
        {"year":"desc"}
    ],
    "_source":["year", "title"]
}

//表示的是 title中包含 beautiful或者 title中包含 mind的数据 至关于 /movies/_search?q=title:(beautiful mind)
GET /movies/_search
{
    "profile": "true",
    "from": 0,
    "size": 5,
    "query": {
        "match":{
            "title": "beautiful mind"
        }
    }
}


//表示字段 titile中既要包含 beautiful 又要包含 mind 不分先后 至关于 /movies/_search?q=title:(beautiful AND mind)

GET /movies/_search
{
    "profile": "true",
    "from": 0,
    "size": 5,
    "query": {
        "match":{
            "title":{
                "query": "beautiful mind",
                "operator": "AND"
            }
        }
    }
}

//表示字段 title中 必须的包含 beautiful mind 按照顺序的方式返回,至关于 /movies/_search?q=title:"beautiful mind"
GET /movies/_search
{
    "profile":"true",
    "from": 0,
    "size": 10,
    "query": {
        "match_phrase":{
            "title":{
                "query": "beautiful mind"
            }
        }
    }
}

// 表示 beautiful 和 mind 中间能够冗余 3个单词
GET /movies/_search
{
    "profile":"true",
    "from": 0,
    "size": 10,
    "query": {
        "match_phrase":{
            "title":{
                "query": "beautiful mind",
                "slop": 3
            }
        }
    }
}
指定索引查询
/_search  全部索引上查询
/index1/_search 在索引 index1 上查询
/index1,index2/_search 在索引 index1 index2 上查询
/index*/_search 以index开头的索引上查询
queryString
//查询 字段为 title 中包含 beautiful 或者 mind的数据
/movies/_search
{
    "profile": true,
    "query":{
        "query_string":{
            "query":"beautiful mind",
            "default_field": "title"
        }
    }
}

//查询title中必须存在beautiful和mind的数据,在query中使用的 AND 表示的是 而且的意思
/movies/_search
{
    "profile": true,
    "query":{
        "query_string":{
            "query":"beautiful AND mind",
            "default_field": "title"
        }
    }
}

//同上
/movies/_search
{
    "profile": true,
    "query":{
        "query_string":{
            "query":"beautiful mind",
            "default_field": "title",
            "default_operator": "and"
        }
    }
}

//使用 simple_query_string 执行查询操做
/movies/_search
{
    "profile": "true",
    "query":{
        "simple_query_string":{
            "query": "beaultiful mind",
            "default_operator": "and",
            "fields": ["title"]
        }
    }
}

//同上,simple_string_query 使用的是 + - | 表示 and not or, 只有 fields
/movies/_search
{
    "profile": "true",
    "query":{
        "simple_query_string":{
            "query": "beaultiful + mind",
            "fields": ["title"]
        }
    }
}

simple_string_query 和 string_query 对比
一、string_query 可使用 default_filed和fields 可是 simple_string_query 只可用 fields
二、string_query 在 query 字段中能够 使用 AND OR NOT 表示 “而且、 或者、不” 得关系,可是必须的为大写,simple_string_query 必须 使用 "+ | -" 表示 而且 或者 不 的关系;二者均可以使用 default_operator 达到一样的效果

_mapping的定义 以及 使用

dynamic _mapping 默认动态设置 数据类型;
dynamic _mapping 设置的值分别为 true false strict 默认为true
true 表示 新增字段会自动索引;false表示 新增字段不会索引,strict表示 不能新增数据;
查看mapping信息 GET /index/_mapping

PUT /index/_mapping
{
    "dynamic": true
}
聚合 aggs

分组统计

GET /kibana_sample_data_flights/_search
{
    //显示详细的表示查询字段过程
    "profile": true ,
    //只是显示0条结果,这里主要看统计信息,因此不看数据
    "size": 0,
    //统计信息
    "aggs":{
        //表示统计结果呈现字段
        "flight_dest":{
            //分组统计,相似group by
            "terms":{
                //分组字段
                "field": "OriginCountry",
                //统计结果只是显示一条数据
                "size": 10
            }
        },
        //获取总数据的平均值
        "avg_price":{
            "avg":{
                "field":"AvgTicketPrice"
            }
        },
        //获取总数据的最大值和最小值
        "max_price":{
            "max":{
                "field": "AvgTicketPrice"
            }
        },
        "min_price":{
            "min":{
                "field":"AvgTicketPrice"
            }
        }
        
        //分组中添加二次分组
        "weather":{
            //按照天气字段分组
            "terms":{
                "field": "DestWeather"
            },
            //在天气分组中,再按照OriginCountry进行分组
            "aggs":{
                "origin_source":{
                    "terms":{
                        "field": "OriginCountry"
                    }
                }
            }
        }
    }
}
相关文章
相关标签/搜索