美文网首页
elasticsearch使用

elasticsearch使用

作者: 昕有灵犀wx | 来源:发表于2018-03-21 11:17 被阅读0次

./bin/elasticsearch

查看服务情况

curl -XGET 'localhost:9200/?pretty'

健康体检

curl -XGET 'localhost:9200/_cat/health?v&pretty'

查看节点情况

curl -XGET 'localhost:9200/_cat/nodes?v&pretty'

查看当前节点的所有 Index

curl -XGET 'localhost:9200/_cat/indices?v&pretty'

列出所有Index 所包含的Type(字段)

curl 'localhost:9200/_mapping?pretty=true'

查看所有索引使用情况

curl -XGET 'localhost:9200/_cat/indices?v&pretty'

PUT提交新数据

curl -XPUT 'localhost:9200/customer/_doc/1?pretty&pretty' -H 'Content-Type: application/json' -d'

{

  "name": "John Doe"

}

'

curl -XGET 'localhost:9200/customer/_doc/1?pretty&pretty'

创建索引

curl -XPUT 'localhost:9200/customer?pretty'

索引删除

curl -XDELETE 'localhost:9200/customer?pretty'

返回所有记录

curl 'localhost:9200/website/blog/_search'

新建文档(类似mysql insert插入操作)

curl -XPUT 'localhost:9200/website/blog/123?pretty' -H 'Content-Type: application/json' -d'

{

  "title": "My first blog entry",

  "text":  "Just trying this out...",

  "date":  "2014/01/01"

}'

另一个方式,使用post,不用指定ID

curl -XPOST 'localhost:9200/website/blog?pretty' -H 'Content-Type: application/json' -d'

{

  "title": "My second blog entry",

  "text":  "Still trying this out...",

  "date":  "2014/01/01"

}'

按ID检索

curl -XGET 'localhost:9200/website/blog/a5NMRmIBso-Mz5SmHtAU?pretty'

curl -XGET 'localhost:9200/website/blog/123?pretty'

检索部分字段

GET /website/blog/123?_source=title,text

只想得到_source字段而不要其他的元数据,你可以这样请求:

GET /website/blog/123/_source

检查文档是否存在

如果你想做的只是检查文档是否存在——你对内容完全不感兴趣——使用HEAD方法来代替GET。HEAD请求不会返回响应体,只有HTTP头:

curl -XHEAD 'http://localhost:9200/website/blog/123'

更新整个文档

文档在Elasticsearch中是不可变的——我们不能修改他们。如果需要更新已存在的文档,我们可以使用《索引文档》章节提到的index API 重建索引(reindex) 或者替换掉它。

curl -XPUT 'localhost:9200/website/blog/123?pretty' -H 'Content-Type: application/json' -d'

{

  "title": "My first blog entry2",

  "text":  "Just trying this out2...",

  "date":  "2014/01/02"

}'

在响应中,我们可以看到Elasticsearch把_version增加了。

{

  "_index" :  "website",

  "_type" :    "blog",

  "_id" :      "123",

  "_version" : 2,

  "created":  false <1>

}

删除文档

删除文档的语法模式与之前基本一致,只不过要使用DELETE方法:

DELETE /website/blog/123

如果文档被找到,Elasticsearch将返回200 OK状态码和以下响应体。注意_version数字已经增加了。

{

  "found" :    true,

  "_index" :  "website",

  "_type" :    "blog",

  "_id" :      "123",

  "_version" : 3

}

如果文档未找到,我们将得到一个404 Not Found状态码,响应体是这样的:

{

  "found" :    false,

  "_index" :  "website",

  "_type" :    "blog",

  "_id" :      "123",

  "_version" : 4

}

尽管文档不存在——"found"的值是false——_version依旧增加了。这是内部记录的一部分,它确保在多节点间不同操作可以有正确的顺序。

删除一个文档也不会立即从磁盘上移除,它只是被标记成已删除。Elasticsearch将会在你之后添加更多索引的时候才会在后台进行删除内容的清理。

相关文章

网友评论

      本文标题:elasticsearch使用

      本文链接:https://www.haomeiwen.com/subject/hgbwqftx.html