curl -XPUT 'http://192.168.80.123:9200/school/jsj/1' -d '{
"class": "软件工程",
"subject": "math",
"name": {
"first": "li",
"last": "jie"
},
"create_time": "2017-08-10",
"score": "98"
}'
http://192.168.80.123:9200/school/jsj/1
结果:
{
"_index": "school",
"_type": "jsj",
"_id": "1",
"_version": 1,
"found": true,
"_source": {
"class": "软件工程",
"subject": "math",
"name": {
"first": "li",
"last": "jie"
},
"create_time": "2017-08-10",
"score": "98"
}
}
curl -XGET 'http://192.168.80.123:9200/school/jsj/1'
返回:
{
"_index": "school",
"_type": "jsj",
"_id": "1",
"_version": 1,
"found": true,
"_source": {
"class": "软件工程",
"subject": "math",
"name": {
"first": "li",
"last": "jie"
},
"create_time": "2017-08-10",
"score": "98"
}
}
curl -XPUT 'http://192.168.80.123:9200/school/jsj/2' -d '{
"subject": "math",
"name": {
"first": "zhang",
"last": "san"
},
"create_time": "2017-07-01",
"score": "59"
}'
curl -XGET 'http://192.168.80.123:9200/school/jsj/1?_source=subject'
结果:
{
"_index": "school",
"_type": "jsj",
"_id": "1",
"_version": 1,
"found": true,
"_source": {
"subject": "math"
}
}
curl -XGET 'http://192.168.80.123:9200/school/jsj/1?_source=subject,score'
结果:
{
"_index": "school",
"_type": "jsj",
"_id": "1",
"_version": 1,
"found": true,
"_source": {
"score": "98",
"subject": "math"
}
}
curl -XGET 'http://192.168.80.123:9200/school/jsj/1?_source'
结果:
{
"_index": "school",
"_type": "jsj",
"_id": "1",
"_version": 1,
"found": true,
"_source": {
"class": "软件工程",
"subject": "math",
"name": {
"first": "li",
"last": "jie"
},
"create_time": "2017-08-10",
"score": "98"
}
}
curl -XPUT 'http://192.168.80.123:9200/school/jsj/1' -d '{
"subject": "math",
"name": {
"first": "li",
"last": "jie"
},
"create_time": "2017-08-11",
"score": "100"
}'
返回:
{
"_index": "school",
"_type": "jsj",
"_id": "1",
"_version": 2,
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"created": false
}
再查看就变成100分了.
curl -XPOST 'http://192.168.80.123:9200/school/jsj/1/_update' -d '{
"doc": {
"score": "666"
}
}'
返回:
{
"_index": "school",
"_type": "jsj",
"_id": "1",
"_version": 4,
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
}
}
再查看就变成666分了.
curl -XDELETE 'http://192.168.80.123:9200/school/jsj/1'
返回:
{
"found": true,
"_index": "school",
"_type": "jsj",
"_id": "1",
"_version": 5,
"result": "deleted",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
}
}
再查看id为1的返回数据为:
{
"_index": "school",
"_type": "jsj",
"_id": "1",
"found": false
}
curl -XGET 'http://192.168.80.123:9200/school/jsj/_search' -d '{
"query": { "match_all": {} }
}'
返回:
{
"took": 10, //执行搜索的时间(以毫秒为单位)
"timed_out": false, //是否超时
"_shards": { //搜索分片,成功和失败的分片
"total": 5, //总搜索分片
"successful": 5, //成功搜索分片
"failed": 0 //失败搜索分片
},
"hits": {
"total": 2, //符合我们的搜索条件的文档总数
"max_score": 1.0, //最高分数
"hits": [{ //搜索结果的实际数组(默认为前10个文档)
"_index": "school",
"_type": "jsj",
"_id": "2",
"_score": 1.0, //是文档的分数信息,与排名相关度有关,参考各大搜索引擎的搜索结果,就容易理解。
"_source": {
"subject": "math",
"name": {
"first": "zhang",
"last": "san"
},
"create_time": "2017-07-01",
"score": "59"
}
},
{
"_index": "school",
"_type": "jsj",
"_id": "1",
"_score": 1.0, //是文档的分数信息,与排名相关度有关,参考各大搜索引擎的搜索结果,就容易理解。
"_source": {
"class": "软件工程",
"subject": "math",
"name": {
"first": "li",
"last": "jie"
},
"create_time": "2017-08-10",
"score": "98"
}
}]
}
}
curl-XGET'http: //192.168.80.123: 9200/school/jsj/_search'-d'{
"query": {
"match_all": {
}
},
"size": 1
}'
返回:
{
"took": 6,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 1.0,
"hits": [{
"_index": "school",
"_type": "jsj",
"_id": "2",
"_score": 1.0,
"_source": {
"subject": "math",
"name": {
"first": "zhang",
"last": "san" },
"create_time": "2017-07-01",
"score": "59"
}
}]
}
}
curl-XGET'http: //192.168.80.123: 9200/school/jsj/_search'-d'{
"query": {
"match_all": {
}
},
"from": 1,
"size": 10
}'
返回:
{
"took": 4,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 1.0,
"hits": [{
"_index": "school",
"_type": "jsj",
"_id": "1",
"_score": 1.0,
"_source": {
"class": "软件工程",
"subject": "math",
"name": {
"first": "li",
"last": "jie" },
"create_time": "2017-08-10",
"score": "98"
}
}]
}
}
先执行(不然报错,报错如下):
{“error”:{“root_cause”:[{“type”:”illegal_argument_exception”,”reason”:”Fielddata is disabled on text fields by default. Set fielddata=true
curl -XPUT 'http://192.168.80.123:9200/school/_mapping/jsj/' -d '{
"properties": {
"score": {
"type": "text",
"fielddata": true
}
}
}'
返回:
{"acknowledged":true}
然后:
curl-XGET'http: //192.168.80.123: 9200/school/jsj/_search'-d'{
"query": {
"match_all": {
}
},
"sort": {
"score": "desc"
}
}'
返回:
{
"took": 140,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 2,
"max_score": null,
"hits": [{
"_index": "school",
"_type": "jsj",
"_id": "1",
"_score": null,
"_source": {
"class": "软件工程",
"subject": "math",
"name": {
"first": "li",
"last": "jie" },
"create_time": "2017-08-10",
"score": "98"
},
"sort": ["98"]
},
{
"_index": "school",
"_type": "jsj",
"_id": "2",
"_score": null,
"_source": {
"subject": "math",
"name": {
"first": "zhang",
"last": "san" },
"create_time": "2017-07-01",
"score": "59"
},
"sort": ["59"]
}]
}
}
curl-XGET'http: //192.168.80.123: 9200/school/jsj/_search'-d'{
"query": {
"match_all": {
}
},
"_source": ["subject",
"name.last"]
}'
返回:
{
"took": 8,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 1.0,
"hits": [{
"_index": "school",
"_type": "jsj",
"_id": "2",
"_score": 1.0,
"_source": {
"subject": "math",
"name": {
"last": "san" }
}
},
{
"_index": "school",
"_type": "jsj",
"_id": "1",
"_score": 1.0,
"_source": {
"subject": "math",
"name": {
"last": "jie" }
}
}]
}
}
curl-XGET'http: //192.168.80.123: 9200/school/jsj/_search'-d'{
"query": {
"match": {
"name.last": "san"
}
},
"_source": ["subject",
"name.first"]
}'
返回:
{
"took": 6,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.2876821,
"hits": [{
"_index": "school",
"_type": "jsj",
"_id": "2",
"_score": 0.2876821,
"_source": {
"subject": "math",
"name": {
"first": "zhang" }
}
}]
}
}
curl -XGET 'http://192.168.80.123:9200/school/jsj/_search' -d '{
"query": { "match_phrase": { "name.last": "san" } }
}'
curl -XGET 'http://192.168.80.123:9200/school/jsj/_search' -d '{
"query": {
"bool": {
"must": [
{ "match": { "subject": "math" } },
{ "match": { "name.last": "jie" } }
]
}
}
}'
curl -XGET 'http://192.168.80.123:9200/school/jsj/_search' -d '{
"query": {
"bool": {
"should": [
{ "match": { "subject": "aaa" } },
{ "match": { "name.last": "jie" } }
]
}
}
}'
curl -XGET 'http://192.168.80.123:9200/school/jsj/_search' -d '{
"query": {
"bool": {
"must_not": [
{ "match": { "subject": "math" } },
{ "match": { "name.last": "jie" } }
]
}
}
}'
curl -XGET 'http://192.168.80.123:9200/school/jsj/_search' -d '{
"query": {
"bool": {
"must": [
{ "match": { "subject": "math" } }
],
"must_not": [
{ "match": { "name.last": "jie" } }
]
}
}
}'
curl -XGET 'http://192.168.80.123:9200/school/jsj/_search' -d '{
"query": {
"bool": {
"must": { "match_all": {} },
"filter": {
"range": {
"score": {
"gt": "8",
"lt": "99999"
}
}
}
}
}
}'
首先:
curl -XPUT 'http://192.168.80.123:9200/school/_mapping/jsj/' -d '{
"properties": {
"name.last": {
"type": "text",
"fielddata": true
}
}
}'
然后:
curl -XGET 'http://192.168.80.123:9200/school/jsj/_search' -d '{
"size": 0,
"aggs": {
"group_by_state": {
"terms": {
"field": "name.last"
}
}
}
}'