当前位置:   article > 正文

ES安装以及使用_es全文检索 kaban

es全文检索 kaban

1.安装

java升级到jdk8,下载es

https://www.elastic.co/downloads/elasticsearch

解压es压缩包,启动es

./bin/elasticsearch

访问ES:http://localhost:9200/

显示:

  1. {
  2. "name" : "pleYB0j",
  3. "cluster_name" : "elasticsearch",
  4. "cluster_uuid" : "tRrh3XC_Rka1huf1zMOTSg",
  5. "version" : {
  6. "number" : "5.3.0",
  7. "build_hash" : "3adb13b",
  8. "build_date" : "2017-03-23T03:31:50.652Z",
  9. "build_snapshot" : false,
  10. "lucene_version" : "6.4.1"
  11. },
  12. "tagline" : "You Know, for Search"
  13. }

2.ES命令

参考:https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html
ES命令格式:
curl -<REST Verb> <Node>:<Port>/<Index>/<Type><ID>

2.1创建索引

curl -XPUT 'localhost:9200/customer?pretty'
第一个命令使用PUT创建了一个叫做“customer”的索引。我们简单地将pretty附加到调用的尾部,使其以美观的形式打印出JSON响应
访问:http://localhost:9200/_cat/indices?v
  1. health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
  2. yellow open customer -D-eQGuNT-uG-g0MkuiBGg 5 1 0 0 650b 650b
第二个命令的结果告知我们,我们现在有一个叫做customer的索引,并且它有5个主分片和1份复制(都是默认值),其中包含0个文档。

2.2索引并查询文档

curl -XPUT 'localhost:9200/customer/external/1?pretty' -d ' {"name": "John Doe"}'
输出:
  1. {
  2. "_index" : "customer",
  3. "_type" : "external",
  4. "_id" : "1",
  5. "_version" : 1,
  6. "result" : "created",
  7. "_shards" : {
  8. "total" : 2,
  9. "successful" : 1,
  10. "failed" : 0
  11. },
  12. "created" : true
  13. }
将一个简单的客户文档索引到customer索引、“external”类型中,这个文档的ID是1
取出文档:
 curl -XGET 'localhost:9200/customer/external/1?pretty'
结果:
  1. {
  2. "_index" : "customer",
  3. "_type" : "external",
  4. "_id" : "1",
  5. "_version" : 1,
  6. "found" : true,
  7. "_source" : {
  8. "name" : "John Doe"
  9. }
  10. }
除了一个叫做found的字段来指明我们找到了一个ID为1的文档,和另外一个字段——_source——返回我们前一步中索引的完整JSON文档之外,其它的都没有什么特别之处。

2.3删除索引

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




3.DEMO

导入测试数据
curl -XPOST 'localhost:9200/bank/account/_bulk?pretty' --data-binary @accounts.json

测试数据acounts.json,有两条数据,如下所示:
  1. {"index":{"_id":"1"}}
  2. {"account_number":1,"balance":39225,"firstname":"Amber","lastname":"Duke","age":32,"gender":"M","address":"880 Holmes Lane","employer":"Pyrami","email":"amberduke@pyrami.com","city":"Brogan","state":"IL"}
  3. {"index":{"_id":"6"}}
  4. {"account_number":6,"balance":5686,"firstname":"Hattie","lastname":"Bond","age":36,"gender":"M","address":"671 Bristol Street","employer":"Netagy","email":"hattiebond@netagy.com","city":"Dante","state":"TN"}

查询数据:
  curl -XPOST 'localhost:9200/bank/_search?pretty' -d ' { "query": { "match": { "account_number": 1 } } }'
结果:
  1. {
  2. "took" : 11,
  3. "timed_out" : false,
  4. "_shards" : {
  5. "total" : 5,
  6. "successful" : 5,
  7. "failed" : 0
  8. },
  9. "hits" : {
  10. "total" : 1,
  11. "max_score" : 1.0,
  12. "hits" : [
  13. {
  14. "_index" : "bank",
  15. "_type" : "account",
  16. "_id" : "1",
  17. "_score" : 1.0,
  18. "_source" : {
  19. "account_number" : 1,
  20. "balance" : 39225,
  21. "firstname" : "Amber",
  22. "lastname" : "Duke",
  23. "age" : 32,
  24. "gender" : "M",
  25. "address" : "880 Holmes Lane",
  26. "employer" : "Pyrami",
  27. "email" : "amberduke@pyrami.com",
  28. "city" : "Brogan",
  29. "state" : "IL"
  30. }
  31. }
  32. ]
  33. }
  34. }








声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/894302
推荐阅读
相关标签
  

闽ICP备14008679号