到elasticsearch网站下载最新版本的elasticsearch 6.2.1html
https://www.elastic.co/downloads/elasticsearch
中文文档请参考java
https://www.elastic.co/guide/cn/elasticsearch/guide/current/index.html
英文文档及其Java API使用方法请参考,官方文档比任何博客均可信数据库
https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/index.html
Python API使用方法json
http://elasticsearch-py.readthedocs.io/en/master/
下载tar包,而后解压到/usr/local目录下,修改一下用户和组以后能够使用非root用户启动,启动命令bootstrap
./bin/elasticsearch
而后访问http://127.0.0.1:9200/api
若是须要让外网访问Elasticsearch的9200端口的话,须要将es的host绑定到外网bash
修改 /configs/elasticsearch.yml文件,添加以下app
network.host: 0.0.0.0 http.port: 9200
而后重启,若是遇到下面问题的话curl
[2018-01-28T23:51:35,204][INFO ][o.e.b.BootstrapChecks ] [qR5cyzh] bound or publishing to a non-loopback address, enforcing bootstrap checks ERROR: [2] bootstrap checks failed [1]: max file descriptors [4096] for elasticsearch process is too low, increase to at least [65536] [2]: max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
解决方法elasticsearch
在root用户下执行
sysctl -w vm.max_map_count=262144
接下来导入json格式的数据,数据内容以下
{"index":{"_id":"1"}} {"title":"许宝江","url":"7254863","chineseName":"许宝江","sex":"男","occupation":" 滦县农业局局长","nationality":"中国"} {"index":{"_id":"2"}} {"title":"鲍志成","url":"2074015","chineseName":"鲍志成","occupation":"医师","nationality":"中国","birthDate":"1901年","deathDate":"1973年","graduatedFrom":"香港大学"}
须要注意的是{"index":{"_id":"1"}}和文件末尾另起一行换行是不可少的
其中的id能够从0开始,甚至是abc等等
不然会出现400状态,错误提示分别为
Malformed action/metadata line [1], expected START_OBJECT or END_OBJECT but found [VALUE_STRING]
The bulk request must be terminated by a newline [\n]"
使用下面命令来导入json文件
其中的people.json为文件的路径,能够是/home/common/下载/xxx.json
其中的es是index,people是type,在elasticsearch中的index和type能够理解成关系数据库中的database和table,二者都是必不可少的
curl -H "Content-Type: application/json" -XPOST 'localhost:9200/es/people/_bulk?pretty&refresh' --data-binary "@people.json"
成功后的返回值是200,好比
{ "took" : 233, "errors" : false, "items" : [ { "index" : { "_index" : "es", "_type" : "people", "_id" : "1", "_version" : 1, "result" : "created", "forced_refresh" : true, "_shards" : { "total" : 2, "successful" : 1, "failed" : 0 }, "_seq_no" : 0, "_primary_term" : 1, "status" : 201 } }, { "index" : { "_index" : "es", "_type" : "people", "_id" : "2", "_version" : 1, "result" : "created", "forced_refresh" : true, "_shards" : { "total" : 2, "successful" : 1, "failed" : 0 }, "_seq_no" : 0, "_primary_term" : 1, "status" : 201 } } ] }
<0>查看字段的mapping
http://localhost:9200/es/people/_mapping
接下来能够使用对应的查询语句对数据进行查询
<1>按id来查询
http://localhost:9200/es/people/1
<2>简单的匹配查询,查询某个字段中包含某个关键字的数据(GET)
http://localhost:9200/es/people/_search?q=_id:1
http://localhost:9200/es/people/_search?q=title:许
<3>多字段查询,在多个字段中查询包含某个关键字的数据(POST)
能够使用Firefox中的RESTer插件来构造一个POST请求,在升级到Firefox quantum以后,原来使用的Poster插件挂了
在title和sex字段中查询包含 许 字的数据
{ "query": { "multi_match" : { "query" : "许", "fields": ["title", "sex"] } } }
还能够额外指定返回值
size指定返回的数量
from指定返回的id起始值
_source指定返回的字段
highlight指定语法高亮
{ "query": { "multi_match" : { "query" : "中国", "fields": ["nationality", "sex"] } }, "size": 2, "from": 0, "_source": [ "title", "sex", "nationality" ], "highlight": { "fields" : { "title" : {} } } }
<4>Boosting
用于提高字段的权重,能够将max_score的分数乘以一个系数
{ "query": { "multi_match" : { "query" : "中国", "fields": ["nationality^3", "sex"] } }, "size": 2, "from": 0, "_source": [ "title", "sex", "nationality" ], "highlight": { "fields" : { "title" : {} } } }
<5>组合查询,能够实现一些比较复杂的查询
AND -> must
NOT -> must not
OR -> should
{ "query": { "bool": { "must": { "bool" : { "should": [ { "match": { "title": "鲍" }}, { "match": { "title": "许" }} ], "must": { "match": {"nationality": "中国" }} } }, "must_not": { "match": {"sex": "女" }} } } }
<6>模糊(Fuzzy)查询(POST)
{ "query": { "multi_match" : { "query" : "厂长", "fields": ["title", "sex","occupation"], "fuzziness": "AUTO" } }, "_source": ["title", "sex", "occupation"], "size": 1 }
经过模糊匹配将 厂长 和 局长 匹配上
AUTO的时候,当query的长度大于5的时候,模糊值指定为2
<7>通配符(Wildcard)查询(POST)
?
匹配任何字符
*
匹配零个或多个字
{ "query": { "wildcard" : { "title" : "*宝" } }, "_source": ["title", "sex", "occupation"], "size": 1 }
<8>正则(Regexp)查询(POST)
{ "query": { "regexp" : { "authors" : "t[a-z]*y" } }, "_source": ["title", "sex", "occupation"], "size": 3 }
<9>短语匹配(Match Phrase)查询(POST)
短语匹配查询 要求在请求字符串中的全部查询项必须都在文档中存在,文中顺序也得和请求字符串一致,且彼此相连。
默认状况下,查询项之间必须紧密相连,但能够设置 slop
值来指定查询项之间能够分隔多远的距离,结果仍将被看成一次成功的匹配。
{ "query": { "multi_match" : { "query" : "许长江", "fields": ["title", "sex","occupation"], "type": "phrase" } }, "_source": ["title", "sex", "occupation"], "size": 3 }
注意使用slop的时候距离是累加的,滦农局 和 滦县农业局 差了2个距离
{ "query": { "multi_match" : { "query" : "滦农局", "fields": ["title", "sex","occupation"], "type": "phrase", "slop":2 } }, "_source": ["title", "sex", "occupation"], "size": 3 }
<10>短语前缀(Match Phrase Prefix)查询(POST)