elasticsearch使用教程

1.es与mysql对比

mysqlelasticserch
数据库(database)索引(indices)
表(tables)types
行(rows)documents
字段(columns)fields

2.docker安装

docker run -d --name elasticsearch  -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" elasticsearch:7.1.0
# 修改配置解决跨域问题
docker exec -it elasticsearch /bin/bash
cd /usr/share/elasticsearch/config/
vi elasticsearch.yml

在elasticsearch.yml的文件末尾加上:
http.cors.enabled: true
http.cors.allow-origin: "*"

重启
docker restart elasticsearch

3.安装ik分词器

# elasticsearch的版本和ik分词器的版本需要保持一致
docker exec -it elasticsearch /bin/bash

cd /usr/share/elasticsearch/plugins/
elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.1.0/elasticsearch-analysis-ik-7.1.0.zip
exit

docker restart elasticsearch   

4.elasticsearch-head安装

docker pull docker.io/mobz/elasticsearch-head:5

# 启动elasticsearch-head
docker run -d \
  --name=elasticsearch-head \
  --restart=always \
  -p 9100:9100 \
  docker.io/mobz/elasticsearch-head:5

# 解决elasticsearch-head 刷不出数据的问题
# 1、进入 es-head 安装目录;
# 2、cd _site/
# 3、编辑 vendor.js 共有两处
# 将 6886行 contentType: "application/x-www-form-urlencoded" 修改
# 为contentType: "application/json;charset=UTF-8"
# 然后再将 7574行 var inspectData = s.contentType ==="application/x-www-form-urlencoded" && 
# 修改为 var inspectData = s.contentType === "application/json;charset=UTF-8" &&
# 重启elasticsearch-head

5.python安装

pip install elasticsearch  # 基本的elasticsearch操作
pip install elasticsearch-dsl  # 对elasticserach.py的封装的高级、简便操作

6.创建/更新一条记录并自动创建索引

from elasticsearch import Elasticsearch

es = Elasticsearch(['http://192.168.241.120/'], port=9200)  # 连接elasticsearch

doc = {
    'name': '张三',
    'uid': 1,
    'create_time': datetime.now(),
    'book_name': 'python',
    'desc': '从入门到放弃',
}
# 创建一条记录, 指定索引为 book_info ,指定记录id为1, 如果该记录已存在会更新该记录
res = es.index(index="book_info", id=1, body=doc)
print(res)
"""
{
    '_index': 'book_info',
    '_type': '_doc',
    '_id': '1',
    '_version': 1,
    'result': 'created',
    '_shards': {
        'total': 2,
        'successful': 1,
        'failed': 0
    },
    '_seq_no': 0,
    '_primary_term': 1
}
"""

es自动创建的索引信息
"""
{
    "state": "open",
    "settings": {
        "index": {
            "creation_date": "1607261557456",
            "number_of_shards": "1",
            "number_of_replicas": "1",
            "uuid": "Zms7Mhv_RO28ww5IPTbnEQ",
            "version": {
                "created": "7010099"
            },
            "provided_name": "book_info"
        }
    },
    "mappings": {
        "_doc": {
            "properties": {
                "uid": {
                    "type": "long"
                },
                "create_time": {
                    "type": "date"
                },
                "name": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "ignore_above": 256,
                            "type": "keyword"
                        }
                    }
                },
                "book_name": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "ignore_above": 256,
                            "type": "keyword"
                        }
                    }
                },
                "desc": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "ignore_above": 256,
                            "type": "keyword"
                        }
                    }
                }
            }
        }
    },
    "aliases": [],
    "primary_terms": {
        "0": 1
    },
    "in_sync_allocations": {
        "0": [
            "F0QceTYrSeGNq94mt6NjEw"
        ]
    }
}
"""

7.批量创建

from elasticsearch import Elasticsearch
from elasticsearch.helpers import bulk

body = [
    {
        '_index': 'book_info',
        '_id': 2,
        '_source': {
            'name': '李四',
            'uid': 2,
            'create_time': datetime.now(),
            'book_name': 'java',
            'desc': '从入门到精通',
        }
    },
    {
        '_index': 'book_info',
        '_id': 3,
        '_source': {
            'name': '王五',
            'uid': 3,
            'create_time': datetime.now() + timedelta(hours=1),
            'book_name': 'go',
            'desc': 'web高并发开发',
        }
    }
]
res = bulk(es, actions=body)
print(res)  # (2, [])  元组类型 

8.统计有多少条数据

q1 = {
    'query': {
        "match": {
            "book_name": 'python'
        }
    },
}

# 查询book_info中book_name是python 的有多少条记录
res = es.count(index='book_info', body=q1)
print(res)  # {'count': 1, '_shards': {'total': 1, 'successful': 1, 'skipped': 0, 'failed': 0}}

# 总共有多少条记录
res1 = es.count(index='book_info')  
print(res1)  # {'count': 3, '_shards': {'total': 1, 'successful': 1, 'skipped': 0, 'failed': 0}}

9.create创建记录

doc2 = {
    'name': '马保国',
    'uid': 5,
    'create_time': datetime.now(),
    'book_name': 'mysql',
    'desc': '从小白到大神',
}

# 如果创建的id 已存在 会返回409 报错
res = es.create(index='book_info', id=5, body=doc2)
print(res)
"""
{'_index': 'book_info', '_type': '_doc', '_id': '5', '_version': 1, 'result': 'created',
    '_shards': {'total': 2, 'successful': 1, 'failed': 0}, '_seq_no': 9, '_primary_term': 1}
"""

10.删除记录

res = es.delete(index='book_info', id=1)
print(res)
"""
{'_index': 'book_info', '_type': '_doc', '_id': '1', '_version': 7, 'result': 'deleted',
       '_shards': {'total': 2, 'successful': 1, 'failed': 0}, '_seq_no': 10, '_primary_term': 1}

"""

q2 = {
    'query': {
        'match': {
            'uid': 2,
        }
    }
}
# 查询uid=2的然后删除
res = es.delete_by_query(index='book_info', body=q2)
print(res)
"""
{'took': 834, 'timed_out': False, 'total': 1, 'deleted': 1, 'batches': 1, 'version_conflicts': 0, 'noops': 0,
       'retries': {'bulk': 0, 'search': 0}, 'throttled_millis': 0, 'requests_per_second': -1.0,
       'throttled_until_millis': 0, 'failures': []}
"""

11.判断记录是否存在

res = es.exists(index='book_info', id=1)
print(res)  # False

res1 = es.exists(index='book_info', id=3)
print(res1)  # True

12.get获取某条记录

# 获取id=3的记录
res = es.get(index='book_info', id=3)
print(res)
"""
{
    '_index': 'book_info',
    '_type': '_doc',
    '_id': '3',
    '_version': 1,
    '_seq_no': 7,
    '_primary_term': 1,
    'found': True,
    '_source': {
        'name': '王五',
        'uid': 3,
        'create_time': '2020-12-06T22:55:46.265774',
        'book_name': 'go',
        'desc': 'web高并发开发'
    }
}
"""

13.mget获取多条记录

doc3 = {
    'docs': [  # 获取指定记录的数据, 自定义返回数据结构
        {
            '_id': 3,
            '_source': True,  # 返回所有数据
        },
        {
            '_id': 4,
            '_source': ['name', 'book_name']  # 只返回用户名和书名
        },
    ]
}

res = es.mget(index='book_info', body=doc3)
print(res)
"""
{
    'docs': [
        {
            '_index': 'book_info',
            '_type': '_doc',
            '_id': '3',
            '_version': 1,
            '_seq_no': 7,
            '_primary_term': 1,
            'found': True,
            '_source': {
                'name': '王五',
                'uid': 3,
                'create_time': '2020-12-06T22:55:46.265774',
                'book_name': 'go',
                'desc': 'web高并发开发'
            }
        },
        {
            '_index': 'book_info',
            '_type': '_doc',
            '_id': '4',
            '_version': 1,
            '_seq_no': 8,
            '_primary_term': 1,
            'found': True,
            '_source': {
                'name': '老王',
                'book_name': 'javascript'
            }
        }
    ]
}
"""

14.msearch批量查询

q3 = [
    {
        'index': 'book_info'  # 索引
    },
    {
        'query': {  # 查询条件
            'match': {
                'uid': 3,
            }
        }
    },
    {
        'index': 'loginfo'
    },
    {
        'query': {
            'match': {
                'bk_biz_id': 3,
            }
        }
    }
]
# 批量查询
res = es.msearch(body=q3)
print(res)
"""
{
    'took': 2,
    'responses': [
        {
            'took': 0,
            'timed_out': False,
            '_shards': {
                'total': 1,
                'successful': 1,
                'skipped': 0,
                'failed': 0
            },
            'hits': {
                'total': {
                    'value': 1,
                    'relation': 'eq'
                },
                'max_score': 1.0,
                'hits': [
                    {
                        '_index': 'book_info',
                        '_type': '_doc',
                        '_id': '3',
                        '_score': 1.0,
                        '_source': {
                            'name': '王五',
                            'uid': 3,
                            'create_time': '2020-12-06T22:55:46.265774',
                            'book_name': 'go',
                            'desc': 'web高并发开发'
                        }
                    }
                ]
            },
            'status': 200
        },
        {
            'took': 1,
            'timed_out': False,
            '_shards': {
                'total': 2,
                'successful': 2,
                'skipped': 0,
                'failed': 0
            },
            'hits': {
                'total': {
                    'value': 1,
                    'relation': 'eq'
                },
                'max_score': 1.0,
                'hits': [
                    {
                        '_index': 'loginfo',
                        '_type': '_doc',
                        '_id': '3',
                        '_score': 1.0,
                        '_source': {
                            'bk_biz_id': 3,
                            'ip': '192.168.149.132',
                            'log_time': '2020-12-02T23:30:41',
                            'app': 'saaslog',
                            'code': 400,
                            'log_content': 'hello everybody'
                        }
                    }
                ]
            },
            'status': 200
        }
    ]
}
"""

15.查询

q4 = {
    "query": {
        "match_all": {}  # 查询所有
    }
}

q5 = {
    "query": {
        "match": {  # 查询某个字段
            'uid': 3
        }
    }
}

q6 = {
    # 查询 book_name=mysql或者是javascript,uid大于等于3不等于4的记录
    'query': {
        "bool": {
            "must": {"match": {"book_name": "mysql"}},  # must 相当于and
            "must_not": {"match": {"uid": "4"}},  # must_not 相当于not
            "should": {"match": {"book_name": "javascript"}},  # should 相当于or
            "filter": {"range": {"uid": {"gte": 3}}}  # 过滤查询, 不参与平分
        }
    }
}

res = es.search(index='book_info', body=q4)
print(res)


16.原生操作

1.快速创建索引和记录
PUT /customer/_doc/1
{
  "name": "John Doe"
}
# 当索引不存在时,自动创建索引,并且创建唯一的id的一条记录

# 相应结果
{
  "_index" : "customeer",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 0,
  "_primary_term" : 1
}

创建记录
POST /customer/_doc/
{
  "name": "zhangsan"
}


# 根据id获取某个记录详情
GET /customer/_doc/1
{
  "_index" : "customeer",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 3,
  "_seq_no" : 2,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "name" : "yangxin"
  }
}

2.快速搜索
GET /qidian/_search
{
  "query": {"match_all": {}},
  "sort": [
    {
      "id": "desc"  # asc
    }
  ]
}

# 返回结果:
# 默认返回10条记录
{
  "took" : 1, # 查询耗时 1秒
  "timed_out" : false, # 查询是否超时
  "_shards" : {  # 分片信息
    "total" : 1,  # 总共1个分片
    "successful" : 1,  # 成功1个
    "skipped" : 0,  # 跳过0个
    "failed" : 0  # 失败0个
  },
  "hits" : {
    "total" : {
      "value" : 303, # 总共多少条记录
      "relation" : "eq"
    },
    #"max_score" : null, # 最相关记录的分数
    "max_score" : 19.19993,
    "hits" : [
      {
        "_index" : "qidian",
        "_type" : "modelresult",
        "_id" : "903",
        #"_score" : null,
        "_score" : 19.19993,
        "_source" : {
          "id" : 903,
          "django_ct" : "home_application.books",
          "django_id" : "903",
          "text" : """
12
56
78
""",
          "title" : "12",
          "author" : "56",
          "book_type" : "78",
          "sub_type" : "90",
          "update_status" : "55",
          "intro" : "11"
        },
        "sort" : [
          903
        ]
      },
     ]
    }
   }
   

GET /qidian/_search
{
  "query": {"match_all": {}},
  "sort": [
    {
      "id": "desc"  # asc
    }
  ],
  "from": 10, # offset
  "size": 10  # limit
}


GET /qidian/_search
# 包含匹配
{
  "query": { "match": { "author": "卖报小郎君" } }
}

# 词语匹配
GET /qidian/_search
{
  "query": {"match_phrase": {"author": "卖报小郎君"}}
}

# 查询作者等于xxx,title不等于xxx 的记录
GET /qidian/_search
{
  "query": {
    "bool": {
      "must": [ # and
        {
          "match": {"author": "卖报的小郎君"}
        }
      ],
      "must_not": [ # 条件取反
        {
          "match": {"title": "灵镜行者"}
        }
      ]
    }
  }
}

# 查询记录 id在633到640的范围
GET /qidian/_search
{
  "query": {
    "bool": {
      "must": {"match_all": {}},
      "filter": {
        "range": {
          "id": {
            "gte": 633,
            "lte": 640
          }
        }
      }
    }
  }
}

// 对作者聚合分组
GET /qidian/_search
{
  "size": 0,
  "aggs": {
    "group_by_author": {  # 自定义聚合名字
      "terms": {  # 聚合类型  min,max.sum,avg ....
        "field": "author.keyword"
      }
    }
  }
}

3.更新记录
POST /customeer/_update/1
{
  "doc": {
    "name": "wangqi"
  }
}

PUT /customeer/_doc/1
{
  "name": "王六"
}

# 记录不存在就创建
POST /customeer/_update/3
{
  "doc": {
    "name": "wangba"
  },
  "upsert": {
    "name": "wangjiu"
  }
}

# 查询修改
POST customeer/_update_by_query
{
  # 查询
  "query": {
    "term": {
      "name": "wangjiu"
    }
  },
  # 脚本修改操作
  "script": "ctx._source.name = 'wanshi'"
}

4.删除
# 删除id为1的记录
DELETE /customeer/_doc/1

# 返回结果
{
  "_index" : "customeer",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 8,
  "result" : "deleted",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 14,
  "_primary_term" : 3
}

5.批量查询
GET /_mget
{
  "docs": [
    {
      "_index": "customeer",
      "_type": "_doc",
      "_id": "CKamYooBu_jWP4fo7LRC"
    },
    {
      "_index": "customeer",
      "_type": "_doc",
      "_id": "XKatYooBu_jWP4foKbjA"
    }
  ]
}

# 返回结果
{
  "docs" : [
    {
      "_index" : "customeer",
      "_type" : "_doc",
      "_id" : "CKamYooBu_jWP4fo7LRC",
      "_version" : 1,
      "_seq_no" : 3,
      "_primary_term" : 2,
      "found" : true,
      "_source" : {
        "name" : "zhangsan"
      }
    },
    {
      "_index" : "customeer",
      "_type" : "_doc",
      "_id" : "XKatYooBu_jWP4foKbjA",
      "_version" : 1,
      "_seq_no" : 4,
      "_primary_term" : 2,
      "found" : true,
      "_source" : {
        "name" : "lisi"
      }
    }
  ]
}

6.索引复制
POST _reindex
{
  "source": {
    "index": "customeer"
  },
  "dest": {
    "index": "customer"
  }
}

# 返回结果
{
  "took" : 148,
  "timed_out" : false,
  "total" : 5,
  "updated" : 0,
  "created" : 5,
  "deleted" : 0,
  "batches" : 1,
  "version_conflicts" : 0,
  "noops" : 0,
  "retries" : {
    "bulk" : 0,
    "search" : 0
  },
  "throttled_millis" : 0,
  "requests_per_second" : -1.0,
  "throttled_until_millis" : 0,
  "failures" : [ ]
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值