下载Elasticsearch:
https://www.elastic.co/cn/downloads/elasticsearch
下载kibana可视化插件:
https://www.elastic.co/cn/downloads/kibana
Elasticsearch配置文件:
安装目录—config目录下—elasticsearch.yml:
cluster.name: Ealdan #配置elasticsearch的集群名称,默认是elasticsearch。建议修改成一个有意义的名称。node.name: Ealdan_node_1 #节点名,通常一台物理服务器就是一个节点,es会默认随机指定一个名字,建议指定一个有意义的名称,方便管理network.host: 127.0.0.1 #绑定ip地址http.port: 9200 #暴露的http端口transport.tcp.port: 9300 #内部端口transport.host: localhostnode.master: true #主节点node.data: true #数据节点node.ingest: true#discovery.zen.ping.unicast.hosts: ["0.0.0.0:9300", "0.0.0.0:9301", "0.0.0.0:9302"] #设置集群中master节点的初始列表#discovery.zen.minimum_master_nodes: 1 #主结点数量的最少值 ,此值的公式为:(master_eligible_nodes / 2) + 1 ,比如:有3个符合要求的主结点,那么这里要设置为2。bootstrap.memory_lock: false #内存的锁定只给es用node.max_local_storage_nodes: 1 #单机允许的最大存储结点数,通常单机启动一个结点建议设置为1,开发环境如果单机启动多个节点可设置大于1path.data: D:\编程软件\elasticsearch-7.8.0\data #索引目录path.logs: D:\编程软件\elasticsearch-7.8.0\logs #日志#http.cors.enabled: true # 跨域设置#http.cors.allow‐origin: /.*/
kibana配置文件:
安装目录—config目录下—elasticsearch.yml:
server.port: 5601server.host: "0.0.0.0"elasticsearch.hosts: ["http://localhost:9200"]elasticsearch.requestTimeout: 90000
启动Elasticsearch:
在安装目录的bin目录下,启动elasticsearch.bat文件
浏览器输入:http://localhost:9200/
查看集群:http://localhost:9200/_cat/nodes?pretty
启动kibana:
在安装目录的bin目录下,启动kibana.bat文件
浏览器输入:http://localhost:5601/app/kibana#
7.0版本之后put型式添加文档,必须要指定id
GET _search{"query": {"match_all": {}}}#查看所有节点信息GET _cat/nodes?v&s#查看所有索引状态GET _cat/indicesGET _cat/indices?v&s#查看所以年相关信息GET users#查看索引的前十条文档GET movies/_search#查看状态为绿的索引GET _cat/indices?v&health=green#按照文档个数排序GET _cat/indices?v&s=docs.count:desc#查看具体的字段GET _cat/indices?pri&v&h=health,index,pri,rep,docs,count,mtGET _cat/nodes#添加索引库PUT blog{"mappings": {"properties": {"title": {"type": "text","index": true,"store": true,"analyzer": "standard"},"comment":{"type": "keyword","store": true,"index": false}}}}GET blogPOST blog/_doc{"name": "text"}GET blog/_searchDELETE blogPUT /blog{"settings": {"number_of_shards": 3,"number_of_replicas": 1},"mappings": {"properties": {"title":{"type": "text","index": true,"store": true,"analyzer": "standard"},"comment":{"type":"keyword","store": true,"index": true}}}}#查询索引库GET blog/_search#添加一条内容,put必须指定idPUT blog/_doc/1{"title":"Ealdan","comment":"elastic kibana"}#添加一条内容POST blog/_doc/2{"title":"测试实例","comment":"搜索引擎","name":"Ealdan"}#删除一条内容DELETE blog/_doc/2#查询索引库中包含该字段的数据GET blog/_search?q=name:"Ealdan"#分词器测试POST _analyze{"analyzer": "standard","text":"Ealdan Test"}POST movies/_analyze{"field": "title",}#查询包含的数据GET /_search{"query": {"match": {"name": "Ealdan"}}}#分词器POST _analyze{"analyzer":"ik_smart","text":"大吉大利"}POST _analyze{"analyzer":"ik_max_word","text":"大吉大利"}
# 创建索引PUT test{"settings": {"index":{"number_of_shards":5,"number_of_replicas":1}}}# 使用默认配置创建索引pretty只是为了如果json返回打印更 加的漂亮一点PUT test2?pretty# 更改索引值PUT test/_settings{"number_of_replicas":0}# 查看指定的索引配置信息GET test/_settings# 查看所有的索引配置信息GET _all/_settings# 查看所有的索引的健康信息GET /_cat/indices?v# 向索引库中添加文档PUT /test/zjj/1{"name":"xiaou","age":18,"desc":"我是xiaou"}# 通过id获得信息GET /test/zjj/1# GET /test/zjj出现问题# 对查询数据筛选GET /test/zjj/1?_source=age,desc# 修改文档# 1覆盖idPUT /test/zjj/1{"age":"20"}# 2修改POST /test/zjj/1{"age":18}# 更新POST /test/zjj/1/_update?pretty{"script" : "ctx._source.age += 5"}# 删除DELETE test/zjj/2# 删除索引库DELETE testPUT xiaou{"settings":{"index":{"number_of_shards": 3,"number_of_replicas": 0}}}POST /xiaou/test/_bulk{"index":{"_id":1}}{"title":"Java","price":55}{"index":{"_id":2}}{"title":"php","price":60}{"index":{"_id":3}}{"title":"ww","price":75}GET xiaou/test/_mget{"ids":["1","2","3","4"]}POST /xiaou/test/_bulk{"delete":{"_id":1}}{"create":{"_id":4}}{"title":"javascript","price":75}{"index":{"_index":"test3","_type":"t","_id":1}}{"name":"xiaou"}{"update":{"_index":"xiaou","_type":"test","_id":3}}{"doc":{"price":58}}POST /xiaou/test/_bulk{"index":{"_id":1}}{"title":"i love java"}GET /xiaou/_mappingGET xiaou/test/_search?q=javaPUT test4{"settings": {"index":{"number_of_shards": 3,"number_of_replicas": 0}},"mappings": {"books":{"properties": {"title":{"type":"text"},"name":{"type":"text","index":false},"price":{"type":"double"},"number":{"type":"object","dynamic":true}}}}}GET test4/_mappingPUT test4/books/1{"name":"xiaou","number":{"1":1,"2":3},"price":56.55,"title":"this java"}GET /test4/books/1# 数据准备PUT /lib3{"settings":{"number_of_shards" : 3,"number_of_replicas" : 0},"mappings":{"user":{"properties":{"name": {"type":"text","analyzer": "ik_max_word"},"address": {"type":"text","analyzer": "ik_max_word"},"age": {"type":"integer"},"desc": {"type":"text","analyzer": "ik_max_word"},"birthday": {"type":"date"}}}}}PUT /lib3/user/1{"name":"小美","address":"浙江桐乡","age":18,"desc":"喜欢唱歌,吃饭,睡觉","birthday":"1999-03-05"}PUT /lib3/user/2{"name":"小u","address":"浙江杭州","age": 25,"desc":"喜欢唱歌,睡觉","birthday":"1999-03-05"}PUT /lib3/user/3{"name":"小啦","address":"浙江下沙","age":16,"desc":"喜欢玩游戏,睡觉","birthday":"1999-03-05"}PUT /lib3/user/4{"name":"小打","address":"浙江呜呜","age":12,"desc":"喜欢玩游戏","birthday":"2000-03-05"}# term查询GET /lib3/user/_search{"query": {"term": {"name":"小美"}}}# terms查询GET /lib3/user/_search{"query": {"terms": {"desc":["吃饭","游戏"]}}}# 分页GET /lib3/user/_search{"from": 0,"size": 2,"query": {"terms": {"desc":["吃饭","游戏"]}}}# 返回版本号GET /lib3/user/_search{"from": 0,"size": 2,"version": true,"query": {"terms": {"desc":["吃饭","游戏"]}}}# match查询GET /lib3/user/_search{"query": {"match": {"address": "拉你桐乡"}}}# 查询所有文档GET /lib3/user/_search{"query": {"match_all": {}}}# 指定多个字段GET lib3/user/_search{"query": {"multi_match": {"query": "游戏桐乡","fields": ["address","desc"]}}}# 短语匹配查询GET lib3/user/_search{"query": {"match_phrase": {"desc": "玩游戏"}}}# 指定返回的字段GET lib3/user/_search{"_source": ["address","desc"],"query": {"match_phrase": {"desc": "玩游戏"}}}# 控制加载的字段GET /lib3/user/_search{"query": {"match_all": {}},"_source": {"includes": ["name"],"excludes": ["age"]}}# 通配符GET /lib3/user/_search{"query": {"match_all": {}},"_source": {"includes": ["n*"],"excludes": ["a?e"]}}# sortGET /lib3/user/_search{"query": {"match_all": {}},"sort": [{"age": {"order": "asc"}}]}# 前缀匹配查询GET /lib3/user/_search{"query": {"match_phrase_prefix": {"name": "小"}}}# 范围查询GET /lib3/user/_search{"query": {"range": {"birthday": {"gte": "1999-01-01","lte": "2000-01-01"}}}}# wildcard查询GET /lib3/user/_search{"query": {"wildcard": {"name": {"value": "*u"}}}}# fuzzy实现模糊查询GET /lib3/user/_search{"query": {"fuzzy": {"name":"u"}}}# 高亮GET /lib3/user/_search{"query": {"match": {"desc": "唱歌"}},"highlight": {"fields": {"desc": {}}}}POST /lib4/items/_bulk{"index": {"_id": 1}}{"price": 40,"itemID": "ID100123"}{"index": {"_id": 2}}{"price": 50,"itemID": "ID100124"}{"index": {"_id": 3}}{"price": 25,"itemID": "ID100124"}{"index": {"_id": 4}}{"price": 30,"itemID": "ID100125"}{"index": {"_id": 5}}{"price": null,"itemID": "ID100127"}GET lib4/items/_search{"post_filter": {"term": {"price": "40"}}}GET lib4/items/_search{"post_filter": {"terms": {"price":[25,40]}}}GET lib4/items/_search{"post_filter": {"term": {"itemID": "id100123"}}}# 组合过滤查询GET lib4/items/_search{"post_filter": {"bool": {"should":[{"term":{"price":25}},{"term":{"itemID":"id100123"}}],"must_not":{"term":{"price":30}}}}}GET lib4/items/_search{"post_filter": {"bool": {"should":[{"term":{"price":25}}],"must_not":{"term":{"price":30}}}}}# 嵌套使用boolGET lib4/items/_search{"post_filter": {"bool": {"should":[{"term":{"itemID":"id100123"}},{"bool":{"must":[{"term":{"itemID":"id100124"}},{"term":{"price":40}}]}}]}}}# 范围过滤GET lib4/items/_search{"post_filter": {"range": {"price": {"gte": 40,"lt": 50}}}}# 过滤非空GET lib4/items/_search{"query": {"bool": {"filter": {"exists": {"field": "price"}}}}}# sumGET lib4/items/_search{"size": 0,"aggs": {"price_sum": {"sum": {"field": "price"}}}}# avgGET lib4/items/_search{"size": 0,"aggs": {"price_avg": {"avg": {"field": "price"}}}}#maxGET lib4/items/_search{"size": 0,"aggs": {"price_max": {"max": {"field": "price"}}}}#minGET lib4/items/_search{"size": 0,"aggs": {"price_min": {"min": {"field": "price"}}}}# cardinality:求基数GET lib4/items/_search{"size": 0,"aggs": {"price_card": {"cardinality": {"field": "price"}}}}# 分组GET lib4/items/_search{"size": 0,"aggs":{"price_group":{"terms": {"field": "price"}}}}#GET lib3/user/_search{"query": {"match": {"desc": "唱歌"}},"size": 0, "aggs": {"avg_group_by": {"terms": {"field": "age","order": {"avg_of_age": "desc"}},"aggs": {"avg_of_age": {"avg": {"field": "age"}}}}}}# 复合查询GET lib3/user/_search{"query": {"bool": {"must": [{"match": {"desc": "唱歌"}}],"must_not": [{"match": {"desc": "吃饭"}}],"should": [{"match": {"address": "杭州"}},{"range": {"birthday": {"gte": "1999-03-05","lte": "2000-03-05"}}}]}}}GET lib3/user/_search{"query": {"bool": {"must": [{"match": {"desc": "唱歌"}}],"filter": {"bool": {"must":{"range": {"birthday": {"gte": "1999-03-05","lt": "2000-03-05"}}}}}}}}
