博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
elasticsearch 基础入门
阅读量:4229 次
发布时间:2019-05-26

本文共 56143 字,大约阅读时间需要 187 分钟。

环境和版本

环境:centOS7 虚拟机

版本:elasticsearch-7.3.1-linux-x86_64.tar.gz

Java 版本:Java11(我原来是Java8,用 yum 升级一下 -- 卸载,下载 )

  • openjdk version "11.0.8" 2020-07-14 LTS
  • OpenJDK Runtime Environment 18.9 (build 11.0.8+10-LTS)
  • OpenJDK 64-Bit Server VM 18.9 (build 11.0.8+10-LTS, mixed mode, sharing)

配置

elasticsearch 默认是只允许本机 localhost 访问的,如果你想在你机子谷歌浏览器上也可以访问,可以按照如下设置:

vim ./config/elasticsearch.yml ,添加以下几行(192.168.128.139 修改为你虚拟机自己设置的静态 IP):

# 192.168.128.139 写你虚拟机自己设置的静态 IP[looking@master elasticsearch-7.3.1]$ vim ./config/elasticsearch.ymlnetwork.host: 192.168.128.139discovery.seed_hosts: ["192.168.128.139"]cluster.initial_master_nodes: ["es-01"]node.max_local_storage_nodes: 10

如果elasticsearch启动报错: Native controller process has stopped - no new native processes can be started 

报错配置

解决办法(在 root 用户下,不然可能没有权限修改的):

[root@master ~]# vim /etc/security/limits.conf # 在文件末尾添加以下几行, looking 修改为你要运行 elasticsearch 的用户名looking soft nofile 65536looking hard nofile 65536looking soft nproc 4096looking hard nproc 4096
[root@master ~]# cd /etc/security/limits.d[root@master limits.d]# lltotal 4-rw-r--r--. 1 root root 197 Aug 23 22:13 20-nproc.conf[root@master limits.d]# vi 20-nproc.conf # looking 修改为你运行 elasticsearch 的用户名# Default limit for number of user's processes to prevent# accidental fork bombs.# See rhbz #432903 for reasoning.looking          soft    nproc     4096root       soft    nproc     unlimited
[root@master limits.d]# vi /etc/sysctl.conf # sysctl settings are defined through files in# /usr/lib/sysctl.d/, /run/sysctl.d/, and /etc/sysctl.d/.## Vendors settings live in /usr/lib/sysctl.d/.# To override a whole file, create a new file with the same in# /etc/sysctl.d/ and put new settings there. To override# only specific settings, add a file with a lexically later# name in /etc/sysctl.d/ and put new settings there.## For more information, see sysctl.conf(5) and sysctl.d(5).vm.max_map_count = 655360

 ​​​​​修改完以后重启一下虚拟机吧(我之前也按照上面这样修改的,但是启动 ./bin/elasticsearch 的时候还是报错,重启一下不知道怎么就好了,反正重启一下花不了多长时间)

再次启动 

[looking@master elasticsearch-7.3.1]$ ./bin/elasticsearchOpenJDK 64-Bit Server VM warning: Option UseConcMarkSweepGC was deprecated in version 9.0 and will likely be removed in a future release.[2020-08-23T22:41:12,202][INFO ][o.e.e.NodeEnvironment    ] [master] using [1] data paths, mounts [[/ (rootfs)]], net usable_space [11.9gb], net total_space [26.8gb], types [rootfs][2020-08-23T22:41:12,204][INFO ][o.e.e.NodeEnvironment    ] [master] heap size [990.7mb], compressed ordinary object pointers [true][2020-08-23T22:41:12,232][INFO ][o.e.n.Node               ] [master] node name [master], node ID [hmeiFSEDRZK4hY0jQ1eV7Q], cluster name [elasticsearch][2020-08-23T22:41:12,233][INFO ][o.e.n.Node               ] [master] version[7.3.1], pid[2195], build[default/tar/4749ba6/2019-08-19T20:19:25.651794Z], OS[Linux/3.10.0-957.27.2.el7.x86_64/amd64], JVM[Oracle Corporation/OpenJDK 64-Bit Server VM/11.0.8/11.0.8+10-LTS][2020-08-23T22:41:12,234][INFO ][o.e.n.Node               ] [master] JVM home [/usr/lib/jvm/java-11-openjdk-11.0.8.10-0.el7_8.x86_64][2020-08-23T22:41:12,234][INFO ][o.e.n.Node               ] [master] JVM arguments [-Xms1g, -Xmx1g, -XX:+UseConcMarkSweepGC, -XX:CMSInitiatingOccupancyFraction=75, -XX:+UseCMSInitiatingOccupancyOnly, -Des.networkaddress.cache.ttl=60, -Des.networkaddress.cache.negative.ttl=10, -XX:+AlwaysPreTouch, -Xss1m, -Djava.awt.headless=true, -Dfile.encoding=UTF-8, -Djna.nosys=true, -XX:-OmitStackTraceInFastThrow, -Dio.netty.noUnsafe=true, -Dio.netty.noKeySetOptimization=true, -Dio.netty.recycler.maxCapacityPerThread=0, -Dlog4j.shutdownHookEnabled=false, -Dlog4j2.disable.jmx=true, -Djava.io.tmpdir=/tmp/elasticsearch-14712143831274494769, -XX:+HeapDumpOnOutOfMemoryError, -XX:HeapDumpPath=data, -XX:ErrorFile=logs/hs_err_pid%p.log, -Xlog:gc*,gc+age=trace,safepoint:file=logs/gc.log:utctime,pid,tags:filecount=32,filesize=64m, -Djava.locale.providers=COMPAT, -Dio.netty.allocator.type=unpooled, -XX:MaxDirectMemorySize=536870912, -Des.path.home=/home/looking/packages/elasticsearch-7.3.1, -Des.path.conf=/home/looking/packages/elasticsearch-7.3.1/config, -Des.distribution.flavor=default, -Des.distribution.type=tar, -Des.bundled_jdk=true]

测试结果

[looking@master ~]$ curl 'http://192.168.128.139:9200/?pretty'{  "name" : "master",  "cluster_name" : "elasticsearch",  "cluster_uuid" : "i1IqdSqXSySIkr5oiUUbzg",  "version" : {    "number" : "7.3.1",    "build_flavor" : "default",    "build_type" : "tar",    "build_hash" : "4749ba6",    "build_date" : "2019-08-19T20:19:25.651794Z",    "build_snapshot" : false,    "lucene_version" : "8.1.0",    "minimum_wire_compatibility_version" : "6.8.0",    "minimum_index_compatibility_version" : "6.0.0-beta1"  },  "tagline" : "You Know, for Search"}

浏览器访问

pretty 就是让输出好看一点,其实就是 pretty print。

Hello World

安装过程还行,其实就是解压运行就好了,关键是不能在 root 下运行倒是有点让人一脸懵逼(可能是怕权限太高,万一出了岔子要背锅)。所以我就创建了一个普通用户,把解压的包 cp 到 looking 用户目录并给与相应的权限。

[root@master packages]# cp -r elasticsearch-7.3.1 /home/looking/packages/[root@master packages]# cd /home/looking/packages/[root@master packages]# chown -R looking:looking elasticsearch-7.3.1/
# terminal1[looking@master packages]$ lltotal 0drwxr-xr-x. 10 looking looking 166 Aug 22 22:13 elasticsearch-7.3.1[looking@master packages]$ cd elasticsearch-7.3.1/# 跑起来[looking@master elasticsearch-7.3.1]$ ./bin/elasticsearchOpenJDK 64-Bit Server VM warning: Option UseConcMarkSweepGC was deprecated in version 9.0 and will likely be removed in a future release....]:9200}, {127.0.0.1:9200}[2020-08-22T22:25:00,031][INFO ][o.e.n.Node               ] [master] started[2020-08-22T22:25:00,333][INFO ][o.e.l.LicenseService     ] [master] license [be635ca4-e571-4eba-8268-7864a65e4157] mode [basic] - valid[2020-08-22T22:25:00,336][INFO ][o.e.x.s.s.SecurityStatusChangeListener] [master] Active license is now [BASIC]; Security is disabled[2020-08-22T22:25:00,369][INFO ][o.e.g.GatewayService     ] [master] recovered [0] indices into cluster_state
# # terminal12[looking@master ~]$ curl 'http://localhost:9200/?pretty'{  "name" : "master",  "cluster_name" : "elasticsearch",  "cluster_uuid" : "i1IqdSqXSySIkr5oiUUbzg",  "version" : {    "number" : "7.3.1",    "build_flavor" : "default",    "build_type" : "tar",    "build_hash" : "4749ba6",    "build_date" : "2019-08-19T20:19:25.651794Z",    "build_snapshot" : false,    "lucene_version" : "8.1.0",    "minimum_wire_compatibility_version" : "6.8.0",    "minimum_index_compatibility_version" : "6.0.0-beta1"  },  "tagline" : "You Know, for Search"}

常用操作

查看所有索引

[looking@master ~]$ curl localhost:9200/_cat/indices?vhealth status index    uuid                   pri rep docs.count docs.deleted store.size pri.store.sizeyellow open   website  svdjlzy2TfCQFLHcKWhjRw   1   1          2            0      8.9kb          8.9kbyellow open   bank     E-6sCm3iRRqSweO9L5nWow   1   1       1000            0    414.3kb        414.3kbyellow open   megacorp rzaxRxNTRTyF3YsKlrjxxA   1   1          2            0      6.4kb          6.4kbyellow open   blogs    qfnun_91RI2O1lgTjnBmCQ   3   1          0            0       849b           849b

 查看索引结构

[looking@master ~]$ curl localhost:9200/megacorp/_mapping?pretty{  "megacorp" : {    "mappings" : {      "properties" : {        "about" : {          "type" : "text",          "fields" : {            "keyword" : {              "type" : "keyword",              "ignore_above" : 256            }          }        },        "age" : {          "type" : "long"        },        "first_name" : {          "type" : "text",          "fields" : {            "keyword" : {              "type" : "keyword",              "ignore_above" : 256            }          }        },        "interests" : {          "type" : "text",          "fields" : {            "keyword" : {              "type" : "keyword",              "ignore_above" : 256            }          },          "fielddata" : true        },        "last_name" : {          "type" : "text",          "fields" : {            "keyword" : {              "type" : "keyword",              "ignore_above" : 256            }          }        },        "properties" : {          "properties" : {            "interests" : {              "properties" : {                "fielddata" : {                  "type" : "boolean"                },                "type" : {                  "type" : "text",                  "fields" : {                    "keyword" : {                      "type" : "keyword",                      "ignore_above" : 256                    }                  }                }              }            }          }        }      }    }  }}

查看索引别名

[looking@master ~]$ curl localhost:9200/_alias?pretty{  "website" : {    "aliases" : { }  },  "bank" : {    "aliases" : {      "banks" : { }    }  },  "megacorp" : {    "aliases" : { }  },  "blogs" : {    "aliases" : { }  }}------------------------------------------------------[looking@master ~]$ curl localhost:9200/bank/_alias?pretty{  "bank" : {    "aliases" : {      "banks" : { }    }  }}

添加索引别名

[looking@master ~]$ curl -XPOST 'localhost:9200/_aliases?pretty' -H 'Content-Type: application/json' -d '{    "actions": [        {"add": {"index": "bank", "alias": "banks"}}    ]}'{  "acknowledged" : true}

删除索引别名

[looking@master ~]$ curl -XPOST 'localhost:9200/_aliases?pretty' -H 'Content-Type: application/json' -d '> {>   "actions": [>       {"remove": {"index": "bank", "alias": "banks"}}>   ]> }> '{  "acknowledged" : true}

修改索引别名

[looking@master ~]$ curl localhost:9200/bank/_alias?pretty{  "bank" : {    "aliases" : {      "banks" : { }    }  }}[looking@master ~]$ curl -XPOST 'localhost:9200/_aliases?pretty' -H 'Content-Type: application/json' -d '> {>   "actions": [>       {"remove": {"index": "bank", "alias": "banks"}},>       {"add": {"index": "bank", "alias": "bank_bak"}}>   ]> }> '{  "acknowledged" : true}[looking@master ~]$ curl localhost:9200/bank/_alias?pretty{  "bank" : {    "aliases" : {      "bank_bak" : { }    }  }}

查询索引数据(全量数据)

[looking@master ~]$ curl -XGET localhost:9200/megacorp/employee/_search?pretty{  "took" : 3,  "timed_out" : false,  "_shards" : {    "total" : 1,    "successful" : 1,    "skipped" : 0,    "failed" : 0  },  "hits" : {    "total" : {      "value" : 2,      "relation" : "eq"    },    "max_score" : 1.0,    "hits" : [      {        "_index" : "megacorp",        "_type" : "employee",        "_id" : "1",        "_score" : 1.0,        "_source" : {          "first_name" : "Looking",          "last_name" : "Smith",          "age" : 25,          "about" : "I love to go rock climbing",          "interests" : [            "sports",            "music"          ]        }      },      {        "_index" : "megacorp",        "_type" : "employee",        "_id" : "3",        "_score" : 1.0,        "_source" : {          "first_name" : "Douglas",          "last_name" : "Fir",          "age" : 35,          "about" : "I like to build cabinets",          "interests" : [            "forestry"          ]        }      }    ]  }}

删除指定索引

[looking@master ~]$ curl -XDELETE localhost:9200/megacorp?pretty{  "acknowledged" : true}

查询集群文档数目

[looking@master ~]$ curl -X GET "localhost:9200/_count?pretty"{  "count" : 1005,  "_shards" : {    "total" : 6,    "successful" : 6,    "skipped" : 0,    "failed" : 0  }}

批量插入数据(从文件)

[looking@master ~]$ curl -XPOST 'localhost:9200/bank/account/_bulk?pretty' -H 'Content-Type: application/json' --data-binary "@accounts.json" ...    {      "index" : {        "_index" : "bank",        "_type" : "account",        "_id" : "990",        "_version" : 1,        "result" : "created",        "_shards" : {          "total" : 2,          "successful" : 1,          "failed" : 0        },        "_seq_no" : 998,        "_primary_term" : 1,        "status" : 201      }    },    {      "index" : {        "_index" : "bank",        "_type" : "account",        "_id" : "995",        "_version" : 1,        "result" : "created",        "_shards" : {          "total" : 2,          "successful" : 1,          "failed" : 0        },        "_seq_no" : 999,        "_primary_term" : 1,        "status" : 201      }    }  ]}

索引文档(插入数据)

[looking@master packages]$ curl -X PUT "localhost:9200/megacorp/employee/1?pretty" -H 'Content-Type: application/json' -d'> {>     "first_name" : "John",>     "last_name" :  "Smith",>     "age" :        25,>     "about" :      "I love to go rock climbing",>     "interests": [ "sports", "music" ]> }> '{  "_index" : "megacorp",  "_type" : "employee",  "_id" : "1",  "_version" : 1,  "result" : "created",  "_shards" : {    "total" : 2,    "successful" : 1,    "failed" : 0  },  "_seq_no" : 0,  "_primary_term" : 1}[looking@master packages]$ curl -X PUT "localhost:9200/megacorp/employee/2?pretty" -H 'Content-Type: application/json' -d'> {>     "first_name" :  "Jane",>     "last_name" :   "Smith",>     "age" :         32,>     "about" :       "I like to collect rock albums",>     "interests":  [ "music" ]> }> '{  "_index" : "megacorp",  "_type" : "employee",  "_id" : "2",  "_version" : 1,  "result" : "created",  "_shards" : {    "total" : 2,    "successful" : 1,    "failed" : 0  },  "_seq_no" : 1,  "_primary_term" : 1}[looking@master packages]$ curl -X PUT "localhost:9200/megacorp/employee/3?pretty" -H 'Content-Type: application/json' -d'> {>     "first_name" :  "Douglas",>     "last_name" :   "Fir",>     "age" :         35,>     "about":        "I like to build cabinets",>     "interests":  [ "forestry" ]> }> '{  "_index" : "megacorp",  "_type" : "employee",  "_id" : "3",  "_version" : 1,  "result" : "created",  "_shards" : {    "total" : 2,    "successful" : 1,    "failed" : 0  },  "_seq_no" : 2,  "_primary_term" : 1}

检索文档(查询数据)

[looking@master packages]$ curl -X GET "localhost:9200/megacorp/employee/1?pretty"{  "_index" : "megacorp",  "_type" : "employee",  "_id" : "1",  "_version" : 1,  "_seq_no" : 0,  "_primary_term" : 1,  "found" : true,  "_source" : {    "first_name" : "John",    "last_name" : "Smith",    "age" : 25,    "about" : "I love to go rock climbing",    "interests" : [      "sports",      "music"    ]  }}

删除文档(删除数据)

[looking@master ~]$ curl -XDELETE localhost:9200/megacorp/employee/2?pretty{  "_index" : "megacorp",  "_type" : "employee",  "_id" : "2",  "_version" : 3,  "result" : "deleted",  "_shards" : {    "total" : 2,    "successful" : 1,    "failed" : 0  },  "_seq_no" : 7,  "_primary_term" : 11}------------------------------------------------------------[looking@master ~]$ curl -XGET localhost:9200/megacorp/employee/_search?pretty{  "took" : 1241,  "timed_out" : false,  "_shards" : {    "total" : 1,    "successful" : 1,    "skipped" : 0,    "failed" : 0  },  "hits" : {    "total" : {      "value" : 2,      "relation" : "eq"    },    "max_score" : 1.0,    "hits" : [      {        "_index" : "megacorp",        "_type" : "employee",        "_id" : "1",        "_score" : 1.0,        "_source" : {          "first_name" : "John",          "last_name" : "Smith",          "age" : 25,          "about" : "I love to go rock climbing",          "interests" : [            "sports",            "music"          ]        }      },      {        "_index" : "megacorp",        "_type" : "employee",        "_id" : "3",        "_score" : 1.0,        "_source" : {          "first_name" : "Douglas",          "last_name" : "Fir",          "age" : 35,          "about" : "I like to build cabinets",          "interests" : [            "forestry"          ]        }      }    ]  }}

更新文档(更新数据)

[looking@master ~]$ curl -XPOST 'localhost:9200/megacorp/employee/1/_update?pretty'  -H 'Content-Type: application/json' -d '{"doc":{  "first_name": "Looking"  }}'{  "_index" : "megacorp",  "_type" : "employee",  "_id" : "1",  "_version" : 4,  "result" : "updated",  "_shards" : {    "total" : 2,    "successful" : 1,    "failed" : 0  },  "_seq_no" : 8,  "_primary_term" : 11}------------------------------------------------------------[looking@master ~]$ curl -XGET localhost:9200/megacorp/employee/_search?pretty                                  {  "took" : 18,  "timed_out" : false,  "_shards" : {    "total" : 1,    "successful" : 1,    "skipped" : 0,    "failed" : 0  },  "hits" : {    "total" : {      "value" : 2,      "relation" : "eq"    },    "max_score" : 1.0,    "hits" : [      {        "_index" : "megacorp",        "_type" : "employee",        "_id" : "1",        "_score" : 1.0,        "_source" : {          "first_name" : "Looking",          "last_name" : "Smith",          "age" : 25,          "about" : "I love to go rock climbing",          "interests" : [            "sports",            "music"          ]        }      },      {        "_index" : "megacorp",        "_type" : "employee",        "_id" : "3",        "_score" : 1.0,        "_source" : {          "first_name" : "Douglas",          "last_name" : "Fir",          "age" : 35,          "about" : "I like to build cabinets",          "interests" : [            "forestry"          ]        }      }    ]  }}

全部搜索

[looking@master packages]$ curl -X GET "localhost:9200/megacorp/employee/_search?pretty"{  "took" : 549,  "timed_out" : false,  "_shards" : {    "total" : 1,    "successful" : 1,    "skipped" : 0,    "failed" : 0  },  "hits" : {    "total" : {      "value" : 3,      "relation" : "eq"    },    "max_score" : 1.0,    "hits" : [      {        "_index" : "megacorp",        "_type" : "employee",        "_id" : "1",        "_score" : 1.0,        "_source" : {          "first_name" : "John",          "last_name" : "Smith",          "age" : 25,          "about" : "I love to go rock climbing",          "interests" : [            "sports",            "music"          ]        }      },      {        "_index" : "megacorp",        "_type" : "employee",        "_id" : "2",        "_score" : 1.0,        "_source" : {          "first_name" : "Jane",          "last_name" : "Smith",          "age" : 32,          "about" : "I like to collect rock albums",          "interests" : [            "music"          ]        }      },      {        "_index" : "megacorp",        "_type" : "employee",        "_id" : "3",        "_score" : 1.0,        "_source" : {          "first_name" : "Douglas",          "last_name" : "Fir",          "age" : 35,          "about" : "I like to build cabinets",          "interests" : [            "forestry"          ]        }      }    ]  }}

条件搜索

[looking@master packages]$ curl -X GET "localhost:9200/megacorp/employee/_search?q=last_name:Smith&pretty"{  "took" : 178,  "timed_out" : false,  "_shards" : {    "total" : 1,    "successful" : 1,    "skipped" : 0,    "failed" : 0  },  "hits" : {    "total" : {      "value" : 2,      "relation" : "eq"    },    "max_score" : 0.47000363,    "hits" : [      {        "_index" : "megacorp",        "_type" : "employee",        "_id" : "1",        "_score" : 0.47000363,        "_source" : {          "first_name" : "John",          "last_name" : "Smith",          "age" : 25,          "about" : "I love to go rock climbing",          "interests" : [            "sports",            "music"          ]        }      },      {        "_index" : "megacorp",        "_type" : "employee",        "_id" : "2",        "_score" : 0.47000363,        "_source" : {          "first_name" : "Jane",          "last_name" : "Smith",          "age" : 32,          "about" : "I like to collect rock albums",          "interests" : [            "music"          ]        }      }    ]  }}

表达式搜索

[looking@master packages]$ curl -X GET "localhost:9200/megacorp/employee/_search?pretty" -H 'Content-Type: application/json' -d'{    "query" : {        "match" : {            "last_name" : "Smith"        }    }}'# 上边语句的执行返回结果和下面是一样的[looking@master packages]$ curl -X GET "localhost:9200/megacorp/employee/_search?q=last_name:Smith&pretty"{  "took" : 8,  "timed_out" : false,  "_shards" : {    "total" : 1,    "successful" : 1,    "skipped" : 0,    "failed" : 0  },  "hits" : {    "total" : {      "value" : 2,      "relation" : "eq"    },    "max_score" : 0.47000363,    "hits" : [      {        "_index" : "megacorp",        "_type" : "employee",        "_id" : "1",        "_score" : 0.47000363,        "_source" : {          "first_name" : "John",          "last_name" : "Smith",          "age" : 25,          "about" : "I love to go rock climbing",          "interests" : [            "sports",            "music"          ]        }      },      {        "_index" : "megacorp",        "_type" : "employee",        "_id" : "2",        "_score" : 0.47000363,        "_source" : {          "first_name" : "Jane",          "last_name" : "Smith",          "age" : 32,          "about" : "I like to collect rock albums",          "interests" : [            "music"          ]        }      }    ]  }}

复杂搜索

[looking@master workspace]$ curl -X GET "localhost:9200/megacorp/employee/_search?pretty" -H 'Content-Type: application/json' -d'> {>     "query" : {>         "bool": {>             "must": {>                 "match" : {>                     "last_name" : "smith" >                 }>             },>             "filter": {>                 "range" : {>                     "age" : { "gt" : 30 } >                 }>             }>         }>     }> }> '{  "took" : 11,  "timed_out" : false,  "_shards" : {    "total" : 1,    "successful" : 1,    "skipped" : 0,    "failed" : 0  },  "hits" : {    "total" : {      "value" : 1,      "relation" : "eq"    },    "max_score" : 0.47000363,    "hits" : [      {        "_index" : "megacorp",        "_type" : "employee",        "_id" : "2",        "_score" : 0.47000363,        "_source" : {          "first_name" : "Jane",          "last_name" : "Smith",          "age" : 32,          "about" : "I like to collect rock albums",          "interests" : [            "music"          ]        }      }    ]  }}

全文搜索

Elasticsearch 默认按照相关性得分排序,即每个文档跟查询的匹配程度。第一个最高得分的结果很明显:John Smith 的 about 属性清楚地写着 “rock climbing” 。但为什么 Jane Smith 也作为结果返回了呢?原因是她的 about 属性里提到了 “rock” 。因为只有 “rock” 而没有 “climbing” ,所以它的相关性得分低于 John 的。

[looking@master workspace]$ curl -X GET "localhost:9200/megacorp/employee/_search?pretty" -H 'Content-Type: application/json' -d'> {>     "query" : {>         "match" : {>             "about" : "rock climbing">         }>     }> }> '{  "took" : 104,  "timed_out" : false,  "_shards" : {    "total" : 1,    "successful" : 1,    "skipped" : 0,    "failed" : 0  },  "hits" : {    "total" : {      "value" : 2,      "relation" : "eq"    },    "max_score" : 1.4167402,    "hits" : [      {        "_index" : "megacorp",        "_type" : "employee",        "_id" : "1",        "_score" : 1.4167402,        "_source" : {          "first_name" : "John",          "last_name" : "Smith",          "age" : 25,          "about" : "I love to go rock climbing",          "interests" : [            "sports",            "music"          ]        }      },      {        "_index" : "megacorp",        "_type" : "employee",        "_id" : "2",        "_score" : 0.45895916,        "_source" : {          "first_name" : "Jane",          "last_name" : "Smith",          "age" : 32,          "about" : "I like to collect rock albums",          "interests" : [            "music"          ]        }      }    ]  }}

短语搜索

[looking@master workspace]$ curl -X GET "localhost:9200/megacorp/employee/_search?pretty" -H 'Content-Type: application/json' -d'> {>     "query" : {>         "match_phrase" : {>             "about" : "rock climbing">         }>     }> }> '{  "took" : 47,  "timed_out" : false,  "_shards" : {    "total" : 1,    "successful" : 1,    "skipped" : 0,    "failed" : 0  },  "hits" : {    "total" : {      "value" : 1,      "relation" : "eq"    },    "max_score" : 1.4167402,    "hits" : [      {        "_index" : "megacorp",        "_type" : "employee",        "_id" : "1",        "_score" : 1.4167402,        "_source" : {          "first_name" : "John",          "last_name" : "Smith",          "age" : 25,          "about" : "I love to go rock climbing",          "interests" : [            "sports",            "music"          ]        }      }    ]  }}

高亮搜索

[looking@master workspace]$ curl -X GET "localhost:9200/megacorp/employee/_search?pretty" -H 'Content-Type: application/json' -d'> {>     "query" : {>         "match_phrase" : {>             "about" : "rock climbing">         }>     },>     "highlight": {>         "fields" : {>             "about" : {}>         }>     }> }> '{  "took" : 31,  "timed_out" : false,  "_shards" : {    "total" : 1,    "successful" : 1,    "skipped" : 0,    "failed" : 0  },  "hits" : {    "total" : {      "value" : 1,      "relation" : "eq"    },    "max_score" : 1.4167402,    "hits" : [      {        "_index" : "megacorp",        "_type" : "employee",        "_id" : "1",        "_score" : 1.4167402,        "_source" : {          "first_name" : "John",          "last_name" : "Smith",          "age" : 25,          "about" : "I love to go rock climbing",          "interests" : [            "sports",            "music"          ]        },        "highlight" : {          "about" : [            "I love to go rock climbing"          ]        }      }    ]  }}

集群

集群健康

[looking@master workspace]$ curl -X GET "localhost:9200/_cluster/health?pretty"{  "cluster_name" : "elasticsearch",  "status" : "yellow",  "timed_out" : false,  "number_of_nodes" : 1,  "number_of_data_nodes" : 1,  "active_primary_shards" : 1,  "active_shards" : 1,  "relocating_shards" : 0,  "initializing_shards" : 0,  "unassigned_shards" : 1,  "delayed_unassigned_shards" : 0,  "number_of_pending_tasks" : 0,  "number_of_in_flight_fetch" : 0,  "task_max_waiting_in_queue_millis" : 0,  "active_shards_percent_as_number" : 50.0}

添加索引

[looking@master ~]$ curl -X PUT "localhost:9200/blogs?pretty" -H 'Content-Type: application/json' -d'> {>    "settings" : {>       "number_of_shards" : 3,>       "number_of_replicas" : 1>    }> }> '{  "acknowledged" : true,  "shards_acknowledged" : true,  "index" : "blogs"}
[looking@master ~]$ curl -X GET "localhost:9200/_cluster/health?pretty"{  "cluster_name" : "elasticsearch",  "status" : "yellow",  "timed_out" : false,  "number_of_nodes" : 1,  "number_of_data_nodes" : 1,  "active_primary_shards" : 4,  "active_shards" : 4,  "relocating_shards" : 0,  "initializing_shards" : 0,  "unassigned_shards" : 4,  "delayed_unassigned_shards" : 0,  "number_of_pending_tasks" : 0,  "number_of_in_flight_fetch" : 0,  "task_max_waiting_in_queue_millis" : 0,  "active_shards_percent_as_number" : 50.0}

故障转移

当集群中只有一个节点在运行时,意味着会有一个单点故障问题,我们只需再启动一个节点即可防止数据丢失。要开启多个节点的话记得先在 config 目录下的 elasticsearch.yml 配置中加 node.max_local_storage_nodes: 10,不然无法启动多个节点。

数据输入输出

文档元数据

一个文档不仅仅包含它的数据 ,也包含 元数据 —— 有关 文档的信息。 三个必须的元数据元素如下:

_index(数据库)

一个 索引 应该是因共同的特性被分组到一起的文档集合。 例如,你可能存储所有的产品在索引 products 中,而存储所有销售的交易到索引 sales 中。 虽然也允许存储不相关的数据到一个索引中,但这通常看作是一个反模式的做法。

实际上,在 Elasticsearch 中,我们的数据是被存储和索引在 分片 中,而一个索引仅仅是逻辑上的命名空间, 这个命名空间由一个或者多个分片组合在一起。 然而,这是一个内部细节,我们的应用程序根本不应该关心分片,对于应用程序而言,只需知道文档位于一个 索引 内。 Elasticsearch 会处理所有的细节。

我们将在  介绍如何自行创建和管理索引,但现在我们将让 Elasticsearch 帮我们创建索引。 所有需要我们做的就是选择一个索引名,这个名字必须小写,不能以下划线开头,不能包含逗号。我们用 website 作为索引名举例。

_type(数据表)

数据可能在索引中只是松散的组合在一起,但是通常明确定义一些数据中的子分区是很有用的。 例如,所有的产品都放在一个索引中,但是你有许多不同的产品类别,比如 "electronics" 、 "kitchen" 和 "lawn-care"。这些文档共享一种相同的(或非常相似)的模式:他们有一个标题、描述、产品代码和价格。他们只是正好属于“产品”下的一些子类。

Elasticsearch 公开了一个称为 types (类型)的特性,它允许您在索引中对数据进行逻辑分区。不同 types 的文档可能有不同的字段,但最好能够非常相似。 我们将在  中更多的讨论关于 types 的一些应用和限制。

_id(数据记录)

ID 是一个字符串,当它和 _index 以及 _type 组合就可以唯一确定 Elasticsearch 中的一个文档。 当你创建一个新的文档,要么提供自己的 _id ,要么让 Elasticsearch 帮你生成。

索引文档

自定义ID

如果你的文档有一个自然的标识符 (例如,一个 user_account 字段或其他标识文档的值),你应该使用如下方式的 index API 并提供你自己 _id :

# [looking@master ~]$ curl -X PUT "localhost:9200/blogs?pretty" -H 'Content-Type: application/json' -dPUT /{index}/{type}/{id}{  "field": "value",  ...}
curl -X PUT "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"}'------------------------------------------------------------[looking@master ~]$ curl -X PUT "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"> }> '{  "_index" : "website",  "_type" : "blog",  "_id" : "123",  "_version" : 1,  "result" : "created",  "_shards" : {    "total" : 2,    "successful" : 1,    "failed" : 0  },  "_seq_no" : 0,  "_primary_term" : 1}

该响应表明文档已经成功创建,该索引包括 _index 、 _type 和 _id 元数据, 以及一个新元素: _version 。在 Elasticsearch 中每个文档都有一个版本号。当每次对文档进行修改时(包括删除), _version 的值会递增。 在  中,我们讨论了怎样使用 _version 号码确保你的应用程序中的一部分修改不会覆盖另一部分所做的修改。

Autogenerating IDs

如果你的数据没有自然的 ID, Elasticsearch 可以帮我们自动生成 ID 。 请求的结构调整为: 不再使用 PUT 谓词(“使用这个 URL 存储这个文档”), 而是使用 POST 谓词(“存储文档在这个 URL 命名空间下”)。现在该 URL 只需包含 _index 和 _type :

[looking@master ~]$ curl -X POST "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"> }> '{  "_index" : "website",  "_type" : "blog",  "_id" : "ynqLG3QBC54xDiNbftAq",  "_version" : 1,  "result" : "created",  "_shards" : {    "total" : 2,    "successful" : 1,    "failed" : 0  },  "_seq_no" : 1,  "_primary_term" : 1}

自动生成的 ID 是 URL-safe、 基于 Base64 编码且长度为20个字符的 GUID 字符串。 这些 GUID 字符串由可修改的 FlakeID 模式生成,这种模式允许多个节点并行生成唯一 ID ,且互相之间的冲突概率几乎为零。

查询文档

查询单个文档

[looking@master packages]$ curl -X GET "localhost:9200/website/blog/123?pretty"{  "_index" : "website",  "_type" : "blog",  "_id" : "123",  "_version" : 1,  "_seq_no" : 0,  "_primary_term" : 1,  "found" : true,  "_source" : {    "title" : "My first blog entry",    "text" : "Just trying this out...",    "date" : "2014/01/01"  }}

查询多个文档

[looking@master packages]$ curl -X GET "localhost:9200/website/blog/_search?pretty"{  "took" : 3,  "timed_out" : false,  "_shards" : {    "total" : 1,    "successful" : 1,    "skipped" : 0,    "failed" : 0  },  "hits" : {    "total" : {      "value" : 2,      "relation" : "eq"    },    "max_score" : 1.0,    "hits" : [      {        "_index" : "website",        "_type" : "blog",        "_id" : "123",        "_score" : 1.0,        "_source" : {          "title" : "My first blog entry",          "text" : "Just trying this out...",          "date" : "2014/01/01"        }      },      {        "_index" : "website",        "_type" : "blog",        "_id" : "ynqLG3QBC54xDiNbftAq",        "_score" : 1.0,        "_source" : {          "title" : "My second blog entry",          "text" : "Still trying this out...",          "date" : "2014/01/01"        }      }    ]  }}

显示相应头部

[looking@master packages]$ curl -i -XGET "localhost:9200/website/blog/124?pretty"HTTP/1.1 404 Not FoundWarning: 299 Elasticsearch-7.3.1-4749ba6 "[types removal] Specifying types in document get requests is deprecated, use the /{index}/_doc/{id} endpoint instead."content-type: application/json; charset=UTF-8content-length: 83{  "_index" : "website",  "_type" : "blog",  "_id" : "124",  "found" : false}

返回指定字段

[looking@master packages]$ curl -XGET "localhost:9200/website/blog/123?_source=title,text&pretty"{  "_index" : "website",  "_type" : "blog",  "_id" : "123",  "_version" : 1,  "_seq_no" : 0,  "_primary_term" : 1,  "found" : true,  "_source" : {    "text" : "Just trying this out...",    "title" : "My first blog entry"  }}

排除元数据(只返回真正存储的数据)

[looking@master packages]$ curl -XGET "localhost:9200/website/blog/123/_source?pretty"{  "title" : "My first blog entry",  "text" : "Just trying this out...",  "date" : "2014/01/01"}

文档存在性检查

[looking@master packages]$ curl -i -XHEAD "localhost:9200/website/blog/123"HTTP/1.1 200 OKWarning: 299 Elasticsearch-7.3.1-4749ba6 "[types removal] Specifying types in document get requests is deprecated, use the /{index}/_doc/{id} endpoint instead."content-type: application/json; charset=UTF-8content-length: 215------------------------------------------------------------[looking@master packages]$ curl -i -XHEAD "localhost:9200/website/blog/124"HTTP/1.1 404 Not FoundWarning: 299 Elasticsearch-7.3.1-4749ba6 "[types removal] Specifying types in document get requests is deprecated, use the /{index}/_doc/{id} endpoint instead."content-type: application/json; charset=UTF-8content-length: 61

更新整个文档

[looking@master packages]$ curl -XGET "localhost:9200/website/blog/123?pretty"{  "_index" : "website",  "_type" : "blog",  "_id" : "123",  "_version" : 1,  "_seq_no" : 0,  "_primary_term" : 1,  "found" : true,  "_source" : {    "title" : "My first blog entry",    "text" : "Just trying this out...",    "date" : "2014/01/01"  }}[looking@master packages]$ curl -X PUT "localhost:9200/website/blog/123?pretty" -H 'Content-Type: application/json' -d'> {>   "title": "My first blog entry",>   "text":  "I am starting to get the hang of this...",>   "date":  "2014/01/02"> }> '{  "_index" : "website",  "_type" : "blog",  "_id" : "123",  "_version" : 2,  "result" : "updated",  "_shards" : {    "total" : 2,    "successful" : 1,    "failed" : 0  },  "_seq_no" : 2,  "_primary_term" : 10}[looking@master packages]$ curl -XGET "localhost:9200/website/blog/123?pretty"{  "_index" : "website",  "_type" : "blog",  "_id" : "123",  "_version" : 2,  "_seq_no" : 2,  "_primary_term" : 10,  "found" : true,  "_source" : {    "title" : "My first blog entry",    "text" : "I am starting to get the hang of this...",    "date" : "2014/01/02"  }}

创建文档

当我们索引一个文档,怎么确认我们正在创建一个完全新的文档,而不是覆盖现有的呢?请记住, _index 、 _type 和 _id 的组合可以唯一标识一个文档。所以,确保创建一个新文档的最简单办法是,使用索引请求的 POST 形式让 Elasticsearch 自动生成唯一 _id :

curl -XPOST "localhost:9200/website/blog/" -H 'Content-Type: application/json' -d'{ ... }'

如果文档已存在,使用下边方法确保会报冲突错误而不是直接覆盖:

op_type

[looking@master packages]$ curl -X PUT "localhost:9200/website/blog/123?op_type=create&pretty" -H 'Content-Type: application/json' -d'> {>   "title": "My first blog entry",>   "text":  "Just trying this out...",>   "date":  "2014/01/01"> }> '{  "error" : {    "root_cause" : [      {        "type" : "version_conflict_engine_exception",        "reason" : "[123]: version conflict, document already exists (current version [2])",        "index_uuid" : "svdjlzy2TfCQFLHcKWhjRw",        "shard" : "0",        "index" : "website"      }    ],    "type" : "version_conflict_engine_exception",    "reason" : "[123]: version conflict, document already exists (current version [2])",    "index_uuid" : "svdjlzy2TfCQFLHcKWhjRw",    "shard" : "0",    "index" : "website"  },  "status" : 409}

/_create

[looking@master packages]$ curl -X PUT "localhost:9200/website/blog/123/_create?pretty" -H 'Content-Type: application/json' -d'> {>   "title": "My first blog entry",>   "text":  "Just trying this out...",>   "date":  "2014/01/01"> }> '{  "error" : {    "root_cause" : [      {        "type" : "version_conflict_engine_exception",        "reason" : "[123]: version conflict, document already exists (current version [2])",        "index_uuid" : "svdjlzy2TfCQFLHcKWhjRw",        "shard" : "0",        "index" : "website"      }    ],    "type" : "version_conflict_engine_exception",    "reason" : "[123]: version conflict, document already exists (current version [2])",    "index_uuid" : "svdjlzy2TfCQFLHcKWhjRw",    "shard" : "0",    "index" : "website"  },  "status" : 409}

删除文档

[looking@master packages]$ curl -X DELETE "localhost:9200/website/blog/123?pretty"{  "_index" : "website",  "_type" : "blog",  "_id" : "123",  "_version" : 3,  "result" : "deleted",  "_shards" : {    "total" : 2,    "successful" : 1,    "failed" : 0  },  "_seq_no" : 3,  "_primary_term" : 10}------------------------------------------------------------[looking@master packages]$ curl -X DELETE "localhost:9200/website/blog/123?pretty"{  "_index" : "website",  "_type" : "blog",  "_id" : "123",  "_version" : 1,  "result" : "not_found",  "_shards" : {    "total" : 2,    "successful" : 1,    "failed" : 0  },  "_seq_no" : 4,  "_primary_term" : 10}

并发控制

可以利用 _version 号来确保 应用中相互冲突的变更不会导致数据丢失。我们通过指定想要修改文档的 version 号来达到这个目的。 如果该版本不是当前版本号,我们的请求将会失败。

创建新的博客

[looking@master packages]$ curl -X PUT "localhost:9200/website/blog/1/_create?pretty" -H 'Content-Type: application/json' -d'> {>   "title": "My first blog entry",>   "text":  "Just trying this out..."> }> '{  "_index" : "website",  "_type" : "blog",  "_id" : "1",  "_version" : 1,  "result" : "created",  "_shards" : {    "total" : 2,    "successful" : 1,    "failed" : 0  },  "_seq_no" : 5,  "_primary_term" : 10}

检索创建的博客

[looking@master packages]$ curl -X GET "localhost:9200/website/blog/1?pretty"{  "_index" : "website",  "_type" : "blog",  "_id" : "1",  "_version" : 1,  "_seq_no" : 5,  "_primary_term" : 10,  "found" : true,  "_source" : {    "title" : "My first blog entry",    "text" : "Just trying this out..."  }}

重建文档索引

[looking@master packages]$ curl -X PUT "localhost:9200/website/blog/1?version=2&version_type=external&pretty" -H 'Content-Type: application/json' -d'{                                  "title": "My first blog entry",  "text":  "Starting to get the hang of this..."}'{  "_index" : "website",  "_type" : "blog",  "_id" : "1",  "_version" : 2,  "result" : "updated",  "_shards" : {    "total" : 2,    "successful" : 1,    "failed" : 0  },  "_seq_no" : 8,  "_primary_term" : 10}

文档部分更新

[looking@master packages]$ curl -X GET "localhost:9200/website/blog/1?pretty"{  "_index" : "website",  "_type" : "blog",  "_id" : "1",  "_version" : 2,  "_seq_no" : 8,  "_primary_term" : 10,  "found" : true,  "_source" : {    "title" : "My first blog entry",    "text" : "Starting to get the hang of this..."  }}[looking@master packages]$ curl -X POST "localhost:9200/website/blog/1/_update?pretty" -H 'Content-Type: application/json' -d'> {>    "doc" : {>       "tags" : [ "testing" ],>       "views": 0>    }> }> '{  "_index" : "website",  "_type" : "blog",  "_id" : "1",  "_version" : 3,  "result" : "updated",  "_shards" : {    "total" : 2,    "successful" : 1,    "failed" : 0  },  "_seq_no" : 9,  "_primary_term" : 10}[looking@master packages]$ curl -X GET "localhost:9200/website/blog/1?pretty"{  "_index" : "website",  "_type" : "blog",  "_id" : "1",  "_version" : 3,  "_seq_no" : 9,  "_primary_term" : 10,  "found" : true,  "_source" : {    "title" : "My first blog entry",    "text" : "Starting to get the hang of this...",    "views" : 0,    "tags" : [      "testing"    ]  }}
[looking@master packages]$ curl -X GET "localhost:9200/website/blog/1?pretty"{  "_index" : "website",  "_type" : "blog",  "_id" : "1",  "_version" : 3,  "_seq_no" : 9,  "_primary_term" : 10,  "found" : true,  "_source" : {    "title" : "My first blog entry",    "text" : "Starting to get the hang of this...",    "views" : 0,    "tags" : [      "testing"    ]  }}[looking@master packages]$ curl -X POST "localhost:9200/website/blog/1/_update?pretty" -H 'Content-Type: application/json' -d'> {>    "script" : "ctx._source.views+=1"> }> '{  "_index" : "website",  "_type" : "blog",  "_id" : "1",  "_version" : 4,  "result" : "updated",  "_shards" : {    "total" : 2,    "successful" : 1,    "failed" : 0  },  "_seq_no" : 10,  "_primary_term" : 10}[looking@master packages]$ curl -X GET "localhost:9200/website/blog/1?pretty"{  "_index" : "website",  "_type" : "blog",  "_id" : "1",  "_version" : 4,  "_seq_no" : 10,  "_primary_term" : 10,  "found" : true,  "_source" : {    "title" : "My first blog entry",    "text" : "Starting to get the hang of this...",    "views" : 1,    "tags" : [      "testing"    ]  }}

取回多个文档

[looking@master packages]$ curl -X GET "localhost:9200/_mget?pretty" -H 'Content-Type: application/json' -d'{   "docs" : [      {         "_index" : "website",         "_type" :  "blog",         "_id" :    2      },      {         "_index" : "website",         "_type" :  "pageviews",         "_id" :    1,         "_source": "views"      }   ]}'{  "docs" : [    {      "_index" : "website",      "_type" : "blog",      "_id" : "2",      "_version" : 11,      "_seq_no" : 7,      "_primary_term" : 10,      "found" : true,      "_source" : {        "title" : "My first external blog entry",        "text" : "This is a piece of cake..."      }    },    {      "_index" : "website",      "_type" : "pageviews",      "_id" : "1",      "found" : false    }  ]}
[looking@master packages]$ curl -X GET "localhost:9200/website/blog/_mget?pretty" -H 'Content-Type: application/json' -d'> {>    "docs" : [>       { "_id" : 2 },>       { "_type" : "pageviews", "_id" :   1 }>    ]> }> '{  "docs" : [    {      "_index" : "website",      "_type" : "blog",      "_id" : "2",      "_version" : 11,      "_seq_no" : 7,      "_primary_term" : 10,      "found" : true,      "_source" : {        "title" : "My first external blog entry",        "text" : "This is a piece of cake..."      }    },    {      "_index" : "website",      "_type" : "pageviews",      "_id" : "1",      "found" : false    }  ]}

搜索

多索引搜索

[looking@master ~]$ curl -XGET localhost:9200/us,gb/_search?pretty{  "took" : 3,  "timed_out" : false,  "_shards" : {    "total" : 2,    "successful" : 2,    "skipped" : 0,    "failed" : 0  },  "hits" : {    "total" : {      "value" : 14,      "relation" : "eq"    },    "max_score" : 1.0,    "hits" : [      {        "_index" : "gb",        "_type" : "user",        "_id" : "2",        "_score" : 1.0,        "_source" : {          "email" : "mary@jones.com",          "name" : "Mary Jones",          "username" : "@mary"        }      },      ...      {        "_index" : "us",        "_type" : "user",        "_id" : "6",        "_score" : 1.0,        "_source" : {          "date" : "2014-09-16",          "name" : "John Smith",          "tweet" : "The Elasticsearch API is really easy to use",          "user_id" : 1        }      }    ]  }}

结果默认显示 10 条,如果想多输出一些结果,可以自定义 size。 

[looking@master ~]$ curl -XGET "localhost:9200/us,gb/_search?size=14&pretty"
[looking@master ~]$ curl -XGET "localhost:9200/us,gb/_search?size=3&from=5&pretty"{  "took" : 2,  "timed_out" : false,  "_shards" : {    "total" : 2,    "successful" : 2,    "skipped" : 0,    "failed" : 0  },  "hits" : {    "total" : {      "value" : 14,      "relation" : "eq"    },    "max_score" : 1.0,    "hits" : [      {        "_index" : "gb",        "_type" : "user",        "_id" : "11",        "_score" : 1.0,        "_source" : {          "date" : "2014-09-21",          "name" : "Mary Jones",          "tweet" : "Elasticsearch is built for the cloud, easy to scale",          "user_id" : 2        }      },      {        "_index" : "gb",        "_type" : "user",        "_id" : "13",        "_score" : 1.0,        "_source" : {          "date" : "2014-09-23",          "name" : "Mary Jones",          "tweet" : "So yes, I am an Elasticsearch fanboy",          "user_id" : 2        }      },      {        "_index" : "us",        "_type" : "user",        "_id" : "1",        "_score" : 1.0,        "_source" : {          "email" : "john@smith.com",          "name" : "John Smith",          "username" : "@john"        }      }    ]  }}

轻量搜索

[looking@master ~]$ curl -X GET "localhost:9200/_search?q=mary&pretty"{  "took" : 484,  "timed_out" : false,  "_shards" : {    "total" : 8,    "successful" : 8,    "skipped" : 0,    "failed" : 0  },  "hits" : {    "total" : {      "value" : 8,      "relation" : "eq"    },    "max_score" : 1.4454254,    "hits" : [      {        "_index" : "us",        "_type" : "user",        "_id" : "4",        "_score" : 1.4454254,        "_source" : {          "date" : "2014-09-14",          "name" : "John Smith",          "tweet" : "@mary it is not just text, it does everything",          "user_id" : 1        }      },      {        "_index" : "gb",        "_type" : "user",        "_id" : "2",        "_score" : 0.2876821,        "_source" : {          "email" : "mary@jones.com",          "name" : "Mary Jones",          "username" : "@mary"        }      },      {        "_index" : "gb",        "_type" : "user",        "_id" : "3",        "_score" : 0.06453852,        "_source" : {          "date" : "2014-09-13",          "name" : "Mary Jones",          "tweet" : "Elasticsearch means full text search has never been so easy",          "user_id" : 2        }      },      {        "_index" : "gb",        "_type" : "user",        "_id" : "5",        "_score" : 0.06453852,        "_source" : {          "date" : "2014-09-15",          "name" : "Mary Jones",          "tweet" : "However did I manage before Elasticsearch?",          "user_id" : 2        }      },      {        "_index" : "gb",        "_type" : "user",        "_id" : "7",        "_score" : 0.06453852,        "_source" : {          "date" : "2014-09-17",          "name" : "Mary Jones",          "tweet" : "The Query DSL is really powerful and flexible",          "user_id" : 2        }      },      {        "_index" : "gb",        "_type" : "user",        "_id" : "9",        "_score" : 0.06453852,        "_source" : {          "date" : "2014-09-19",          "name" : "Mary Jones",          "tweet" : "Geo-location aggregations are really cool",          "user_id" : 2        }      },      {        "_index" : "gb",        "_type" : "user",        "_id" : "11",        "_score" : 0.06453852,        "_source" : {          "date" : "2014-09-21",          "name" : "Mary Jones",          "tweet" : "Elasticsearch is built for the cloud, easy to scale",          "user_id" : 2        }      },      {        "_index" : "gb",        "_type" : "user",        "_id" : "13",        "_score" : 0.06453852,        "_source" : {          "date" : "2014-09-23",          "name" : "Mary Jones",          "tweet" : "So yes, I am an Elasticsearch fanboy",          "user_id" : 2        }      }    ]  }}

映射和分析

分析与分析器

[looking@master ~]$ curl -X GET "localhost:9200/_search?q=2014&pretty"{  "took" : 27,  "timed_out" : false,  "_shards" : {    "total" : 8,    "successful" : 8,    "skipped" : 0,    "failed" : 0  },  "hits" : {    "total" : {      "value" : 1,      "relation" : "eq"    },    "max_score" : 1.0,    "hits" : [      {        "_index" : "bank",        "_type" : "account",        "_id" : "2014",        "_score" : 1.0,        "_source" : {          "account_number" : 2014,          "balance" : 8212,          "firstname" : "Josefina",          "lastname" : "Wagner",          "age" : 36,          "gender" : "M",          "address" : "418 Estate Road",          "employer" : "Kyaguru",          "email" : "josefinawagner@kyaguru.com",          "city" : "Darbydale",          "state" : "FL"        }      }    ]  }}[looking@master ~]$ curl -X GET "localhost:9200/_search?q=2014-09-15&pretty"{  "took" : 186,  "timed_out" : false,  "_shards" : {    "total" : 8,    "successful" : 8,    "skipped" : 0,    "failed" : 0  },  "hits" : {    "total" : {      "value" : 1,      "relation" : "eq"    },    "max_score" : 1.0,    "hits" : [      {        "_index" : "gb",        "_type" : "user",        "_id" : "5",        "_score" : 1.0,        "_source" : {          "date" : "2014-09-15",          "name" : "Mary Jones",          "tweet" : "However did I manage before Elasticsearch?",          "user_id" : 2        }      }    ]  }}[looking@master ~]$ curl -X GET "localhost:9200/_search?q=date:2014-09-15&pretty"{  "took" : 5,  "timed_out" : false,  "_shards" : {    "total" : 8,    "successful" : 8,    "skipped" : 0,    "failed" : 0  },  "hits" : {    "total" : {      "value" : 1,      "relation" : "eq"    },    "max_score" : 1.0,    "hits" : [      {        "_index" : "gb",        "_type" : "user",        "_id" : "5",        "_score" : 1.0,        "_source" : {          "date" : "2014-09-15",          "name" : "Mary Jones",          "tweet" : "However did I manage before Elasticsearch?",          "user_id" : 2        }      }    ]  }}[looking@master ~]$ curl -X GET "localhost:9200/_search?q=date:2014&pretty"{  "took" : 7,  "timed_out" : false,  "_shards" : {    "total" : 8,    "successful" : 8,    "skipped" : 0,    "failed" : 0  },  "hits" : {    "total" : {      "value" : 0,      "relation" : "eq"    },    "max_score" : null,    "hits" : [ ]  }}

测试分析器

[looking@master ~]$ curl -X GET "localhost:9200/_analyze?pretty" -H 'Content-Type: application/json' -d'> {>   "analyzer": "standard",>   "text": "Text to analyze"> }> '{  "tokens" : [    {      "token" : "text",      "start_offset" : 0,      "end_offset" : 4,      "type" : "
", "position" : 0 }, { "token" : "to", "start_offset" : 5, "end_offset" : 7, "type" : "
", "position" : 1 }, { "token" : "analyze", "start_offset" : 8, "end_offset" : 15, "type" : "
", "position" : 2 } ]}

映射

[looking@master ~]$ curl -XGET "localhost:9200/gb/_mapping?pretty"{  "gb" : {    "mappings" : {      "properties" : {        "date" : {          "type" : "date"        },        "email" : {          "type" : "text",          "fields" : {            "keyword" : {              "type" : "keyword",              "ignore_above" : 256            }          }        },        "name" : {          "type" : "text",          "fields" : {            "keyword" : {              "type" : "keyword",              "ignore_above" : 256            }          }        },        "tweet" : {          "type" : "text",          "fields" : {            "keyword" : {              "type" : "keyword",              "ignore_above" : 256            }          }        },        "user_id" : {          "type" : "long"        },        "username" : {          "type" : "text",          "fields" : {            "keyword" : {              "type" : "keyword",              "ignore_above" : 256            }          }        }      }    }  }}

测试查询

[looking@master ~]$ curl -X GET "localhost:9200/gb/tweet/_validate/query?pretty" -H 'Content-Type: application/json' -d'> {>    "query": {>       "tweet" : {>          "match" : "really powerful">       }>    }> }> '{  "valid" : false}[looking@master ~]$ curl -X GET "localhost:9200/gb/tweet/_validate/query?explain&pretty" -H 'Content-Type: application/json' -d'> {>    "query": {>       "tweet" : {>          "match" : "really powerful">       }>    }> }> '{  "valid" : false,  "error" : "org.elasticsearch.common.ParsingException: no [query] registered for [tweet]"}

索引管理

创建索引

[looking@master ~]$ curl -XPUT localhost:9200/my_index?pretty{  "acknowledged" : true,  "shards_acknowledged" : true,  "index" : "my_index"}[looking@master ~]$ curl -XGET localhost:9200/_cat/indices?vhealth status index    uuid                   pri rep docs.count docs.deleted store.size pri.store.sizeyellow open   bank     cSxDPBsyRVyjsHGe9b2RZA   1   1      13022        16978      5.6mb          5.6mbyellow open   website  svdjlzy2TfCQFLHcKWhjRw   1   1          3            1     15.1kb         15.1kbyellow open   megacorp Ed5gg9hoRM24dE3AAD1DEQ   1   1          3            0     11.5kb         11.5kbyellow open   blogs    qfnun_91RI2O1lgTjnBmCQ   3   1          0            0       849b           849byellow open   my_index MygM_DFSQQ2ydVXuRUFXow   1   1          0            0       230b           230b

删除索引

[looking@master ~]$ curl -XGET localhost:9200/_cat/indices?vhealth status index    uuid                   pri rep docs.count docs.deleted store.size pri.store.sizeyellow open   bank     cSxDPBsyRVyjsHGe9b2RZA   1   1      13022        16978      5.6mb          5.6mbyellow open   website  svdjlzy2TfCQFLHcKWhjRw   1   1          3            1     15.1kb         15.1kbyellow open   megacorp Ed5gg9hoRM24dE3AAD1DEQ   1   1          3            0     11.5kb         11.5kbyellow open   blogs    qfnun_91RI2O1lgTjnBmCQ   3   1          0            0       849b           849byellow open   gb       rl1qXOJ6SfSX1ITREyNw9A   1   1          7            0     12.4kb         12.4kbyellow open   us       C8cI7LhcQeCAM9eGsx8FNw   1   1          7            0      7.4kb          7.4kb[looking@master ~]$ curl -XDELETE localhost:9200/us,gb?pretty{  "acknowledged" : true}[looking@master ~]$ curl -XGET localhost:9200/_cat/indices?vhealth status index    uuid                   pri rep docs.count docs.deleted store.size pri.store.sizeyellow open   bank     cSxDPBsyRVyjsHGe9b2RZA   1   1      13022        16978      5.6mb          5.6mbyellow open   website  svdjlzy2TfCQFLHcKWhjRw   1   1          3            1     15.1kb         15.1kbyellow open   megacorp Ed5gg9hoRM24dE3AAD1DEQ   1   1          3            0     11.5kb         11.5kbyellow open   blogs    qfnun_91RI2O1lgTjnBmCQ   3   1          0            0       849b           849b

索引设置

[looking@master ~]$ curl -X PUT "localhost:9200/my_temp_index?pretty" -H 'Content-Type: application/json' -d'> {>     "settings": {>         "number_of_shards" :   1,>         "number_of_replicas" : 0>     }> }> '{  "acknowledged" : true,  "shards_acknowledged" : true,  "index" : "my_temp_index"}[looking@master ~]$ curl -X PUT "localhost:9200/my_temp_index/_settings?pretty" -H 'Content-Type: application/json' -d'> {>     "number_of_replicas": 1> }> '{  "acknowledged" : true}

配置分析器

[looking@master ~]$ curl -X PUT "localhost:9200/spanish_docs?pretty" -H 'Content-Type: application/json' -d'> {>     "settings": {>         "analysis": {>             "analyzer": {>                 "es_std": {>                     "type":      "standard",>                     "stopwords": "_spanish_">                 }>             }>         }>     }> }> '{  "acknowledged" : true,  "shards_acknowledged" : true,  "index" : "spanish_docs"}

索引别名

[looking@master ~]$ curl -XGET localhost:9200/_cat/indices?vhealth status index        uuid                   pri rep docs.count docs.deleted store.size pri.store.sizeyellow open   bank         cSxDPBsyRVyjsHGe9b2RZA   1   1      13022        16978      5.6mb          5.6mbyellow open   website      svdjlzy2TfCQFLHcKWhjRw   1   1          3            1     15.1kb         15.1kbyellow open   megacorp     Ed5gg9hoRM24dE3AAD1DEQ   1   1          3            0     11.5kb         11.5kbyellow open   spanish_docs uAim-vNgQFqZeQjRkWbuqA   1   1          0            0       283b           283byellow open   blogs        qfnun_91RI2O1lgTjnBmCQ   3   1          0            0       849b           849b------------------------------------------------------------[looking@master ~]$ curl -XPUT localhost:9200/blogs/_alias/blogs_alias?pretty{  "acknowledged":true}------------------------------------------------------------[looking@master ~]$ curl -XGET localhost:9200/_cat/indices?vhealth status index        uuid                   pri rep docs.count docs.deleted store.size pri.store.sizeyellow open   bank         cSxDPBsyRVyjsHGe9b2RZA   1   1      13022        16978      5.6mb          5.6mbyellow open   website      svdjlzy2TfCQFLHcKWhjRw   1   1          3            1     15.1kb         15.1kbyellow open   megacorp     Ed5gg9hoRM24dE3AAD1DEQ   1   1          3            0     11.5kb         11.5kbyellow open   spanish_docs uAim-vNgQFqZeQjRkWbuqA   1   1          0            0       283b           283byellow open   blogs        qfnun_91RI2O1lgTjnBmCQ   3   1          0            0       849b           849b------------------------------------------------------------[looking@master ~]$ curl -XGET localhost:9200/*/_alias/blogs_alias?pretty{  "blogs" : {    "aliases" : {      "blogs_alias" : { }    }  }}------------------------------------------------------------[looking@master ~]$ curl -XGET localhost:9200/blogs/_alias?pretty{  "blogs" : {    "aliases" : {      "blogs_alias" : { }    }  }}------------------------------------------------------------[looking@master ~]$ curl -XGET localhost:9200/_alias?pretty{  "spanish_docs" : {    "aliases" : { }  },  "blogs" : {    "aliases" : {      "blogs_alias" : { }    }  },  "bank" : {    "aliases" : { }  },  "website" : {    "aliases" : { }  },  "megacorp" : {    "aliases" : { }  }}------------------------------------------------------------[looking@master ~]$ curl -X POST "localhost:9200/_aliases?pretty" -H 'Content-Type: application/json' -d'> {>     "actions": [>         { "remove": { "index": "blogs", "alias": "blogs_alias" }},>         { "add":    { "index": "blogs", "alias": "blogs_bak" }}>     ]> }> '{  "acknowledged" : true}------------------------------------------------------------[looking@master ~]$ curl -XGET localhost:9200/blogs/_alias/*?pretty{  "blogs" : {    "aliases" : {      "blogs_bak" : { }    }  }}

 

转载地址:http://qcjqi.baihongyu.com/

你可能感兴趣的文章
UVM:8.3.1 重载transaction
查看>>
UVM:8.3.2 重载sequence
查看>>
leetcode171.[math] Excel Sheet Column Number
查看>>
Log4j配置
查看>>
java发送https请求证书问题
查看>>
js新消息提醒
查看>>
js窗体消息提醒
查看>>
深入Hibernate映射文件(二)——<hibernate-mapping>的属性
查看>>
详解在Spring中进行集成测试
查看>>
Struts2中过滤器和拦截器的区别
查看>>
51单片机:led灯闪烁10次后熄灭
查看>>
安卓:okhttp请求,获取返回数据
查看>>
安卓:股票筛选及分析系统
查看>>
增加windows下Tomcat运行时的内存
查看>>
tomcat群集中session共享的几个方案
查看>>
查找google谷歌北京IP地址的方法
查看>>
本科大数据专业该怎么上?
查看>>
云创大数据1+X大数据应用部署与调优职业技能等级证书预申报正式开启!
查看>>
人工智能需要一个可被证明的理论作为基础 | 哈佛丘成桐
查看>>
入门 | 一文概览深度学习中的激活函数
查看>>