elasticsearch 基本用法。

环境 elasticsearch6.0 kibana-6.0.0

进入
elasticsearch /bin 目录下

下载对应版本的ik分词(我这里是6.0版本)

elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v6.0.0/elasticsearch-analysis-ik-6.0.0.zip

运行
kibana 运行默认端口5601 开始代码把!

1.CURD

#1.新建数据库(索引)
#number_of_shards 分片的数量 默认5(一旦创建完成不能改)
#number_of_replicas 副本的数量 默认1
PUT blog
{
  "settings":{
    "index":{
      "number_of_shards":5,
      "number_of_replicas":1
    }    
  }
}
#2.获取索引消息
GET _all
GET blog
#3.添加表和数据
POST blog/article
{
  "title":"我是博客",
  "msg":{
    "author":"admin",
    "position":"程序员"
  },
  "desc":"i like you",
  "create_time":"2019-07-12",
  "look":50,
  "like":100
}
#4.获取数据(这里先简写后面详细讲解)
GET blog/article/_search
#5.修改数据
PUT blog/article/0PkS5msBS7MmrBYMH0SR
{
  "title":"我是程序员",
  "msg":{
    "author":"admin",
    "position":"程序员"
  },
  "desc":"i like you",
  "create_time":"2019-07-12",
  "look":50,
  "like":100
}
#5.1 修改数据
POST blog/article/0PkS5msBS7MmrBYMH0SR/_update
{
  "doc":{
    "title":"i like work ( fuck you ) "
  }
}
#6.delete (无法删除表,可以删除索引)
DELETE blog/article/0PkS5msBS7MmrBYMH0SR
DELETE blog
#7. 批量操作(同时执行增删改查)
(
格式 添加 {"index":{"_index":"索引名称","_type":"表名称","_id":"id(可不传入)"}}
接着下面填写数据
修改 {"update":{"_id":"id","_type":"表名称","_index":"索引名称"}}
接着下面修改数据
删除 {"delete":{"_id":"id","_type":"表名称","_index":"索引名称"}}
)
POST _bulk
{"index":{"_index":"blog","_type":"article","_id":1}}
{"title":"我是程序员","msg":{"author":"admin","position":"程序员"},"desc":"i like you","create_time":"2019-07-12","like":100}
{"index":{"_index":"blog","_type":"article","_id":2}}
{"title":"我是程序员","msg":{"author":"admin","position":"程序员"},"desc":"i like you","create_time":"2019-07-12","like":100}
{"update":{"_id":2,"_type":"article","_index":"blog"}}
{"doc":{"like":101}}
{"delete":{"_id":1,"_type":"article","_index":"blog"}}
#8.设置插入类型
( text字符串类型 keyword字符串类型(不分词) properties一个对象 date日期 format日期显示格式 integer数字 analyzer选择分词 )
PUT blog
{
  "mappings": {
    "article": {
      "properties": {
        "title":{
          "type": "text",
          "analyzer": "ik_max_word"
        },
        "msg":{
          "properties": {
            "author":{
              "type":"keyword"
            },
            "position":{
              "type":"keyword"
            }
          }
        },
        "desc":{
          "type": "text"
        },
        "create_time":{
          "type": "date",
          "format": "yyyy-MM-dd"
        },
        "like":{
          "type": "integer"
        }
      }
    }
  }
}

执行GET blog 效果如下:
{
  "blog": {
    "aliases": {},
    "mappings": {
      "article": {
        "properties": {
          "create_time": {
            "type": "date",
            "format": "yyyy-MM-dd"
          },
          "desc": {
            "type": "text"
          },
          "like": {
            "type": "integer"
          },
          "msg": {
            "properties": {
              "author": {
                "type": "keyword"
              },
              "position": {
                "type": "keyword"
              }
            }
          },
          "title": {
            "type": "text",
            "analyzer": "ik_max_word"
          }
        }
      }
    },
    "settings": {
      "index": {
        "creation_date": "1562935455291",
        "number_of_shards": "5",
        "number_of_replicas": "1",
        "uuid": "IQ0B-zsNSmeV26mNjb8i_w",
        "version": {
          "created": "6000099"
        },
        "provided_name": "blog"
      }
    }
  }
}
#1.match查询 (根据分词查询对应数据)
GET blog/article/_search
{
  "query":{
    "match":{
      "title":"python"
    }
  }
}
#2.term查询 (不会对搜索解析)
GET blog/article/_search
{
  "query": {
    "term":{
      "title":"python"
    }
  }
}
#3.terms查询 (数组内有一个满足就查询出来)
GET blog/article/_search
{
  "query": {
    "terms": {
      "title": [
        "php",
        "网站"
      ]
    }
  }
}
#4.分页查询
GET blog/article/_search
{
  "query":{
    "match":{
      "title":"python"
    }
  },
  "from": 0,
  "size": 1
}
#5.全部查询
GET blog/article/_search
{
  "query": {
    "match_all": {}
  }
}
#6.多字段查询 multi_match
GET blog/article/_search
{
  "query": {
    "multi_match": {
      "query": "程序员",
      "fields": ["title","msg.position"]
    }
  }  
}
#7.指定返回字段 (要在设置类型时设置 "store": true)
GET blog/article/_search
{
  "stored_fields":["title","desc"],
  "query": {
    "match": {
      "title": "php"
    }
  }
}
#8.sort排序 
GET blog/article/_search
{
  "query": {
    "match_all": {}
  },
  "sort": [
    {
      "like": {
        "order": "desc"
      }
    }
  ]
}
#9. 查询范围  
GET blog/article/_search
{
  "query": {
    "range": {
      "like": {
        "gte": 10,
        "lte": 20
      }
    }
  }
}

组合查询

#1. filter查询
#select * from article where like=50;
GET blog/article/_search
{
  "query": {
    "bool": {
      "must": {"match_all": {}},
      "filter": {
        "term": {
          "like": "50"
        }
      }
    }
  }
}
#should 或者 must必须 must_not取反 exists是否存在
GET blog/article/_search
{
  "query": {
    "bool": {
      "should": [
        {"term":{"title":"python"}}
      ],
      "must": [
        {"term":{"like":50}}
      ]
    }
  }
}