初学Elasticsearch,按照阮一峰老师的教程进行实验。
实验环境:
linux-16.04
java-oracle-jdk8
Elasticsearch-6.3.0
./bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v6.3.0/elasticsearch-analysis-ik-6.3.0.zip
出现:
Exception in thread "main" java.net.NoRouteToHostException: No route to host (Host unreachable)
这样的一个timeout错误,原因为是在公司proxy下运行,且java程序 install 时先下载文件,所以无法访问。
解决方案:
本地安装。
./bin/elasticsearch-plugin install file:~/Downloads/elasticsearch-analysis-ik-6.3.0.zip
$ curl -X PUT 'localhost:9200/accounts' -d '
{
"mappings": {
"person": {
"properties": {
"user": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_max_word"
},
"title": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_max_word"
},
"desc": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_max_word"
}
}
}
}
}'
报错:
{"error":"Content-Type header [application/x-www-form-urlencoded] is not supported
原因为ElasticSearch 6.0版本之后有严格的格式检验
可以在建立index时,加入:-H 'Content-Type: application/json'
如
$ curl -X PUT 'localhost:9200/accounts' -H 'Content-Type: application/json' -d '
{
"mappings": {
"person": {
"properties": {
"user": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_max_word"
},
"title": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_max_word"
},
"desc": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_max_word"
}
}
}
}
}'
类似的,插入记录
$ curl -X PUT 'localhost:9200/accounts/person/1' -d '
{
"user": "张三",
"title": "工程师",
"desc": "数据库管理"
}'
转为
$ curl -X PUT 'localhost:9200/accounts/person/1' -H 'Content-Type: application/json' -d '
{
"user": "张三",
"title": "工程师",
"desc": "数据库管理"
}'