curl -XPUT 'localhost:9200/megacorp/employee/3' -d '
{
"first_name" : "Douglas",
"last_name" : "Fir",
"age" : 35,
"about": "I like to build cabinets",
"interests": [ "forestry" ]
}'
curl -XGET 'localhost:9200/megacorp/employee/1'
curl -XGET 'localhost:9200/megacorp/employee/_search'
curl -XGET 'localhost:9200/megacorp/employee/_search?q=last_name:Smith'
curl -XGET 'localhost:9200/megacorp/employee/_search' -d '
{
"query" : {
"match" : {
"last_name" : "Smith"
}
}
}'
curl -XGET 'localhost:9200/megacorp/employee/_search' -d '
{
"query" : {
"filtered" : {
"filter" : {
"range" : {
"age" : { "gt" : 30 }
}
},
"query" : {
"match" : { "last_name" : "smith" }
}
}
}
}'
curl -XGET 'localhost:9200/megacorp/employee/_search' -d '
{
"query" : {
"match" : {
"about" : "rock climbing"
}
}
}'
curl -XGET 'localhost:9200/megacorp/employee/_search' -d '
{
"query" : {
"match_phrase" : {
"about" : "rock climbing"
}
}
}'
curl -XGET 'localhost:9200/megacorp/employee/_search' -d '
{
"query" : {
"match_phrase" : {
"about" : "rock climbing"
}
},
"highlight": {
"fields" : {
"about" : {}
}
}
}'
curl -XGET 'localhost:9200/megacorp/employee/_search' -d '
{
"aggs": {
"all_interests": {
"terms": { "field": "interests" }
}
}
}'
curl -XGET 'localhost:9200/megacorp/employee/_search' -d '
{
"query": {
"match": {
"last_name": "smith"
}
},
"aggs": {
"all_interests": {
"terms": {
"field": "interests"
}
}
}
}'
curl -XGET 'localhost:9200/megacorp/employee/_search' -d '
{
"aggs" : {
"all_interests" : {
"terms" : { "field" : "interests" },
"aggs" : {
"avg_age" : {
"avg" : { "field" : "age" }
}
}
}
}
}'
curl -XGET 'localhost:9200/_cluster/health'
我们的文档存储在分片中,并且在分片中被索引,但是我们的应用程序不会直接与它们通信,取而代之的是,直接与索引通信。
分片可以是主分片(primary shard)或者是复制分片(replica shard)curl -XPUT 'localhost:9200/blogs' -d '
{
"settings" : {
"number_of_shards" : 3,
"number_of_replicas" : 1
}
}'
PUT /{index}/{type}/{id}
{
"field": "value",
...
}
curl -XGET 'localhost:9200/megacorp/employee/1?pretty'
curl -XGET 'localhost:9200/megacorp/employee/1?pretty&_source=first_name,last_name'
curl -XGET 'localhost:9200/megacorp/employee/1/_source'
curl -i -XHEAD 'localhost:9200/megacorp/employee/1'
curl -XPUT 'localhost:9200/megacorp/employee/3?op_type=create' -d '
{
"first_name" : "Douglas",
"last_name" : "Fir",
"age" : 35,
"about": "I like to build cabinets",
"interests": [ "forestry" ]
}'
curl -XPUT 'localhost:9200/megacorp/employee/4/_create' -d '
{
"first_name" : "Douglas",
"last_name" : "Fir",
"age" : 35,
"about": "I like to build cabinets",
"interests": [ "forestry" ]
}'
curl -XDELETE 'localhost:9200/megacorp/employee/4'
timeout
curl -XGET 'localhost:9200/megacorp/employee/_search?q=+first_name:Jane+last_name:Smith'
curl -XGET 'localhost:9200/megacorp/employee/_search?q=Smith'
{
"tag": {
"type": "string",
"index": "not_analyzed"
}
}
Elasticsearch将使用动态映射猜测字段类型,这类型来自于JSON的基本数据类型,使用以下规则:
Boolean: true or false "boolean"
Whole number: 123 "long"
Floating point: 123.45 "double"
String, valid date: "2014-09-15" "date"
String: "foo bar" "string"
PUT /gb/_mapping/tweet
{
"properties" : {
"tag" : {
"type" : "string",
"index": "not_analyzed"
}
}
}
curl -XGET 'localhost:9200/megacorp/_mapping/employee/'
{
"tweet": "Elasticsearch is very flexible",
"user": {
"id": "@johnsmith",
"gender": "male",
"age": 26,
"name": {
"full": "John Smith",
"first": "John",
"last": "Smith"
}
}
}
"user.name.full": [john, smith],
"user.name.first": [john],
"user.name.last": [smith]
********查询与过滤
{
"query": {
"match": {
"tweet": "elasticsearch"
}
}
}
{
"bool": {
"must": { "match": { "tweet": "elasticsearch" }},
"must_not": { "match": { "name": "mary" }},
"should": { "match": { "tweet": "full text" }}
}
}
{ "term": { "age": 26 }}
{
"query": {
"filtered": { -- filtered 查询同时接受接受 query 与 filter
"query": {
"match_all": { } -- match_all 用来匹配所有文档
},
"filter": {
"term": { -- term 过滤器
"price": 20
}
}
}
}
}
{
"terms": {
"tag": [ "search", "full_text", "nosql" ]
}
}
{
"query": {
"filtered": {
"filter": {
"terms": {
"price": [ 20, 30 ]
}
}
}
}
}
{
"range": {
"age": {
"gte": 20,
"lt": 30
}
}
}
{
"range": {
"timestamp": {
"gt": "now-1h" -- 找出所有时间戳大于当前时间减 1 小时的文档
}
}
}
{
"exists": {
"field": "title"
}
}
{
"bool": {
"must": { "term": { "folder": "inbox" }},
"must_not": { "term": { "tag": "spam" }},
"should": [
{ "term": { "starred": true }},
{ "term": { "unread": true }}
]
}
}
{
"match": {
"tweet": "About Search"
}
}
match 下指定了一个确切值
{ "match": { "age": 26 }}{
"multi_match": {
"query": "full text search",
"fields": [ "title", "body" ]
}
}
{
"bool": {
"must": {
"match": { "title": "how to make millions" }
},
"must_not": {
"match": { "tag": "spam"}
},
"should": [
{ "match": {"tag": "starred" } },
{ "range": { "date": {"gte": "2014-01-01"} } }
]
}
}
{
"filtered": {
"query": {
"match": { "email": "business opportunity" }
},
"filter": {
"term": { "folder": "inbox" }
}
}
}
{
"filtered": {
"query": { "match_all": {}}, -- 查询语句可以省略 相当于 match all
"filter": {
"term": { "folder": "inbox" }
}
}
}
GET /_search
{
"query": {
"filtered": {
"filter": {
"term": {"user_id": 1 }
}
}
},
"sort": {
"date": { "order": "desc" }
}
}
"sort": [
{ "date": { "order": "desc" }},
{ "_score": { "order": "desc" }}
]
------------------------索引管理---------------------------
1 创建索引"name": {
"type": "string",
"analyzer": "whitespace"
}
{
"mappings": {
"my_type": {
"dynamic": "strict", -- 当遇到未知字段时, my_type 对象将会抛出异常
"properties": {
"title": {
"type": "string"
},
"stash": {
"type": "object",
"dynamic": true -- stash 对象会自动创建字段
}
}
}
}
}
{
"mappings": {
"my_type": {
"dynamic_templates": [
{
"es": { -- 名称
"match": "*_es", -- 字段名以 _es 结尾需要使用 spanish 分析器
"match_mapping_type": "string",
"mapping": {
"type": "string",
"analyzer": "spanish"
}
}
},
{
"en": {
"match": "*", -- 所有其他字段使用 english 分析器。 匹配所有字符串类型字段
"match_mapping_type": "string",
"mapping": {
"type": "string",
"analyzer": "english"
}
}
}
]
}
}
}
{
"mappings": {
"_default_": {
"_all": { "enabled": false } -- 映射对所有类型禁用 _all 字段
},
"blog": {
"_all": { "enabled": true } -- 只在 blog 字段上开启 _all 字段
}
}
}
GET /megacorp/employee/_search
{
"aggs": {
"all_interests": {
"terms": {
"field": "interests"
},
"aggs": {
"avg_age": {
"avg": {
"field": "age"
}
}
}
}
}
}
curl 192.168.1.201:9200/_cat/indices?v
curl -XDELETE '192.168.1.201:9200/razor_cd'
curl -XDELETE '192.168.1.201:9200/razor_event'
curl -XDELETE '192.168.1.201:9200/razor_usinglog'
curl -XDELETE '192.168.1.201:9200/razor_app'
curl -XDELETE '192.168.1.201:9200/razor_tag'
curl -XGET '192.168.1.201:9200/razor_cd/clientdata/_search?pretty=true'
curl -XGET '192.168.1.201:9200/razor_app/app_info/_search?pretty=true'
curl -XGET '192.168.1.201:9200/razor_tag/app_tag/_search?pretty=true'
curl -XGET '192.168.1.201:9200/razor_event/event/_search?pretty=true'
curl -XGET '192.168.1.201:9200/razor_usinglog/usinglog/_search?pretty=true'
curl -XGET '192.168.1.201:9200/razor_app/app_info/_search?pretty=true' -d '
{
"query": {
"range": {
"localtime": {
"gte": "2016-03-28 00:00:00",
"lt": "2016-03-28 10:08:00"
}
}
}
}'
curl -XGET '192.168.1.201:9200/razor_app/app_info/_search?pretty=true' -d '
{
"query": {
"match": {
"user_id": "0c5ead1ca4bda378f0784440eda92700"
}
}
}'
curl -XGET '192.168.1.201:9200/razor_cd/clientdata/_search?pretty=true' -d '
{
"query": {
"bool": {
"must": [
{
"match": {
"user_id": "a8013699209"
}
},
{
"match": {
"product_id": "10028147"
}
}
]
}
},
"sort": {
"localtime": {
"order": "desc"
}
},
"from": 0,
"size": 100
}
'