elasticsearch使用教程

倪炎彬
2023-12-01

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)


 类似资料: