# 分析文档
POST _analyze
{
"analyzer": "ik_max_word",
"text": "我是中国人"
}
# 创建索引库
# 简化前
{
"settings": {
"属性名": "属性值"
}
}
# 简化后 创建索引库
PUT aaa
# 查看索引库 elasticsearch中查看已添加表aaa
GET aaa
# 删除索引库
DELETE aaa
# 创建字段(表)
PUT aaa
# 创建映射(aaa中添加_doc类型,在类型中设置3个字段)
PUT aaa/_mapping/_doc
{
"properties": {
"title":{
"type": "text",
"analyzer": "ik_max_word"
},
"images":{
"type": "keyword",
"index": false
},
"price":{
"type": "float"
}
}
}
# 查看映射关系
# 查看某个索引库中的所有类型映射
GET aaa/_mapping
# 查看具体映射类型
GET aaa/_mapping/_doc
# 在创建索引库的同时,直接制定索引库中的类型
PUT aaa1
{
"mappings": {
"_doc": {
"properties": {
"title": {
"type": "text",
"analyzer": "ik_max_word"
}
}
}
}
}
# 新增文档(相对于mysql中的行)
# 可以在elasticsearch-head中查看到数据
POST aaa/_doc
{
"title": "小米手机",
"images": "http://image.leyou.com/12479122.jpg",
"price": 2699
}
# 查看文档
GET aaa/_doc/Bf_8hHoBCj8rZN7DH8u8
# 新增文档并自定义id(设置id为1)
POST aaa/_doc/1
{
"title": "华为手机",
"images": "http://image.leyou.com/12479122.jpg",
"price": 4699
}
# id存在就使用Json内容修改文档,如果不存在则新增文档
PUT aaa/_doc/1
{
"title": "华为手机",
"images": "http://image.leyou.com/12479122.jpg",
"price": 4799
}
#删除数据(文档)
DELETE aaa/_doc/2
# 把所有未映射的string类型数据自动映射为keyword类型
PUT aaa2
{
"mappings": {
"_doc": {
"properties": {
"title": {
"type": "text",
"analyzer": "ik_max_word"
}
},
"dynamic_templates": [
{
"my_strings": {
"match_mapping_type": "string",
"mapping": {
"type": "keyword"
}
}
}
]
}
}
}
# 未知的string类型数据就不会被映射为text和keyword并存
# 而是统一以keyword来处理,测试:(新增并查看)
POST aaa2/_doc/3
{
"title": "超大米手机",
"images": "http://image.leyou.com/12479122.jpg",
"price": 3299,
"stock": 200,
"saleable": true,
"subTitle": "哈哈"
}
# 消息被映射成了keyword,而非之前的text和keyword并存
# 说明我们的动态模板生效了
GET aaa2/_mapping
# 查询
# 准备数据
DELETE aaa
PUT aaa
{
"mappings": {
"_doc": {
"properties": {
"title": {
"type": "text",
"analyzer": "ik_max_word"
},
"images": {
"type": "keyword",
"index": false
},
"price": {
"type": "float"
}
},
"dynamic_templates": [
{
"my_strings": {
"match_mapping_type": "string",
"mapping": {
"type": "keyword"
}
}
}
]
}
}
}
# 批量操作
POST _bulk
{"index":{"_index":"aaa","_type":"_doc"}}
{"title":"小米手机","images":"http://image.leyou.com/12479122.jpg","price":2699}
{"index":{"_index":"aaa","_type":"_doc"}}
{"title":"华为手机","images":"http://image.leyou.com/12479122.jpg","price":4699}
{"index":{"_index":"aaa","_type":"_doc"}}
{"title":"苹果手机","images":"http://image.leyou.com/12479122.jpg","price":10999}
{"index":{"_index":"aaa","_type":"_doc"}}
{"title":"超大米手机","images":"http://image.leyou.com/12479122.jpg","price":3299,"stock":200,"saleable":true,"subTitle":"哈哈"}
{"index":{"_index":"aaa","_type":"_doc"}}
{"title":"小米电视4A","images":"http://image.leyou.com/12479122.jpg","price":3899}
# 基本查询
# 查询所有(query :代表查询对象 match_all :代表查询所有 )
GET aaa/_search
{
"query":{
"match_all": {}
}
}
# 匹配查询,先分词再查询,多个词条之间默认or关系
GET aaa/_doc/_search
{
"query":{
"match":{
"title": "小米电视"
}
}
}
# 手动设置查询的多词条为and关系
GET aaa/_doc/_search
{
"query":{
"match": {
"title": {
"query": "小米电视",
"operator": "and"
}
}
}
}
# math查询titl字段会进行分词,然后通过分词查询
GET aaa/_search
{
"query":{
"match": {
"title": "小米手机"
}
}
}
# 词条匹配(term)精确值查询
# 如果字段是字符串类型,一般是在keyword上进行查询
GET aaa/_doc/_search
{
"query": {
"term": {
"price": 3899
}
}
}
# 如果term查询字段string类型,索引库存text型会查询不到
GET aaa/goods/_search
{
"query": {
"term": {
"tite": "小米电视4A"
}
}
}
# 解决办法 : 使用math查询,可以查到数据
GET aaa/_doc/_search
{
"query": {
"match": {
"query": "小米电视4A",
"operator": "and"
}
}
}
# 新增索引
PUT aaa3
{
"mappings": {
"_doc": {
"properties":{
"title":{
"type": "text",
"analyzer": "ik_max_word"
}
},
"dynamic_templates": [
{
"strings":{
"match_mapping_type":"string",
"mapping": {
"type": "keyword"
}
}
}]
}
}
}
# 添加文档
POST theima3/_doc
{
"title": "小米手机",
"images": "http://image.leyou.com/12479122.jpg",
"price": 2699
}
# 布尔组合查询must(与)must_not(非)should(或)组合查询
GET aaa/_doc/_search
{
"query": {
"bool": {
"must": {
"match": {
"title": "小米"
}
},
"must_not": {
"match": {
"title": "电视"
}
}
}
}
}
# 范围查询(range)
GET aaa/_doc/_search
{
"query": {
"range": {
"price": {
"gte": 1000,
"lt": 2800
}
}
}
}
# 模糊查询(fuzzy),是term查询的模糊等价
GET aaa/_doc/_search
{
"query": {
"fuzzy": {
"title": "电视3"
}
}
}
# 结果过滤(只获取部分字段时添加_source)
GET aaa/_doc/_search
{
"_source": [
"title",
"price"
],
"query": {
"term": {
"price": 2699
}
}
}
# includes(指定显示的字段)excludes(指定不显示的字段)
GET aaa/_doc/_search
{
"_source": {
"includes": [
"title",
"price"
]
},
"query": {
"term": {
"price": 2699
}
}
}
GET aaa/_doc/_search
{
"_source": {
"excludes": ["images"]
},
"query": {
"term": {
"price": 2699
}
}
}
# 过滤器(filter)在条件查询中进行过滤
GET aaa/_doc/_search
{
"query": {
"bool": {
"must": {
"match": {
"title": "小米"
}
}
}
}
}
# 条件查询
GET aaa/_doc/_search
{
"query": {
"bool": {
"must":[
{
"match": {
"title": "小米"
}
},
{
"range": {
"price": {
"gte": 2000,
"lte": 3800
}
}
}]
}
}
}
# filter过滤和条件区别filter不影响其他数据
GET aaa/_doc/_search
{
"query": {
"bool": {
"must": {
"match": {
"title": "小米"
}
},
"filter": {
"range": {
"price": {
"gt": 2000,
"lt": 3800
}
}
}
}
}
}
# 无条件查询constant_score替代filter更简洁
GET aaa/_doc/_search
{
"query": {
"constant_score": {
"filter": {
"range": {
"price": {
"gt": 2000,
"lt": 4000
}
}
}
}
}
}
# 排序
# 单字段排序sort指定字段,order指定排序方式顺倒序
GET aaa/_doc/_search
{
"query": {
"match": {
"title": "手机"
}
},
"sort": [
{
"price": {
"order": "desc"
}
}]
}
# 准备数据,添加文档
POST aaa/_doc
{
"title": "坚果手机",
"images": "http://image.leyou.com/124799122.jpg",
"price": 2699
}
# 多字段排序,受限按照字段1排序再按照字段2排序
GET aaa/_doc/_search
{
"query": {
"match": {
"title": "手机"
}
},
"sort": [
{
"_score":{
"order": "desc"
}
},
{
"price": {
"order": "desc"
}
},
{
"price": {
"order": "desc"
}
}]
}
# 分页查询from指定开始位置,size指定一页几条
GET aaa/_doc/_search
{
"query": {
"match_all": {}
},
"sort":[
{
"price": {
"order": "asc"
}
}],
"from": 3,
"size": 3
}
# 高亮原理(搜索关键字加上约定好的标签<em>,前端做css)
GET aaa/_doc/_search
{
"query": {
"match": {
"title": "手机"
}
},
"highlight": {
"pre_tags": "<em>",
"post_tags": "</em>",
"fields": {
"title": {}
}
}
}
# 聚合查询
# 准备条件(创建索引,导入数据)
PUT car
{
"mappings": {
"_doc": {
"properties": {
"clor": {
"type": "keyword"
},
"make": {
"type": "keyword"
}
}
}
}
}
POST car/_doc/_bulk
{"index":{}}
{"price":10000,"color":"红","make":"本田","sold":"2014-10-28"}
{"index":{}}
{"price":20000,"color":"红","make":"本田","sold":"2014-11-05"}
{"index":{}}
{"price":30000,"color":"绿","make":"福特","sold":"2014-05-18"}
{"index":{}}
{"price":15000,"color":"蓝","make":"丰田","sold":"2014-07-02"}
{"index":{}}
{"price":12000,"color":"绿","make":"丰田","sold":"2014-08-19"}
{"index":{}}
{"price":20000,"color":"红","make":"本田","sold":"2014-11-05"}
{"index":{}}
{"price":80000,"color":"红","make":"宝马","sold":"2014-01-01"}
{"index":{}}
{"price":25000,"color":"蓝","make":"福特","sold":"2014-02-12"}
# 聚合查询 (聚合为桶)
GET /car/_search
{
"size": 0,
"aggs": {
"popular_colors": {
"terms": {
"field": "color"
}
}
}
}
DELETE car
# 桶内度量(分组之后对组中的数据进行聚合运算例如:最值,求和...)
GET /car/_search
{
"size": 0,
"aggs": {
"popular_colors": {
"terms": {
"field": "color"
},
"aggs": {
"avg_price": {
"avg": {
"field": "price"
}
}
}
}
}
}