1.ElasticSearch安装
- elasticsearch下载安装
- elasticsearch-head插件
https://github.com/mobz/elasticsearch-head
git clone git://github.com/mobz/elasticsearch-head.git
cd elasticsearch-head
npm install
npm run start
open
http://localhost:9100/
- elasticsearch改配置,使elasticsearch-head插件能跨域访问
config elasticsearch.yml
bootstrap.memory_lock: false
cluster.name: my-elasticsearch-application
http.port: 9200
network.host: 127.0.0.1
node.data: true
node.ingest: true
node.master: true
node.max_local_storage_nodes: 1
node.name: ElasticSearch Master
path.data: D:\Program Files\Elastic\Elasticsearch\data
path.logs: D:\Program Files\Elastic\Elasticsearch\logs
transport.tcp.port: 9300
xpack.license.self_generated.type: basic
xpack.security.enabled: true
http.cors.enabled: true
http.cors.allow-origin: "*"
2.ElasticSearch添加索引
- head插件
{ "novel": { "properties": { "title": { "type": "text" } } } }
- postman
{ "settings": { "number_of_shards": 3, "number_of_replicas": 1 }, "mappings": { "man": { "properties": { "name": { "type": "text" }, "country": { "type": "keyword" }, "age": { "type": "integer" }, "date": { "type": "date", "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis" } } }, "woman": { } } }
elasticsearch-6.7.2版本mappings下不支持[woman, man]两个索引,把woman那个删除。
3.ElasticSearch的增删改查
-
增
- 指定id为1的增
127.0.0.1:9200/people/man/1 put
- 不指定id
127.0.0.1:9200/people/man post
- 指定id为1的增
-
删
127.0.0.1:9200/people/man/YzAIE24B3Dw_V0_L1AaA delete
-
改
- 直接在之前数据上更改
- 用_update 127.0.0.1:9200/people/man/1/_update post
- 使用script
- 直接在之前数据上更改
-
查
插入数据源 import requests import random import json def main(): language = ['C', 'Java', 'Python', 'ElasticSearch'] author = ['张三', '李四', '王五', '赵六'] level = ['入门', '熟悉', '精通'] id = 0 url = 'http://127.0.0.1:9200/book/novel/' headers = {'Content-Type': 'application/json;charset=UTF-8'} for i in language: for j in level: id += 1 data = { 'author': author[language.index(i)], 'title': i + j, 'word_count': random.randint(50, 150) * 100, 'publish_date': '2019-' + ('0' if id < 10 else '') + str(id) + '-01' } print(data) r = requests.post(url + str(id), json.dumps(data), headers=headers) print(r.content) if __name__ == '__main__': main()
-
GET 127.0.0.1:9200/book/novel/1
-
POST 127.0.0.1:9200/book/_search 查询所有
查询全部数据 { "query": { "match_all": {} } } 查询一条数据 { "query": { "match_all": {} }, "from": 1, "size": 1 } 关键词查询 { "query": { "match": { "title": "ElasticSearch" } } } 关键词查询按日期降序 { "query": { "match": { "title": "ElasticSearch" } }, "sort": [ { "publish_date": { "order": "desc" } } ] } 聚合查询 { "aggs": { "group_by_word_count": { "terms": { "field": "word_count" } }, "group_by_publish_date": { "terms": { "field": "publish_date" } } } } 统计 { "aggs": { "grades_word_count": { "stats": { "field": "word_count" } } } } 最小值 { "aggs": { "grades_word_count": { "min": { "field": "word_count" } } } }
-
4.ElasticSearch高级查询
-
子条件查询 特定字段查询所指特定值
-
Query context
模糊匹配 { "query": { "match": { "title": "ElasticSearch入门" } } } 精准匹配 { "query": { "match_phrase": { "title": "ElasticSearch入门" } } } 多字段匹配 { "query": { "multi_match": { "query": "ElasticSearch", "fields": ["author", "title"] } } } 语法查询 { "query": { "query_string": { "query": "(ElasticSearch AND 入门) OR Python" } } } { "query": { "query_string": { "query": "ElasticSearch OR 张三", "fields": ["title", "author"] } } } 字段查询 { "query": { "term": { "word_count": 5700 } } } { "query": { "term": { "author": "张三" } } } 字数范围 5000 <= word_count <= 8000 { "query": { "range": { "word_count": { "gte": 5000, "lte": 8000 } } } } 字数范围 5000 < word_count <= 8000 { "query": { "range": { "word_count": { "gt": 5000, "lte": 8000 } } } } 时间范围 2019-01-01 <= publish_date <= 2019-08-01 { "query": { "range": { "publish_date": { "gte": "2019-01-01", "lte": "2019-08-01" } } } } 时间范围 2019-01-01 <= publish_date <= now { "query": { "range": { "publish_date": { "gte": "2019-01-01", "lte": "now" } } } }
-
Filter context
{ "query": { "bool": { "filter": { "term": { "word_count": 5700 } } } } }
-
-
复合条件查询 以一定的逻辑组合子条件查询
- 固定分数查询
{ "query": { "constant_score": { "filter": { "match": { "title": "ElasticSearch" } } } } } { "query": { "constant_score": { "filter": { "match": { "title": "ElasticSearch" } }, "boost": 2 } } }
- 布尔查询
{ "query": { "bool": { "should": [ { "match": { "title": "ElasticSearch" } }, { "match": { "author": "张三" } } ] } } } { "query": { "bool": { "must": [ { "match": { "title": "ElasticSearch" } }, { "match": { "author": "张三" } } ] } } } { "query": { "bool": { "must": [ { "match": { "title": "ElasticSearch" } }, { "match": { "author": "赵六" } } ], "filter": [ { "term": { "word_count": 6000 } } ] } } } { "query": { "bool": { "must_not": { "term": { "author": "张三" } } } } }
- ...more
- 固定分数查询