本文主要是总结Kibana DevTools操作es的语句。
GET /course_idx/_doc/course:20
匹配"name"字段的值为"6789999"的文档
GET /course_idx/_search
{
"query":{
"term": {
"name": "6789999"
}
}
}
匹配课程编号包含"C00B5230920105650700A1"、"C00B5230921171813401A8"中任意一个值的文档。
GET course_idx/_doc/_search
{
"query" : {
"terms" : {
"courseNo" : [
"C00B5230920105650700A1",
"C00B5230921171813401A8"
],
"boost" : 1.0
}
}
}
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 2,
"max_score" : 1.0,
"hits" : [
{
"_index" : "course_idx",
"_type" : "_doc",
"_id" : "course:23",
"_score" : 1.0,
"_source" : {
"id" : "course:23",
"courseType" : 0,
"courseNo" : "C00B5230921171813401A8",
// 省略其他字段
}
},
{
"_index" : "course_idx",
"_type" : "_doc",
"_id" : "course:7",
"_score" : 1.0,
"_source" : {
"id" : "course:7",
"courseType" : 0,
"courseNo" : "C00B5230920105650700A1",
// 省略其他字段
}
}
]
}
}
用于在文本字段中执行全文搜索,可以模糊匹配文本。它会分析文本,将其分成词汇,并搜索匹配的词汇。
GET /course_idx/_search
{
"query": {
"match": {
"name": "课"
}
}
}
用于在多个字段上执行全文搜索。你可以指定多个字段,并搜索它们中的匹配项。
GET /course_idx/_search
{
"query": {
"multi_match": {
"query": "课",
"fields": ["name", "courseNo"]
}
}
}
查询courseNo=‘C005230922B133545556M4’ and useCount=0的记录
GET /course_idx/_search
{
"query": {
"bool": {
"must": [
{ "term": { "courseNo": "C005230922B133545556M4" } },
{ "term": { "useCount": "0" } }
]
}
}
}
查询courseNo=‘C005230922B133545556M4’ or useCount=5的记录
GET /course_idx/_search
{
"query": {
"bool": {
"should": [
{ "term": { "courseNo": "C005230922B133545556M4" } },
{ "term": { "useCount": "5" } }
]
}
}
}
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 3,
"max_score" : 5.0834727,
"hits" : [
{
"_index" : "course_idx",
"_type" : "_doc",
"_id" : "course:35",
"_score" : 5.0834727,
"_source" : {
"id" : "course:35",
"courseType" : 0,
"courseNo" : "C005230922B133545556M4",
"useCount" : 0
}
},
{
"_index" : "course_idx",
"_type" : "_doc",
"_id" : "lecture:942",
"_score" : 1.0,
"_source" : {
"id" : "lecture:942",
"courseType" : 2,
"courseNo" : "L005231012B1421252702M5",
"useCount" : 5
}
},
{
"_index" : "course_idx",
"_type" : "_doc",
"_id" : "lecture:943",
"_score" : 1.0,
"_source" : {
"id" : "lecture:943",
"courseType" : 2,
"courseNo" : "L005231012142125B2703M4",
"useCount" : 5
}
}
]
}
}
对must的取反操作,它是一个逻辑非运算。
GET /course_idx/_search
{
"query": {
"bool": {
"must_not": [
{ "term": { "courseNo": "C005230922B133545556M4" } }
]
}
}
}
允许使用通配符进行模糊匹配。
星号* :星号用于匹配零个或多个字符。例如,app* 将匹配任何以"app"开头的词汇,例如"apple"、"application"等。
问号?:问号用于匹配一个单一字符。例如,te?t 将匹配"test"、“text"等,但不会匹配"tent”,因为它包含了两个不同的字符。
GET /course_idx/_search
{
"query": {
"wildcard": {
"name.keyword": "22*"
}
}
}
{
"took" : 4,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 31,
"max_score" : 1.0,
"hits" : [
{
"_index" : "course_idx",
"_type" : "_doc",
"_id" : "lecture:184",
"_score" : 1.0,
"_source" : {
"id" : "lecture:184",
"courseType" : 2,
"courseNo" : "L005B2310111036451771M7",
"creatorId" : 155954,
"name" : "22"
}
},
{
"_index" : "course_idx",
"_type" : "_doc",
"_id" : "lecture:211",
"_score" : 1.0,
"_source" : {
"id" : "lecture:211",
"courseType" : 2,
"courseNo" : "L005B2310111052501806M4",
"creatorId" : 155954,
"name" : "222"
}
},
{
"_index" : "course_idx",
"_type" : "_doc",
"_id" : "lecture:557",
"_score" : 1.0,
"_source" : {
"id" : "lecture:557",
"courseType" : 2,
"courseNo" : "L0B052310111423472182M8",
"creatorId" : 155954,
"name" : "22222"
}
}
]
}
}
用于匹配字段的前缀
GET /course_idx/_search
{
"query": {
"prefix": {
"name": "22"
}
}
}
模糊查询name名称字段中包含"课"字的记录。
GET /course_idx/_search
{
"query": {
"fuzzy": {
"name": "课"
}
}
}
POST /course_idx/_doc/course:20
{
"doc": {
"name": "6789"
}
}
// 修改成功!!
{
"_index" : "course_idx",
"_type" : "_doc",
"_id" : "course:20",
"_version" : 14,
"result" : "updated",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 11960,
"_primary_term" : 1
}
DELETE /course_idx/_doc/course:20
这里只列举出几个字段,并不是全部字段的定义。
PUT /course_idx
{
"mappings":{
"_doc":{
"properties":{
"courseType":{
"type":"integer"
},
"creatorId":{
"type":"long"
},
"courseNo":{
"type":"keyword"
},
"name":{
"type":"text",
"fields":{
"keyword":{
"ignore_above":256,
"type":"keyword"
}
}
},
"id":{
"type":"keyword"
}
}
}
}
}
谨慎操作,这个会删除掉所有数据及结构。
DELETE /course_idx
GET /course_idx/_count
// 总记录数是1153条
{
"count" : 1153,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
}
}
统计字段useCount-使用次数的平均数
GET /course_idx/_search
{
"aggs": {
"avg_useCount": {
"avg": {
"field": "useCount"
}
}
}
}
使用次数的汇总
GET /course_idx/_search
{
"aggs": {
"total_useCount": {
"sum": {
"field": "useCount"
}
}
}
}
使用次数的桶统计
GET /course_idx/_search
{
"aggs": {
"useCount_ranges": {
"range": {
"field": "useCount",
"ranges": [
{ "from": 0, "to": 5 },
{ "from": 5, "to": 10 },
{ "from": 10, "to": 100 }
]
}
}
}
}
求最大使用次数和最小使用次数。
GET /course_idx/_search
{
"aggs": {
"max_useCount": {
"max": {
"field": "useCount"
}
},
"min_useCount": {
"min": {
"field": "useCount"
}
}
}
}
按天的直方图,统计每天的文档数量
GET /course_idx/_search
{
"aggs": {
"date_histogram": {
"date_histogram": {
"field": "createdDate",
"interval": "day"
}
}
}
}
es默认的分词器是standard。
下面以“数学的课程库”三个字为示例,看下分词结果。
GET /_analyze
{
"analyzer": "standard",
"text": "数学的课程库"
}
GET /_analyze
{
"text": "数学的课程库"
}
结果都是“数”“学”“的”“课”“程”“库”。
想要更好的支持中文分词,一般的建议是按照ik分词器。
如果想要进一步自定义分词,需要编写你自己的dict文本。