wind-cold 发表于 2019-1-29 12:39:15

54 搜索引擎及Lucene基础、elasticsearch使用基础、ES使用详解、ES查询及Logstash入门

  01搜索引擎及Lucene基础
  

  02elasticsearch使用基础
  配置环境
  node1:192.168.1.131CentOS Linux release 7.2
  node2:192.168.1.132CentOS Linux release 7.2
  node3:192.168.1.133CentOS Linux release 7.2
  

  # vim /etc/profile.d/java.sh
  export JAVA_HOME=/usr
  # yum install java-1.7.0-openjdk-devel.x86_64-y
  # ls elasticsearch-1.7.2.noarch.rpm
  elasticsearch-1.7.2.noarch.rpm
  # yum install elasticsearch-1.7.2.noarch.rpm -y
  # cd /etc/elasticsearch/
  # ls
  elasticsearch.ymllogging.yml
  # vim elasticsearch.yml
  修改
  #cluster.name: elasticsearch
  为
  cluster.name: myes
  修改
  #node.name: "Franz Kafka"
  为
  node.name: "node1"
  # systemctl daemon-reload
  # systemctl start elasticsearch.service
  # scp /etc/profile.d/java.sh node2:/etc/profile.d/
  # scp /etc/profile.d/java.sh node3:/etc/profile.d/
  # scp elasticsearch-1.7.2.noarch.rpm node2:/root
  # scp elasticsearch-1.7.2.noarch.rpm node3:/root
  

  # yum makecache
  # yum install java-1.7.0-openjdk-devel.x86_64 -y
  # yum -y install elasticsearch-1.7.2.noarch.rpm
  # vim /etc/elasticsearch/elasticsearch.yml
  修改
  #cluster.name: elasticsearch
  为
  cluster.name: myes
  修改
  #node.name: "Franz Kafka"
  为
  node.name: "node2"
  # systemctl daemon-reload
  # systemctl start elasticsearch.service   
  # tcpdump -i eno16777736 -nn tcp port 9300
  tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
  listening on eno16777736, link-type EN10MB (Ethernet), capture size 65535 bytes
  10:56:33.167425 IP 192.168.1.131.52903 > 192.168.1.132.9300: Flags , seq 1407746623:1407746775, ack 4084412475, win 229, options , length 152
  10:56:33.168800 IP 192.168.1.132.9300 > 192.168.1.131.52903: Flags , seq 1:21, ack 152, win 1432, options , length 20
  10:56:33.169334 IP 192.168.1.132.33552 > 192.168.1.131.9300: Flags , seq 2860848390:2860848499, ack 2667737508, win 229, options , length 109
  10:56:33.169468 IP 192.168.1.131.52903 > 192.168.1.132.9300: Flags [.], ack 21, win 229, options , length 0
  10:56:33.170588 IP 192.168.1.131.9300 > 192.168.1.132.33552: Flags , seq 1:21, ack 109, win 227, options , length 20
  10:56:33.170632 IP 192.168.1.132.33552 > 192.168.1.131.9300: Flags [.], ack 21, win 229, options , length 0
  ^C
  6 packets captured
  6 packets received by filter
  0 packets dropped by kernel
  

  # yum makecache
  # yum install java-1.7.0-openjdk-devel.x86_64 -y
  # yum -y install elasticsearch-1.7.2.noarch.rpm
  # vim /etc/elasticsearch/elasticsearch.yml
  修改
  #cluster.name: elasticsearch
  为
  cluster.name: myes
  修改
  #node.name: "Franz Kafka"
  为
  node.name: "node3"
  # systemctl daemon-reload
  # systemctl start elasticsearch.service
  

  # curl -X GET 'http://192.168.1.131:9200/?preey'
  {
  "status" : 200,
  "name" : "node1",
  "cluster_name" : "myes",
  "version" : {
      "number" : "1.7.2",
      "build_hash" : "e43676b1385b8125d647f593f7202acbd816e8ec",
      "build_timestamp" : "2015-09-14T09:49:53Z",
      "build_snapshot" : false,
      "lucene_version" : "4.10.4"
  },
  "tagline" : "You Know, for Search"
  }
  

  # curl -X GET 'http://192.168.1.132:9200/?preey'
  {
  "status" : 200,
  "name" : "node2",
  "cluster_name" : "myes",
  "version" : {
      "number" : "1.7.2",
      "build_hash" : "e43676b1385b8125d647f593f7202acbd816e8ec",
      "build_timestamp" : "2015-09-14T09:49:53Z",
      "build_snapshot" : false,
      "lucene_version" : "4.10.4"
  },
  "tagline" : "You Know, for Search"
  }
  

  # curl -X GET 'http://192.168.1.133:9200/?preey'
  {
  "status" : 200,
  "name" : "node3",
  "cluster_name" : "myes",
  "version" : {
      "number" : "1.7.2",
      "build_hash" : "e43676b1385b8125d647f593f7202acbd816e8ec",
      "build_timestamp" : "2015-09-14T09:49:53Z",
      "build_snapshot" : false,
      "lucene_version" : "4.10.4"
  },
  "tagline" : "You Know, for Search"
  }
  

  # curl -X GET 'http://192.168.1.133:9200/_cat/'
  =^.^=
  /_cat/allocation
  /_cat/shards
  /_cat/shards/{index}
  /_cat/master
  /_cat/nodes
  /_cat/indices
  /_cat/indices/{index}
  /_cat/segments
  /_cat/segments/{index}
  /_cat/count
  /_cat/count/{index}
  /_cat/recovery
  /_cat/recovery/{index}
  /_cat/health
  /_cat/pending_tasks
  /_cat/aliases
  /_cat/aliases/{alias}
  /_cat/thread_pool
  /_cat/plugins
  /_cat/fielddata
  /_cat/fielddata/{fields}
  

  # curl -X GET 'http://192.168.1.133:9200/_cat/nodes'
  node2 192.168.1.132 5 18 0.00 d m node2
  node1 192.168.1.131 6 18 0.08 d * node1
  node3 192.168.1.133 4 18 0.01 d m node3
  

  # curl -X GET 'http://192.168.1.133:9200/_cat/nodes?v'
  hostip            heap.percent ram.percent load node.role master name
  node2 192.168.1.132            5          19 0.00 d         m      node2
  node1 192.168.1.131            6          18 0.03 d         *      node1
  node3 192.168.1.133            4          18 0.00 d         m      node3
  

  #显示帮助
  # curl -X GET 'http://192.168.1.133:9200/_cat/nodes?help'
  

  # curl -X GET 'http://192.168.1.133:9200/_cat/nodes?h=name,ip,port,uptime,heap.current'
  node2 192.168.1.132 9300 38.1m 53.8mb
  node1 192.168.1.131 9300 53.1m 64.8mb
  node3 192.168.1.133 9300 30.9m 50.5mb
  

  #查看主节点
  # curl -X GET 'http://192.168.1.133:9200/_cat/master'hdbLm-k-Q4eY9RxeMvAJDw node1 192.168.1.131 node1
  #详细格式
  # curl -X GET 'http://192.168.1.133:9200/_cat/master?v'
  id                     hostip            node
  hdbLm-k-Q4eY9RxeMvAJDw node1 192.168.1.131 node1
  

  #显示健康状态
  # curl -X GET 'http://192.168.1.133:9200/_cat/health'1483328079 11:34:39 myes green 3 3 0 0 0 0 0 0
  #详细格式
  # curl -X GET 'http://192.168.1.133:9200/_cat/health?v'epoch      timestamp cluster status node.total node.data shards pri relo init unassign pending_tasks
  1483328108 11:35:08myes    green         3         3      0   0    0    0      0             0
  

  

  03ES使用详解
  

  # curl -X GET 'http://192.168.1.133:9200/_cluster/health?level=cluster'
  {"cluster_name":"myes","status":"green","timed_out":false,"number_of_nodes":3,"number_of_data_nodes":3,"active_primary_shards":0,"active_shards":0,"relocating_shards":0,"initializing_shards":0,"unassigned_shards":0,"delayed_unassigned_shards":0,"number_of_pending_tasks":0,"number_of_in_flight_fetch":0}
  

  # curl -X GET 'http://192.168.1.133:9200/_cluster/health?level=indicies&pretty'
  {
  "cluster_name" : "myes",
  "status" : "green",
  "timed_out" : false,
  "number_of_nodes" : 3,
  "number_of_data_nodes" : 3,
  "active_primary_shards" : 0,
  "active_shards" : 0,
  "relocating_shards" : 0,
  "initializing_shards" : 0,
  "unassigned_shards" : 0,
  "delayed_unassigned_shards" : 0,
  "number_of_pending_tasks" : 0,
  "number_of_in_flight_fetch" : 0
  }
  

  # curl -X GET 'http://192.168.1.133:9200/_cluster/state/version'{"cluster_name":"myes","version":4}
  

  # curl -X GET 'http://192.168.1.133:9200/_cluster/state/version?pretty'{
  "cluster_name" : "myes",
  "version" : 4
  }
  

  # curl -X GET 'http://192.168.1.133:9200/_cluster/state/master_node?pretty'
  {
  "cluster_name" : "myes",
  "master_node" : "hdbLm-k-Q4eY9RxeMvAJDw"
  }
  

  # curl -X GET 'http://192.168.1.133:9200/_cluster/state/nodes?pretty'
  {
  "cluster_name" : "myes",
  "nodes" : {
      "Jrbz7cRSQlaBH3zHw5x__g" : {
        "name" : "node2",
        "transport_address" : "inet",
        "attributes" : { }
      },
      "hdbLm-k-Q4eY9RxeMvAJDw" : {
        "name" : "node1",
        "transport_address" : "inet",
        "attributes" : { }
      },
      "A7dqV-odSDOdPgscxEpWWg" : {
        "name" : "node3",
        "transport_address" : "inet",
        "attributes" : { }
      }
  }
  }
  

  # curl -X GET 'http://192.168.1.133:9200/_cluster/state/nodes?pretty'{
  "cluster_name" : "myes",
  "nodes" : {
      "Jrbz7cRSQlaBH3zHw5x__g" : {
        "name" : "node2",
        "transport_address" : "inet",
        "attributes" : { }
      },
      "hdbLm-k-Q4eY9RxeMvAJDw" : {
        "name" : "node1",
        "transport_address" : "inet",
        "attributes" : { }
      },
      "A7dqV-odSDOdPgscxEpWWg" : {
        "name" : "node3",
        "transport_address" : "inet",
        "attributes" : { }
      }
  }
  }
  

  # curl -X GET 'http://192.168.1.132:9200/_cluster/stats?pretty'
  {
  "timestamp" : 1483330621606,
  "cluster_name" : "myes",
  "status" : "green",
  "indices" : {
      "count" : 0,
      "shards" : { },
      "docs" : {
        "count" : 0,
        "deleted" : 0
      },
      "store" : {
        "size_in_bytes" : 0,
        "throttle_time_in_millis" : 0
      },
      "fielddata" : {
        "memory_size_in_bytes" : 0,
        "evictions" : 0
      },
      "filter_cache" : {
        "memory_size_in_bytes" : 0,
        "evictions" : 0
      },
      "id_cache" : {
        "memory_size_in_bytes" : 0
      },
      "completion" : {
        "size_in_bytes" : 0
      },
      "segments" : {
        "count" : 0,
        "memory_in_bytes" : 0,
        "index_writer_memory_in_bytes" : 0,
        "index_writer_max_memory_in_bytes" : 0,
        "version_map_memory_in_bytes" : 0,
        "fixed_bit_set_memory_in_bytes" : 0
      },
      "percolate" : {
        "total" : 0,
        "time_in_millis" : 0,
        "current" : 0,
        "memory_size_in_bytes" : -1,
        "memory_size" : "-1b",
        "queries" : 0
      }
  },
  "nodes" : {
      "count" : {
        "total" : 3,
        "master_only" : 0,
        "data_only" : 0,
        "master_data" : 3,
        "client" : 0
      },
      "versions" : [ "1.7.2" ],
      "os" : {
        "available_processors" : 12,
        "mem" : {
        "total_in_bytes" : 11903852544
        },
        "cpu" : [ {
        "vendor" : "Intel",
        "model" : "Xeon",
        "mhz" : 2600,
        "total_cores" : 4,
        "total_sockets" : 2,
        "cores_per_socket" : 2,
        "cache_size_in_bytes" : 15360,
        "count" : 3
        } ]
      },
      "process" : {
        "cpu" : {
        "percent" : 3
        },
        "open_file_descriptors" : {
        "min" : 203,
        "max" : 204,
        "avg" : 203
        }
      },
      "jvm" : {
        "max_uptime_in_millis" : 5914721,
        "versions" : [ {
        "version" : "1.8.0_101",
        "vm_name" : "OpenJDK 64-Bit Server VM",
        "vm_version" : "25.101-b13",
        "vm_vendor" : "Oracle Corporation",
        "count" : 3
        } ],
        "mem" : {
        "heap_used_in_bytes" : 183060568,
        "heap_max_in_bytes" : 3116630016
        },
        "threads" : 129
      },
      "fs" : {
        "total_in_bytes" : 64393052160,
        "free_in_bytes" : 51360722944,
        "available_in_bytes" : 51360722944,
        "disk_reads" : 72943,
        "disk_writes" : 30331,
        "disk_io_op" : 103274,
        "disk_read_size_in_bytes" : 1429777920,
        "disk_write_size_in_bytes" : 1544204288,
        "disk_io_size_in_bytes" : 2973982208
      },
      "plugins" : [ ]
  }
  }
  

  # /usr/share/elasticsearch/bin/plugin -h
  Usage:
      -u, --url      : Set exact URL to download the plugin from
      -i, --install        : Downloads and installs listed plugins
[*]
      -t, --timeout           : Timeout setting: 30s, 1m, 1h... (infinite by default)
      -r, --remove       : Removes listed plugins
      -l, --list                        : List installed plugins
      -v, --verbose                     : Prints verbose messages
      -s, --silent                      : Run in silent mode
      -h, --help                        : Prints this help message
  

  
[*] Plugin name could be:
     elasticsearch/plugin/version for official elasticsearch plugins (download from download.elasticsearch.org)
     groupId/artifactId/version   for community plugins (download from maven central or oss sonatype)
     username/repository          for site plugins (download from github master)
  
  # /usr/share/elasticsearch/bin/plugin -i marvel -u ftp://192.168.56.2/pub/Sources/7.x86_64/elk/plugins/marvel-latest.zip
  

  # /usr/share/elasticsearch/bin/plugin -i bigdesk -u file:///root/bigdesk-latest.zip -> Installing bigdesk...
  Trying file:/root/bigdesk-latest.zip...
  Downloading ....DONE
  Installed bigdesk into /usr/share/elasticsearch/plugins/bigdesk
  Identified as a _site plugin, moving to _site structure ...
  

  # /usr/share/elasticsearch/bin/plugin -l
  Installed plugins:
      - bigdesk
  

  # /usr/share/elasticsearch/bin/plugin -i head -u file:///root/elasticsearch-head-latest.zip -> Installing head...
  Trying file:/root/elasticsearch-head-latest.zip...
  Downloading .........DONE
  Installed head into /usr/share/elasticsearch/plugins/head
  

  # /usr/share/elasticsearch/bin/plugin -i kopf -u file:///root/elasticsearch-kopf-master.zip -> Installing kopf...
  Trying file:/root/elasticsearch-kopf-master.zip...
  Downloading ....................DONE
  Installed kopf into /usr/share/elasticsearch/plugins/kopf
  

  # /usr/share/elasticsearch/bin/plugin -i marvel -u file:///root/marvel-latest.zip -> Installing marvel...
  Trying file:/root/marvel-latest.zip...
  Downloading ....................DONE
  Installed marvel into /usr/share/elasticsearch/plugins/marvel
  

  # /usr/share/elasticsearch/bin/plugin -l
  Installed plugins:
      - bigdesk
      - head
      - kopf
      - marvel
  # systemctl restart elasticsearch.service
  http://192.168.1.131:9200/_plugin/marvel/kibana/#/dashboard/file/marvel.overview.json
  

  # curl -XGET 'http://192.168.1.133:9200/_cat/indices?' //192.168.56.2/pub/Sources/7.x86_64/elk/plugins/marvel-latest.zip
  green open .marvel-2017.01.02 1 1 57 0 861.4kb 512.2kb
  green open .marvel-ki
  

  # curl -XPUT 'localhost:9200/students/class1/1?pretty' -d'
  > {
  >   "first_name": "Jing",
  >   "last_name": "Guo",
  >   "gender" : "Male",
  >   "courses" : "Xianglong Shiba Zhang"
  > }'
  {
  "_index" : "students",
  "_type" : "class1",
  "_id" : "1",
  "_version" : 1,
  "created" : true
  }
  

  # curl -XPUT 'localhost:9200/students/class1/2?pretty' -d '
  > {
  >   "first_name" : "Rong",
  >   "last_name" : "Huang",
  >   "gender" : "Female",
  >   "age" : 23,
  >   "courses" : "Luoying Shenjian"
  > }'
  {
  "_index" : "students",
  "_type" : "class1",
  "_id" : "2",
  "_version" : 1,
  "created" : true
  }
  

  # curl -XGET 'localhost:9200/students/class1/1?pretty'
  {
  "_index" : "students",
  "_type" : "class1",
  "_id" : "1",
  "_version" : 1,
  "found" : true,
  "_source":
  {
  "first_name": "Jing",
  "last_name": "Guo",
  "gender" : "Male",
  "courses" : "Xianglong Shiba Zhang"
  }
  }
  

  # curl -XGET 'localhost:9200/students/class1/2?pretty'
  {
  "_index" : "students",
  "_type" : "class1",
  "_id" : "2",
  "_version" : 1,
  "found" : true,
  "_source":
  {
  "first_name" : "Rong",
  "last_name" : "Huang",
  "gender" : "Female",
  "age" : 23,
  "courses" : "Luoying Shenjian"
  }
  }
  

  # curl -XPOST 'localhost:9200/students/class1/2/_update?pretty' -d '
  {
  "doc" : { "age" : 22 }
  }'
  {
  "_index" : "students",
  "_type" : "class1",
  "_id" : "2",
  "_version" : 2
  }
  

  # curl -XGET 'localhost:9200/students/class1/2?pretty'
  {
  "_index" : "students",
  "_type" : "class1",
  "_id" : "2",
  "_version" : 2,
  "found" : true,
  "_source":{"first_name":"Rong","last_name":"Huang","gender":"Female","age":22,"courses":"Luoying Shenjian"}
  }
  

  # curl -XDELETE 'localhost:9200/students/class1/2'   
  {"found":true,"_index":"students","_type":"class1","_id":"2","_version":3}
  # curl -XGET 'localhost:9200/students/class1/2?pretty'
  {
  "_index" : "students",
  "_type" : "class1",
  "_id" : "2",
  "found" : false
  }
  

  # curl -XGET 'localhost:9200/_cat/indices?v'
  health status index            pri rep docs.count docs.deleted store.size pri.store.size
  greenopen   .marvel-2017.01.02   1   1      273            0      3.7mb          1.8mb
  greenopen   students             5   1          1            0      7.3kb          3.6kb
  greenopen   .marvel-kibana       1   1          1            0      6.5kb          3.2kb
  

  # curl -XDELETE 'localhost:9200/students'
  {"acknowledged":true}
  

  # curl -XGET 'localhost:9200/_cat/indices?v'
  health status index            pri rep docs.count docs.deleted store.size pri.store.size
  greenopen   .marvel-2017.01.02   1   1      319            0      4.2mb          2.1mb
  greenopen   .marvel-kibana       1   1          1            0      6.5kb          3.2kb
  

  

  #curl -XPUT 'localhost:9200/students/class1/1?pretty' -d'
  {
  "first_name": "Jing",
  "last_name": "Guo",
  "gender" : "Male",
  "courses" : "Xianglong Shiba Zhang"
  }'
  {
  "_index" : "students",
  "_type" : "class1",
  "_id" : "1",
  "_version" : 1,
  "created" : true
  }
  

  #curl -XPUT 'localhost:9200/students/class1/2?pretty' -d '
  {
  "first_name" : "Rong",
  "last_name" : "Huang",
  "gender" : "Female",
  "age" : 23,
  "courses" : "Luoying Shenjian"
  }'
  {
  "_index" : "students",
  "_type" : "class1",
  "_id" : "2",
  "_version" : 1,
  "created" : true
  }
  

  # curl -XGET 'localhost:9200/students/_search?pretty'
  {
  "took" : 16,
  "timed_out" : false,
  "_shards" : {
      "total" : 5,
      "successful" : 5,
      "failed" : 0
  },
  "hits" : {
      "total" : 2,
      "max_score" : 1.0,
      "hits" : [ {
        "_index" : "students",
        "_type" : "class1",
        "_id" : "1",
        "_score" : 1.0,
        "_source":
  {
  "first_name": "Jing",
  "last_name": "Guo",
  "gender" : "Male",
  "courses" : "Xianglong Shiba Zhang"
  }
      }, {
        "_index" : "students",
        "_type" : "class1",
        "_id" : "2",
        "_score" : 1.0,
        "_source":
  {
  "first_name" : "Rong",
  "last_name" : "Huang",
  "gender" : "Female",
  "age" : 23,
  "courses" : "Luoying Shenjian"
  }
      } ]
  }
  }
  

  # curl -XGET 'localhost:9200/students/_search?pretty' -d '
  > {
  >   "query" : { "match_all" : {} }
  > }'
  {
  "took" : 15,
  "timed_out" : false,
  "_shards" : {
      "total" : 5,
      "successful" : 5,
      "failed" : 0
  },
  "hits" : {
      "total" : 2,
      "max_score" : 1.0,
      "hits" : [ {
        "_index" : "students",
        "_type" : "class1",
        "_id" : "1",
        "_score" : 1.0,
        "_source":
  {
  "first_name": "Jing",
  "last_name": "Guo",
  "gender" : "Male",
  "courses" : "Xianglong Shiba Zhang"
  }
      }, {
        "_index" : "students",
        "_type" : "class1",
        "_id" : "2",
        "_score" : 1.0,
        "_source":
  {
  "first_name" : "Rong",
  "last_name" : "Huang",
  "gender" : "Female",
  "age" : 23,
  "courses" : "Luoying Shenjian"
  }
      } ]
  }
  }
  

  # curl -XGET 'localhost:9200/students/_search?q="Xianglong"'
  {"took":35,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":1,"max_score":0.11506981,"hits":[{"_index":"students","_type":"class1","_id":"1","_score":0.11506981,"_source":
  {
  "first_name": "Jing",
  "last_name": "Guo",
  "gender" : "Male",
  "courses" : "Xianglong Shiba Zhang"
  }}]}}
  

  # curl -XGET 'localhost:9200/students/_search?q="Xianglong"&pretty'
  {
  "took" : 12,
  "timed_out" : false,
  "_shards" : {
      "total" : 5,
      "successful" : 5,
      "failed" : 0
  },
  "hits" : {
      "total" : 1,
      "max_score" : 0.11506981,
      "hits" : [ {
        "_index" : "students",
        "_type" : "class1",
        "_id" : "1",
        "_score" : 0.11506981,
        "_source":
  {
  "first_name": "Jing",
  "last_name": "Guo",
  "gender" : "Male",
  "courses" : "Xianglong Shiba Zhang"
  }
      } ]
  }
  }
  

  # curl -XGET 'localhost:9200/students/_search?q="Xianglong Shiba Zhang"&pretty'curl: (52) Empty reply from server
  

  # curl -XGET 'localhost:9200/students/_search?q=courses:"Xianglong"&pretty'{
  "took" : 9,
  "timed_out" : false,
  "_shards" : {
      "total" : 5,
      "successful" : 5,
      "failed" : 0
  },
  "hits" : {
      "total" : 1,
      "max_score" : 0.15342641,
      "hits" : [ {
        "_index" : "students",
        "_type" : "class1",
        "_id" : "1",
        "_score" : 0.15342641,
        "_source":
  {
  "first_name": "Jing",
  "last_name": "Guo",
  "gender" : "Male",
  "courses" : "Xianglong Shiba Zhang"
  }
      } ]
  }
  }
  

  # curl -XGET 'localhost:9200/students/_search?q=courses:"Xianglong Shiba Zhang"&pretty'
  curl: (52) Empty reply from server
  

  # curl -XDELETE 'localhost:9200/students/class1/1'
  {"found":true,"_index":"students","_type":"class1","_id":"1","_version":2}
  

  # curl -XPUT 'localhost:9200/students/class1/1?pretty' -d '
  > {
  >   "name" : "Guo Jing",
  >   "gender" : "Male",
  >   "age" : 25,
  >   "class" : "Gai Bang"
  >}
  > '
  {
  "_index" : "students",
  "_type" : "class1",
  "_id" : "1",
  "_version" : 1,
  "created" : true
  }
  

  # curl -XPUT 'localhost:9200/students/class1/1?pretty' -d '
  {
  "name" : "Yang Guo",
  "gender" : "Male",
  "age" : 17,
  "class" : "Gmu Pai"
   }
  '
  {
  "_index" : "students",
  "_type" : "class1",
  "_id" : "1",
  "_version" : 2,
  "created" : false
  }
  

  # curl -XPUT 'localhost:9200/students/class1/2?pretty' -d '
  {
  "name" : "Guo Jing",
  "gender" : "Male",
  "age" : 25,
  "class" : "Gai Bang"
   }
  '
  {
  "_index" : "students",
  "_type" : "class1",
  "_id" : "2",
  "_version" : 2,
  "created" : false
  }
  

  # curl -XGET 'localhost:9200/students/_search?q="Guo"'
  {"took":23,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":2,"max_score":0.11506981,"hits":[{"_index":"students","_type":"class1","_id":"1","_score":0.11506981,"_source":
  {
  "name" : "Yang Guo",
  "gender" : "Male",
  "age" : 17,
  "class" : "Gmu Pai"
   }
  },{"_index":"students","_type":"class1","_id":"2","_score":0.11506981,"_source":
  {
  "name" : "Guo Jing",
  "gender" : "Male",
  "age" : 25,
  "class" : "Gai Bang"
   }
  }]}}
  

  # curl -XPUT 'localhost:9200/students/class1/3?pretty' -d '
  {
  "name" : "Huang Rong",
  "gender" : "Female",
  "age" : 22,
  "class" : "Guo Xiaotian"
   }
  '
  {
  "_index" : "students",
  "_type" : "class1",
  "_id" : "3",
  "_version" : 1,
  "created" : true
  }
  

  # curl -XGET 'localhost:9200/students/_search?q="Guo"'
  {"took":16,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":3,"max_score":0.11506981,"hits":[{"_index":"students","_type":"class1","_id":"1","_score":0.11506981,"_source":
  {
  "name" : "Yang Guo",
  "gender" : "Male",
  "age" : 17,
  "class" : "Gmu Pai"
   }
  },{"_index":"students","_type":"class1","_id":"2","_score":0.11506981,"_source":
  {
  "name" : "Guo Jing",
  "gender" : "Male",
  "age" : 25,
  "class" : "Gai Bang"
   }
  },{"_index":"students","_type":"class1","_id":"3","_score":0.11506981,"_source":
  {
  "name" : "Huang Rong",
  "gender" : "Female",
  "age" : 22,
  "class" : "Guo Xiaotian"
   }
  }]}}
  

  # curl -XGET 'localhost:9200/students/_search?q="Guo%20Jing"'
  {"took":24,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":1,"max_score":0.23013961,"hits":[{"_index":"students","_type":"class1","_id":"2","_score":0.23013961,"_source":
  {
  "name" : "Guo Jing",
  "gender" : "Male",
  "age" : 25,
  "class" : "Gai Bang"
   }
  }]}}
  

  # curl -XGET 'localhost:9200/students/_search?q=name:"Guo"'
  {"took":18,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":2,"max_score":0.19178301,"hits":[{"_index":"students","_type":"class1","_id":"1","_score":0.19178301,"_source":
  {
  "name" : "Yang Guo",
  "gender" : "Male",
  "age" : 17,
  "class" : "Gmu Pai"
   }
  },{"_index":"students","_type":"class1","_id":"2","_score":0.19178301,"_source":
  {
  "name" : "Guo Jing",
  "gender" : "Male",
  "age" : 25,
  "class" : "Gai Bang"
   }
  }]}}
  

  # curl -XGET 'localhost:9200/students/class1/4?pretty' -d '
  > {
  >   "name" : "GuoXiang",
  >   "gender" : "Female",
  >   "age" : 10,
  >   "class" : "Emei Pai"
  > }'
  {
  "_index" : "students",
  "_type" : "class1",
  "_id" : "4",
  "found" : false
  }
  

  # curl -XGET 'localhost:9200/students/_search?q=name:"Guo"'
  {"took":23,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":2,"max_score":0.19178301,"hits":[{"_index":"students","_type":"class1","_id":"1","_score":0.19178301,"_source":
  {
  "name" : "Yang Guo",
  "gender" : "Male",
  "age" : 17,
  "class" : "Gmu Pai"
   }
  },{"_index":"students","_type":"class1","_id":"2","_score":0.19178301,"_source":
  {
  "name" : "Guo Jing",
  "gender" : "Male",
  "age" : 25,
  "class" : "Gai Bang"
   }
  }]}}
  

  # curl -XGET 'localhost:9200/students/_search?q=name:"Guo%20Jing"'
  {"took":16,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":1,"max_score":0.38356602,"hits":[{"_index":"students","_type":"class1","_id":"2","_score":0.38356602,"_source":
  {
  "name" : "Guo Jing",
  "gender" : "Male",
  "age" : 25,
  "class" : "Gai Bang"
   }
  }]}}
  

  # curl -XGET 'localhost:9200/students/_search?q=25'
  {"took":14,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":1,"max_score":0.11506981,"hits":[{"_index":"students","_type":"class1","_id":"2","_score":0.11506981,"_source":
  {
  "name" : "Guo Jing",
  "gender" : "Male",
  "age" : 25,
  "class" : "Gai Bang"
   }
  }]}}
  

  # curl -XPUT 'localhost:9200/students/class1/5' -d '
  > {
  >   "name" : "Xiaolong nv",
  >   "gender" : "Female",
  >   "age" : 18,
  >   "description" : " one of 25 "
  > }'
  {"_index":"students","_type":"class1","_id":"5","_version":1,"created":true}
  

  # curl -XGET 'localhost:9200/students/_search?q=25'
  {"took":13,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":2,"max_score":0.11506981,"hits":[{"_index":"students","_type":"class1","_id":"5","_score":0.11506981,"_source":
  {
  "name" : "Xiaolong nv",
  "gender" : "Female",
  "age" : 18,
  "description" : " one of 25 "
  }},{"_index":"students","_type":"class1","_id":"2","_score":0.11506981,"_source":
  {
  "name" : "Guo Jing",
  "gender" : "Male",
  "age" : 25,
  "class" : "Gai Bang"
   }
  }]}}
  

  # curl 'localhost:9200/students/_mapping/class1?pretty'
  {
  "students" : {
      "mappings" : {
        "class1" : {
        "properties" : {
            "age" : {
              "type" : "long"
            },
            "class" : {
              "type" : "string"
            },
            "courses" : {
              "type" : "string"
            },
            "description" : {
              "type" : "string"
            },
            "first_name" : {
              "type" : "string"
            },
            "gender" : {
              "type" : "string"
            },
            "last_name" : {
              "type" : "string"
            },
            "name" : {
              "type" : "string"
            }
        }
        }
      }
  }
  }
  

  04ES查询及Logstash入门
  

  # curl -XGET 'localhost:9200/students/_search?pretty' -d '
  {
  "query": {
  >   "terms": {
  >       "age":
  >       }
  >   }
  > }'
  {
  "took" : 34,
  "timed_out" : false,
  "_shards" : {
      "total" : 5,
      "successful" : 5,
      "failed" : 0
  },
  "hits" : {
      "total" : 3,
      "max_score" : 0.125,
      "hits" : [ {
        "_index" : "students",
        "_type" : "class1",
        "_id" : "5",
        "_score" : 0.125,
        "_source":
  {
  "name" : "Xiaolong nv",
  "gender" : "Female",
  "age" : 18,
  "description" : " one of 25 "
  }
      }, {
        "_index" : "students",
        "_type" : "class1",
        "_id" : "2",
        "_score" : 0.125,
        "_source":
  {
  "name" : "Guo Jing",
  "gender" : "Male",
  "age" : 25,
  "class" : "Gai Bang"
   }
  

      }, {
        "_index" : "students",
        "_type" : "class1",
        "_id" : "3",
        "_score" : 0.125,
        "_source":
  {
  "name" : "Huang Rong",
  "gender" : "Female",
  "age" : 22,
  "class" : "Guo Xiaotian"
   }
  

      } ]
  }
  }
  

  # curl -XGET 'localhost:9200/students/_validate/query?pretty' -d '
  > {
  >   "query": {
  >   "filterd": {
  >      "filter": { "term": { "name": "Guo" } }
  >   }
  >   }
  > }'
  {
  "valid" : false,
  "_shards" : {
      "total" : 1,
      "successful" : 1,
      "failed" : 0
  }
  }
  

  # curl -XGET 'localhost:9200/students/_validate/query?pretty' -d'
  > {
  >   "query": {
  >   "term": { "age": 25 }
  >   }
  > }'
  {
  "valid" : true,
  "_shards" : {
      "total" : 1,
      "successful" : 1,
      "failed" : 0
  }
  }
  

  # curl -XGET 'localhost:9200/students/_validate/query?explain&pretty' -d'
  {
  "query": {
      "term": { "age": 25 }
  }
  }'
  {
  "valid" : true,
  "_shards" : {
      "total" : 1,
      "successful" : 1,
      "failed" : 0
  },
  "explanations" : [ {
      "index" : "students",
      "valid" : true,
      "explanation" : "ConstantScore(age: \u0001\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0019)"
  } ]
  }
  

  # curl -XGET 'localhost:9200/students/_validate/query?explain&pretty' -d'{y' -d'
  "query": {
      "term": { "name": "Guo" }
  } "term": { "name": "Guo" }
  }'}
  {
  "valid" : true,
  "_shards" : {
      "total" : 1,
      "successful" : 1,
      "failed" : 0
  },
  "explanations" : [ {
      "index" : "students",
      "valid" : true,
      "explanation" : "name:Guo"
  } ]
  }
  

  # curl -XGET 'localhost:9200/students/_search?pretty' -d'         {      
  "query": {
      "term": { "name": "Guo" }
  }                        
  }'
  {
  "took" : 9,
  "timed_out" : false,
  "_shards" : {
      "total" : 5,
      "successful" : 5,
      "failed" : 0
  },
  "hits" : {
      "total" : 0,
      "max_score" : null,
      "hits" : [ ]
  }
  }
  

  

  # yum makecache
  # vim /etc/profile.d/java.sh
  添加:
  export JAVA_HOME=/usr
  # yum -y install logstash-1.5.4-1.noarch.rpm
  # vim /etc/profile.d/logstash.sh
  export PATH=/opt/logstash/bin:$PATH
  # . /etc/profile.d/logstash.sh
  # cd /etc/logstash/conf.d/
  input {
  stdin {}
  }
  

  output {
  stdout {
  codec   => rubydebug
  }
  }
  # logstash -f sample.conf --configtest
  Configuration OK
  # logstash -f sample.conf
  Logstash startup completed
  Hello Logstash
  {
         "message" => "Hello Logstash",
        "@version" => "1",
      "@timestamp" => "2017-01-02T13:25:21.779Z",
            "host" => "node4"
  }
  




页: [1]
查看完整版本: 54 搜索引擎及Lucene基础、elasticsearch使用基础、ES使用详解、ES查询及Logstash入门