ElasticSearch RESTful 接口调试记录&Tips

RESTful接口调试日志记录。未整理,纯流水记录。

1.索引

$ curl -XPUT ‘http://localhost:9200/multenant/default/12345d80-1f6e-4f5a-9695-baaef3890610’ -d ‘
{
“META_TYPE_ID”: “2ed7f3b3-c1fc-4386-8866-86ccd2061475”,
“META_DOC_ID”: “12345d80-1f6e-4f5a-9695-baaef3890610”,
“META_TENANTID”: “100001”,
“META_INDEX_TIME”: “201009201316”,
“Name”: null,
“HasWorkExperience”: false,
“Resume_Age”: 35
}’

curl -XPUT ‘http://localhost:9200/multenant/default/12345d81-1f6e-4f5a-9695-baaef3890610’ -d ‘
{
“__typeid”: “2ed7f3b3-c1fc-4386-8866-86ccd2061475”,
“__tenant”: “100002”,
“__time”: “202009201316”,
“Name”: “张三”,
“HasWorkExperience”: true,
“Resume_Age”: 45
}’

2.获取
$ curl -XGET ‘http://localhost:9200/multenant/default/12345d80-1f6e-4f5a-9695-baaef3890610?pretty=true’

result:
{
“_index” : “multenant”,
“_type” : “default”,
“_id” : “12345d80-1f6e-4f5a-9695-baaef3890610”, “_source” :
{
“META_TYPE_ID”: “2ed7f3b3-c1fc-4386-8866-86ccd2061475”,
“META_DOC_ID”: “12345d80-1f6e-4f5a-9695-baaef3890610”,
“META_TENANTID”: “100001”,
“META_INDEX_TIME”: “201009201316”,
“Name”: null,
“HasWorkExperience”: false,
“Resume_Age”: 35
}
}

$ curl -XGET ‘http://localhost:9200/multenant/default/12345d81-1f6e-4f5a-9695-baaef3890610?pretty=true’

有关返回结果数目,加上size参数即可,如下:
http://localhost:9200/_all/_search?pretty=true&size=100 -d'{
“query” : {
“matchAll” : {}
}
}’

3.搜索

curl -XGET ‘http://localhost:9200/multenant/default/_search?pretty=true’ -d ‘
{
“query” : {
“matchAll” : {}
}
}’

curl -XGET ‘http://localhost:9200/multenant/default/_search?pretty=true’ -d ‘
{
“query” : {
“term” : {“Name”:”张三”}
}
}’

curl -XGET ‘http://localhost:9200/multenant/default/_search?q=Resume_Age:35&pretty=true’

范围查找
curl -XGET ‘http://localhost:9200/multenant/_search?pretty=true’ -d ‘
{
“query” : {
“range” : {
“Resume_Age” : { “from” : “0”, “to” : “1000” }
}
}
}’

查找所有index
curl -XGET ‘http://localhost:9200/_all/default/_search?pretty=true’ -d ‘
{
“query” : {
“term” : {“Name”:”张三”}
}
}’

多条件联合
curl -XGET ‘http://localhost:9200/_all/default/_search?pretty=true’ -d ‘
{
“explain”: true,
“from”: 1,
“size”: 12,
“fields”: [
“ISexperience”,
“Name”
],
“query”: {
“term”: {
“ISexperience”: true,
“__TENANTID”: “100001”
},
“range”: {
“Resume_Age”: {
“from”: 0,
“to”: 1000,
“includeLower”: true,
“includeUpper”: true
}
}
}
}

curl -XGET ‘http://localhost:9200/_search?pretty=true’ -d ‘
{
“query” : {
“matchAll” : {}
}
}’

查询多个index

curl -XGET ‘http://localhost:9200/kimchy,another_user/_search?pretty=true’ -d ‘
{
“query” : {
“matchAll” : {}
}
}’

修改索引策略

curl -XPUT http://localhost:9200/another_user/ -d ‘
{
“index” : {
“numberOfShards” : 1,
“numberOfReplicas” : 1
}
}’

查看mapping
$ curl -XGET ‘http://localhost:9200/multenant/_mapping?pretty=true’
$ curl -XGET ‘http://localhost:9200/multenant/default/_mapping?pretty=true’

一次可查询一个或多个index或者type
$ curl -XGET ‘http://localhost:9200/multenant1,multenant/default,v123/_mapping?pretty=true’

type不存在,查询不会报错
$ curl -XGET ‘http://localhost:9200/multenant1/v122343/_mapping?pretty=true’
{
“multenant1” : {
}
}

删除
$ curl -XDELETE ‘http://localhost:9200/multenant/default/2275531b-d079-42fa-9505-9b2859d1264b’

medcl@bs-medcl ~
$ curl -XDELETE ‘http://localhost:9200/multenant/_query?q=user:kimchy'{“ok”:true,”_indices”:{“multenant”:{“_shards”:{“total”:5,”successful”:5,”failed”
:0}}}}

此方法好像不行哦,为什么?看后面
medcl@bs-medcl ~
$ curl -XDELETE ‘http://localhost:9200/multenant/_all/_query?q=user:kimchy'{“ok”:true,”_indices”:{“multenant”:{“_shards”:{“total”:5,”successful”:0,”failed”
:5}}}}

同样失败了
medcl@bs-medcl ~
$ curl -XDELETE ‘http://localhost:9200/_all/_all/_query?q=user:kimchy'{“ok”:true,”_indices”:{“multenant”:{“_shards”:{“total”:5,”successful”:0,”failed”:5}}}}

index type 可以传入多个,注意,如果index未定义,查询会报错:{“error”:”IndexMissingException[[twitter] missing]”},type未定义不会报错
$ curl -XDELETE ‘http://localhost:9200/multenant1,multenant/_all,default/_query
?q=user:kimchy’
{“ok”:true,”_indices”:{“multenant”:{“_shards”:{“total”:5,”successful”:0,”failed”:5}},”multenant1″:{“_shards”:{“total”:5,”successful”:0,”failed”:5}}}}

$ curl -XDELETE ‘http://localhost:9200/multenant1,multenant/default,v123,default/_query?q=user:kimchy’
{“ok”:true,”_indices”:{“multenant”:{“_shards”:{“total”:5,”successful”:0,”failed”
:5}},”multenant1″:{“_shards”:{“total”:5,”successful”:0,”failed”:5}}}}

delete by query dsl
$ curl -XDELETE ‘http://localhost:9200/twitter/tweet/_query’ -d ‘{ “term” : { “user” : “kimchy” }}’

下面的测试通过
删除 $ curl -XDELETE ‘http://localhost:9200/multenant/default/_query’ -d'{“term”:{“R
esume_Age”:”35″}}’

查询 $ curl -XGET ‘http://localhost:9200/multenant/default/_search?q=Resume_Age:35&p
retty=true’

$ curl -XDELETE ‘http://localhost:9200/multenant/default/_query?q=Resume_Age:45’

$ curl -XGET ‘http://localhost:9200/multenant/default/_search?q=Resume_Age:45&pretty=true’

找到原因了,原来type根本没有_all一说,只有index才有,记住了哦。
如果要删除某个index下的所有,无需指定type即可,不需要多此一举加_all了,哈哈。
medcl@bs-medcl ~
$ curl -XDELETE ‘http://localhost:9200/multenant/_query?q=Resume_Age:20’
{“ok”:true,”_indices”:{“multenant”:{“_shards”:{“total”:5,”successful”:5,”failed”:0}}}}
medcl@bs-medcl ~
$ curl -XGET ‘http://localhost:9200/multenant/default/_search?q=Resume_Age:20&pretty=true’
{
“_shards” : {
“total” : 5,
“successful” : 5,
“failed” : 0
},
“hits” : {
“total” : 0,
“max_score” : null,
“hits” : [ ]
}
}

$ curl -XDELETE ‘http://localhost:9200/multenant/_query?q=Resume_Age:3&pretty=true’
{
“ok” : true,
“_indices” : {
“multenant” : {
“_shards” : {
“total” : 5,
“successful” : 5,
“failed” : 0
}
}
}
}
medcl@bs-medcl ~
$ curl -XGET ‘http://localhost:9200/multenant/default/_search?q=Resume_Age:3&pretty=true’
{
“_shards” : {
“total” : 5,
“successful” : 5,
“failed” : 0
},
“hits” : {
“total” : 0,
“max_score” : null,
“hits” : [ ]
}
}

$ curl -XDELETE ‘http://localhost:9200/_all/_query?q=Resume_Age:43&pretty=true’

下面的方法的不行的
$ curl -XDELETE ‘http://localhost:9200/_query?q=Resume_Age:34&pretty=true’
{
“error” : “[_query] missing”
}

再测,_all,sometyp是不存在的type,结果查询失败了
$ curl -XDELETE ‘http://localhost:9200/multenant/_all,default,sometype/_query?q=Resume_Age:23&pretty=true’
{
“ok” : true,
“_indices” : {
“multenant” : {
“_shards” : {
“total” : 5,
“successful” : 0,
“failed” : 5
}
}
}
}
去掉type _all,继续查询,结果还是失败
$ curl -XDELETE ‘http://localhost:9200/multenant/default,sometype/_query?q=Resume_Age:23&pretty=true’
{
“ok” : true,
“_indices” : {
“multenant” : {
“_shards” : {
“total” : 5,
“successful” : 0,
“failed” : 5
}
}
}
}

再测
现有如下数据,3条数据,multenant有2条,default和default_v1各一条,multenant1有一条
medcl@bs-medcl ~
$ curl -XGET ‘http://localhost:9200/_all/_search?q=Resume_Age:45&pretty=true’
{
“_shards” : {
“total” : 10,
“successful” : 10,
“failed” : 0
},
“hits” : {
“total” : 3,
“max_score” : 1.0,
“hits” : [ {
“_index” : “multenant1”,
“_type” : “default”,
“_id” : “12345d81-1f6e-4f5a-9695-baaef3890610”,
“_score” : 1.0, “_source” :
$ curl -XPUT ‘http://localhost:9200/multenant/default/12345d81-1f6e-4f5a-9695-baaef3890610’ -d ‘2ed7f3b3-c1fc-4386-8866-86ccd2061475″,
{ “__tenant”: “100002”,
“__typeid”: “2ed7f3b3-c1fc-4386-8866-86ccd2061475”,
“__tenant”: “100002”,
“__time”: “202009201316”,,
“Name”: “张三”,5
“HasWorkExperience”: true,
“Resume_Age”: 45
}’
“_type” : “default_v1”,
“_id” : “12345d81-1f6e-4f5a-9695-baaef3890610”,
“_score” : 1.0, “_source” :
{
“__typeid”: “2ed7f3b3-c1fc-4386-8866-86ccd2061475”,
“__tenant”: “100002”,
“__time”: “202009201316”,
“Name”: “张三”,
“HasWorkExperience”: true,
“Resume_Age”: 45
}
}, {
“_index” : “multenant”,
“_type” : “default”,
“_id” : “12345d81-1f6e-4f5a-9695-baaef3890610”,
“_score” : 1.0, “_source” :
{
“__typeid”: “2ed7f3b3-c1fc-4386-8866-86ccd2061475”,
“__tenant”: “100002”,
“__time”: “202009201316”,
“Name”: “张三”,
“HasWorkExperience”: true,
“Resume_Age”: 45
}
} ]
}
}
试着删除看看
medcl@bs-medcl ~
$ curl -XDELETE ‘http://localhost:9200/multenant,multenant1/default,default_v1/_query?q=Resume_Age:45&pretty=true’
{
“ok” : true,
“_indices” : {
“multenant” : {
“_shards” : {
“total” : 5,
“successful” : 5,
“failed” : 0
}
},
“multenant1” : {
“_shards” : {
“total” : 5,
“successful” : 0,
“failed” : 5
}
}
}
}
上面很明显失败了,看来,这连个index的type不一样,一起指定是不行的,看下面的这个
medcl@bs-medcl ~
$ curl -XDELETE ‘http://localhost:9200/multenant/default,default_v1/_query?q=Resume_Age:45&pretty=true’
{
“ok” : true,
“_indices” : {
“multenant” : {
“_shards” : {
“total” : 5,
“successful” : 5,
“failed” : 0
}
}
}
}
结果显示删除了指定的数据。
medcl@bs-medcl ~
$ curl -XGET ‘http://localhost:9200/_all/_search?q=Resume_Age:45&pretty=true’
{
“_shards” : {
“total” : 10,
“successful” : 10,
“failed” : 0
},
“hits” : {
“total” : 1,
“max_score” : 1.0,
“hits” : [ {
“_index” : “multenant1”,
“_type” : “default”,
“_id” : “12345d81-1f6e-4f5a-9695-baaef3890610”,
“_score” : 1.0, “_source” :
{
“__typeid”: “2ed7f3b3-c1fc-4386-8866-86ccd2061475”,
“__tenant”: “100002”,
“__time”: “202009201316”,
“Name”: “张三”,
“HasWorkExperience”: true,
“Resume_Age”: 45
}
} ]
}
}
执行如下,结果也是失败的。
medcl@bs-medcl ~
$ curl -XDELETE ‘http://localhost:9200/_all/default,default_v1/_query?q=Resume_Age:45&pretty=true’
{
“ok” : true,
“_indices” : {
“multenant” : {
“_shards” : {
“total” : 5,
“successful” : 5,
“failed” : 0
}
},
“multenant1” : {
“_shards” : {
“total” : 5,
“successful” : 0,
“failed” : 5
}
}
}
}
小结下,不指定index,又要删除指定type下的数据,貌似不行的,查询也是一样,如下
$ curl -XGET ‘http://localhost:9200/_all/default,default_v1/_search?q=Resume_Age:45&pretty=true’
{
“_shards” : {
“total” : 10,
“successful” : 5,
“failed” : 5,
“failures” : [ {
“index” : “multenant1”,
“shard” : 1,
“reason” : “QueryPhaseExecutionException[[multenant1][1]: query[Resume_Age:[45 TO 45]],from[0],size[10]: Query Failed [Failed to execute main query]]; nested: TypeMissingException[[multenant1] type[default_v1] missing]; ”
}, {
“index” : “multenant1”,
“shard” : 0,
“reason” : “QueryPhaseExecutionException[[multenant1][0]: query[Resume_Age:[45 TO 45]],from[0],size[10]: Query Failed [Failed to execute main query]]; nested: TypeMissingException[[multenant1] type[default_v1] missing]; ”
}, {
“index” : “multenant1”,
“shard” : 2,
“reason” : “QueryPhaseExecutionException[[multenant1][2]: query[Resume_Age:[45 TO 45]],from[0],size[10]: Query Failed [Failed to execute main query]]; nested: TypeMissingException[[multenant1] type[default_v1] missing]; ”
}, {
“index” : “multenant1”,
“shard” : 4,
“reason” : “QueryPhaseExecutionException[[multenant1][4]: query[Resume_Age:[45 TO 45]],from[0],size[10]: Query Failed [Failed to execute main query]]; nested: TypeMissingException[[multenant1] type[default_v1] missing]; ”
}, {
“index” : “multenant1”,
“shard” : 3,
“reason” : “QueryPhaseExecutionException[[multenant1][3]: query[Resume_Age:[45 TO 45]],from[0],size[10]: Query Failed [Failed to execute main query]]; nested: TypeMissingException[[multenant1] type[default_v1] missing]; ”
} ]
},
“hits” : {
“total” : 0,
“max_score” : null,
“hits” : [ ]
}
}

如果同一index里两个type里面字段不一样,一个有,一个没有,查询同时指定这两个字段,结果会怎样,测测

$ curl -XPUT ‘http://localhost:9200/multenant2/default/123’ -d ‘
{
“META_TYPE_ID”: “2ed7f3b3-c1fc-4386-8866-86ccd2061475”,
“A”: “abc”
}’

curl -XPUT ‘http://localhost:9200/multenant2/default_v1/123’ -d ‘
{
“META_TYPE_ID”: “2ed7f3b3-c1fc-4386-8866-86ccd2061475”,
“B”: “abc”
}’

$ curl -XGET ‘http://localhost:9200/multenant2/_search?q=”B:abc OR A:abc”&pretty=true’
curl: (52) Empty reply from server

关于多条件的q怎么弄?疑问中。。。
原来这样直接传是不行的,urlencode一下试试
$ curl -XGET ‘http://localhost:9200/multenant2/_search?q=”B%3aabc+OR+A%3aabc”&p
retty=true’
{
“_shards” : {
“total” : 5,
“successful” : 5,
“failed” : 0
},
“hits” : {
“total” : 0,
“max_score” : null,
“hits” : [ ]
}
}
为何?去掉双引号试试,
$ curl -XGET ‘http://localhost:9200/multenant2/default_v1/_search?q=B%3a%22abc%
22+OR+A%3a%22abc%22&pretty=true’
{
“_shards” : {
“total” : 5,
“successful” : 5,
“failed” : 0
},
“hits” : {
“total” : 1,
“max_score” : 0.28986934,
“hits” : [ {
“_index” : “multenant2”,
“_type” : “default_v1”,
“_id” : “123”,
“_score” : 0.28986934, “_source” :
{
“META_TYPE_ID”: “2ed7f3b3-c1fc-4386-8866-86ccd2061475”,
“B”: “abc”
}
} ]
}
}

$ curl -XGET ‘http://localhost:9200/_search?q=B%3a%22abc%22+OR+A%3a%22abc%22&pretty=true’
{edcl@bs-medcl ~
“_shards” : {
“total” : 15,
“successful” : 15,
“failed” : 0
},
“hits” : {
“total” : 2,
“max_score” : 0.28986934,
“hits” : [ {
“_index” : “multenant2”,
“_type” : “default_v1”,
“_id” : “123”,
“_score” : 0.28986934, “_source” :
{
“META_TYPE_ID”: “2ed7f3b3-c1fc-4386-8866-86ccd2061475”,
“B”: “abc”
}
}, {
“_index” : “multenant2”,
“_type” : “default”,
“_id” : “123”,
“_score” : 0.09848769, “_source” :
{
“META_TYPE_ID”: “2ed7f3b3-c1fc-4386-8866-86ccd2061475”,
“A”: “abc”
}
} ]
}
}

$ curl -XGET ‘http://localhost:9200/multenant2/_search?q=B%3a%22abc%22+OR+A%3a%22abc%22&pretty=true’
{
“_shards” : {
“total” : 5,
“successful” : 5,
“failed” : 0
},
“hits” : {
“total” : 2,
“max_score” : 0.28986934,
“hits” : [ {
“_index” : “multenant2”,
“_type” : “default_v1”,
“_id” : “123”,
“_score” : 0.28986934, “_source” :
{
“META_TYPE_ID”: “2ed7f3b3-c1fc-4386-8866-86ccd2061475”,
“B”: “abc”
}
}, {
“_index” : “multenant2”,
“_type” : “default”,
“_id” : “123”,
“_score” : 0.09848769, “_source” :
{
“META_TYPE_ID”: “2ed7f3b3-c1fc-4386-8866-86ccd2061475”,
“A”: “abc”
}
} ]
}
}

试试AND操作
$ curl -XGET ‘http://localhost:9200/multenant2/_search?q=B%3a%22abc%22+AND+A%3a
%22abc%22&pretty=true’
{
“_shards” : {
“total” : 5,
“successful” : 5,
“failed” : 0
},
“hits” : {
“total” : 0,
“max_score” : null,
“hits” : [ ]
}
}

注意:只有指定的type完全满足这个查询的时候,才会合并返回结果,so,写查询的时候,得注意了。
另外,q是支持Lucene的queryparser出来的查询字符串的,查询语法参考这里:
http://lucene.apache.org/java/3_0_1/queryparsersyntax.html

再小结下:
正确的方式,先确定index,然后再针对性的指定type
$ curl -XGET ‘http://localhost:9200/_all/_search?q=Resume_Age:45&pretty=true’
{
“_shards” : {
“total” : 10,
“successful” : 10,
“failed” : 0
},
“hits” : {
“total” : 1,
“max_score” : 1.0,
“hits” : [ {
“_index” : “multenant1”,
“_type” : “default”,
“_id” : “12345d81-1f6e-4f5a-9695-baaef3890610”,
“_score” : 1.0, “_source” :
{
“__typeid”: “2ed7f3b3-c1fc-4386-8866-86ccd2061475”,
“__tenant”: “100002”,
“__time”: “202009201316”,
“Name”: “张三”,
“HasWorkExperience”: true,
“Resume_Age”: 45
}
} ]
}
}
$ curl -XGET ‘http://localhost:9200/multenant/_search?q=Resume_Age:45&pretty=true’
{
“_shards” : {
“total” : 5,
“successful” : 5,
“failed” : 0
},
“hits” : {
“total” : 0,
“max_score” : null,
“hits” : [ ]
}
}