elasticsearch入门及安装

参考教程

elasticsearch入门教程:https://www.yiibai.com/elasticsearch/elasticsearch-getting-start.html

Java JDK安装和配置:https://www.yiibai.com/java/java_environment_setup.html

 

网上的elasticsearch和jdk安装包下载速度太慢了,这是我下载下来保存的

=======================================================

elasticsearch windows安装包:链接: https://pan.baidu.com/s/1fE6rE-lGuwJuA29_0C_VXw 提取码: 5v2v

Java JDK windows64位安装包:链接: https://pan.baidu.com/s/1NVuARW_cK6WeNy-VhTEiVQ 提取码: 8jxs 

curl  windows安装包:链接: https://pan.baidu.com/s/1RLOQOWEo2vt_8YkK7fI-ZQ 提取码: wpny 

=======================================================

 

Elasticsearch需要先安装JDK并配置环境变量,之后下载安装Elasticsearch

 

 

 启动 ElasticSearch

E:\elasticsearch-7.5.0-windows-x86_64\elasticsearch-7.5.0\bin>elasticsearch.bat

启动成功

 

 

 启动成功后浏览器地址栏输入:http://localhost:9200/

 

 

 创建索引

curl -H "Content-Type: application/json" -XPUT "http://localhost:9200/movies/movie/1" -d "{\"title\": \"The Godfather\",\"director\":\"Francis Ford Coppola\",\"year\": 1972}"
{"_index":"movies","_type":"movie","_id":"1","_version":3,"result":"updated","_shards":{"total":2,"successful":1,"failed":0},"_seq_no":2,"_primary_term":1}

注意,{}内的双引号记得加反斜杠\转义

由ID获取文档/索引

curl  -H "Content-Type: application/json" -XGET "http://localhost:9200/movies/movie/1" -d''
{"_index":"movies","_type":"movie","_id":"1","_version":6,"_seq_no":5,"_primary_term":1,"found":true,"_source":{"title": "The Godfather","director":"Francis Ford Coppola","year": 1972}}

删除文档

curl  -H "Content-Type: application/json" -XGET "http://localhost:9200/movies/movie/1" -d''
{"_index":"movies","_type":"movie","_id":"1","found":false}

搜索所有索引和类型 _search

curl -H "Content-Type: application/json"  -XGET "http://localhost:9200/_search"
{"took":1,"timed_out":false,"_shards":{"total":3,"successful":3,"skipped":0,"failed":0},"hits":{"total":{"value":3,"relation":"eq"},"max_score":1.0,"hits":[{"_index":"movies","_type":"movie","_id":"1","_score":1.0,"_source":{"title": "gangtie","director": "pfeiliu","year": 1999,"genres": ["nishishui", "asdwqe"]}},{"_index":"movies","_type":"movie","_id":"2","_score":1.0,"_source":{"title": "The Godfather","director": "Francis Ford Coppola","year": 1972,"genres": ["Crime", "Drama"]}},{"_index":"movies","_type":"movie","_id":"3","_score":1.0,"_source":{"title": "lawer","director": "lean","year": 1989,"genres": ["youar", "pl"]}}]}}

搜索某索引的数据类型

curl -H "Content-Type: application/json"  -XGET "http://localhost:9200/movies/_search"
{"took":1,"timed_out":false,"_shards":{"total":1,"successful":1,"skipped":0,"failed":0},"hits":{"total":{"value":3,"relation":"eq"},"max_score":1.0,"hits":[{"_index":"movies","_type":"movie","_id":"1","_score":1.0,"_source":{"title": "gangtie","director": "pfeiliu","year": 1999,"genres": ["nishishui", "asdwqe"]}},{"_index":"movies","_type":"movie","_id":"2","_score":1.0,"_source":{"title": "The Godfather","director": "Francis Ford Coppola","year": 1972,"genres": ["Crime", "Drama"]}},{"_index":"movies","_type":"movie","_id":"3","_score":1.0,"_source":{"title": "lawer","director": "lean","year": 1989,"genres": ["youar", "pl"]}}]}}

在指定索引中显式搜索指定类型的文档

curl -H "Content-Type: application/json"  -XGET "http://localhost:9200/movies/movie/_search"
{"took":1,"timed_out":false,"_shards":{"total":1,"successful":1,"skipped":0,"failed":0},"hits":{"total":{"value":3,"relation":"eq"},"max_score":1.0,"hits":[{"_index":"movies","_type":"movie","_id":"1","_score":1.0,"_source":{"title": "gangtie","director": "pfeiliu","year": 1999,"genres": ["nishishui", "asdwqe"]}},{"_index":"movies","_type":"movie","_id":"2","_score":1.0,"_source":{"title": "The Godfather","director": "Francis Ford Coppola","year": 1972,"genres": ["Crime", "Drama"]}},{"_index":"movies","_type":"movie","_id":"3","_score":1.0,"_source":{"title": "lawer","director": "lean","year": 1989,"genres": ["youar", "pl"]}}]}}

 

常见错误处理:

错误:{"error":"Content-Type header [application/x-www-form-urlencoded] is not supported","status":406}

解决:添加:-H "Content-Type: application/json"

 

错误:{"error":{"root_cause":[{"type":"mapper_parsing_exception","reason":"failed to parse"}],"type":"mapper_parsing_exception","reason":"failed to parse","caused_by":{"type":"json_e_o_f_exception","reason":"Unexpected end-of-input in field name\n at [Source: org.elasticsearch.common.bytes.BytesReference$MarkSupportingStreamInputWrapper@3c1b21d; line: 1, column: 133]"}},"status":400}curl: (3) unmatched close brace/bracket in URL position 5:
1972}

解决:在{}内的双引号都加上一个"\"转义

 

查看集群健康状况:

浏览器输入:http://localhost:9200/_cat

 

 或者http://localhost:9200/_cat/health?v

 

 查看所有索引:

http://localhost:9200/_cat/indices?v

posted @ 2019-12-14 19:26  腹肌猿  阅读(442)  评论(0编辑  收藏  举报