mapping
plsql
# 注意 type 为text的不能进行统计查询
# 数据类型: Date float long double boolean Object keyword text
# text 可以分词 keyword 则不可以
PUT /emp_tab/_mapping
{
"properties":{
"empno":{
"type": "keyword" ,
"index":true
},
"ename":{
"type": "text"
},
"age":{
"type": "long"
},
"gender":{
"type": "keyword"
},
"sal":{
"type": "double"
}
, "hiredate":{
"type": "date" ,
"format": "yyyy-MM-dd HH:mm:ss"
}
}
}
创建
plsql
# 创建索引
PUT /student
#创建 mapping
PUT /student/_mapping
{
"properties":{
"id":{
"type": "keyword"
},
"name":{
"type": "text"
},
"mick_name":{
"type": "keyword"
}
}
}
## 创建 文档
POST /student/_doc
{
"id":"2",
"name":"李四2",
"mick_name":"李四2"
}
# 批量
POST /student/_bulk
{ "index" : { "_index" : "student" } }
{ "id":"3", "name":"白居易", "mick_name":"白居易"}
{ "index" : { "_index" : "student" } }
{ "id":"4", "name":"花小白","mick_name":"花小白"}
# 查询 文档 mick_name 现在是关键字 则无法模糊查询
GET /student/_search
{
"query": {
"match_phrase": {
"mick_name": "李"
}
}
}
# 查询 文档 name 现在是text 会被分词查询 模糊查询
GET /student/_search
{
"query": {
"match_phrase": {
"name": "李"
}
}
}
plsql
GET /emp_tab/_search
{
"query": {
"match_all": {}
}
}
GET /emp_tab/_search
{
"query": {
"match_phrase": {
"ename": "白"
}
}
}
GET /emp_tab/_search
{
"query": {
"bool": {
"must": [
{
"match_phrase": {
"ename": "白"
}
}
]
}
}
}
GET /emp_tab/_search
{
"query": {
"match_phrase": {
"ename": "白"
}
}
}
GET /emp_tab/_search
{
"query": {
"range": {
"age": {
"gte": 10,
"lte": 24
}
}
}
}
# 聚合查询
GET /emp_tab/_search
{
"aggs": {
"ageCount": {
"avg": {
"field": "age"
}
}
}
}
# 分组
GET /emp_tab/_search
{
"aggs": {
"genderCount": {
"terms": {
"field": "gender"
}
}
}
, "size": 0
}
# 精确查询 不能对 text 做
GET /emp_tab/_search
{
"query": {
"term": {
"gender": {
"value": "女"
}
}
}
}
# should must 参与算分 must not filter 不参与
GET /emp_tab/_search
{
"query": {
"bool": {
"filter": [
{
"match":{
"ename":"白骨"
}
}
]
}
}
}
# in
GET /emp_tab/_search
{
"query": {
"bool": {
"filter": [
{
"match":{
"ename":"白骨"
}
}
]
}
}
}
# in
GET /emp_tab/_search
{
"query": {
"bool": {
"filter": [
{
"terms": {
"gender": [
"男",
"女"
]
}
}
]
}
}
}
}
# 统计 收入 在5000以下的人的女生男生 人数
GET /emp_tab/_search
{
"query": {
"range": {
"sal": {
"lte": 5000
}
}
}
, "aggs": {
"name_count": {
"terms": {
"field": "gender"
}
}
}
}