_source,是否启用原始数据,默认启用
includes,选择包含哪些字段,excludes:去除哪些字段
字段
不分词
- keyword
- constant_keyword
- wildcard
数值类型
- long
- integer
- short
- byte
- unsigned_long
- double
- float
- half_float
- scaled_float: 收缩浮点类型,可以指定收缩因子
日期类型
- date
- date_nanos
Object类型
array类型
range类型
- integer_range
- float_range
- long_range
- double_range
- date_range
- ip_range
适用场景:体重、日期范围,身高
join父子类型
PUT ckiss-company-001
{
"mappings": {
"properties": {
"companyId": {
"type": "integer"
},
"companyName": {
"type": "keyword"
},
"join_father_child": {
"type": "join",
"relations": {
"father": "child"
}
}
}
}
}
#填充集团数据
PUT ckiss-company-001/_doc/1
{
"companyId": 1,
"companyName": "华住会集团",
"join_father_child":{
"name": "father"
}
}
#子公司数据, routing保证父子文档处于同一分片中
PUT ckiss-company-001/_doc/2?routing=1
{
"companyId": 2,
"companyName": "上海华住",
"join_father_child": {
"name": "child",
"parent": "1"
}
}
#子公司数据
PUT ckiss-company-001/_doc/3?routing=1
{
"companyId": 3,
"companyName": "杭州华住",
"join_father_child": {
"name": "child",
"parent": "1"
}
}
# 查询:根据父ID查询所有子节点
GET ckiss-company-001/_search
{
"query": {
"parent_id": {
"type": "child",
"id": 1
}
}
}
# 查询具有子数据的父数据列表
GET ckiss-company-001/_search
{
"query": {
"has_child": {
"type": "child",
"query": {
"match_all": {}
}
}
}
}
# 查询具有父节点的子数据列表
GET ckiss-company-001/_search
{
"query": {
"has_parent": {
"parent_type": "father",
"query": {
"match_all": {}
}
}
}
}
Nested 嵌套键值对
案例练习:
- 假设一个公司有很多区域信息,每一组都是一个键值对,包括省份与城市
- 错误的案例查询使用array类型
错误情况
PUT ckiss-company-002
{
"mappings": {
"properties": {
"companyId": {
"type": "integer"
},
"companyName": {
"type": "keyword"
},
"area": {
"properties": {
"province": {
"type": "keyword"
},
"city": {
"type": "keyword"
}
}
}
}
}
}
# 填充数组
PUT ckiss-company-002/_doc/1
{
"companyId": 1,
"companyName": "华住集团",
"area": [
{
"province": "HN",
"city": "CS"
},
{
"province": "ZJ",
"city": "HZ"
}
]
}
# 按照省份与城市查询
GET ckiss-company-002/_search
{
"query": {
"bool": {
"must": [
{
"term": {
"area.province": {
"value": "HN"
}
}
},
{
"term": {
"area.city": {
"value": "HZ"
}
}
}
]
}
}
}
响应结果有问题:
"hits" : [
{
"_index" : "ckiss-company-002",
"_type" : "_doc",
"_id" : "1",
"_score" : 0.723315,
"_source" : {
"companyId" : 1,
"companyName" : "华住集团",
"area" : [
{
"province" : "HN",
"city" : "CS"
},
{
"province" : "ZJ",
"city" : "HZ"
}
]
}
}
]
正确做法
- 使用nested 类型
- 在area字段下增加 type=nested
- 查询语法中增加 nested 标记 ```json DELETE ckiss-company-002
PUT ckiss-company-002 { “mappings”: { “properties”: { “companyId”: { “type”: “integer” }, “companyName”: { “type”: “keyword” }, “area”: { “type”: “nested”, “properties”: { “province”: { “type”: “keyword” }, “city”: { “type”: “keyword” } } } } } }
造数据
```json
PUT ckiss-company-002/_doc/1
{
"companyId": 1,
"companyName": "华住集团",
"area": [
{
"province": "HN",
"city": "CS"
},
{
"province": "ZJ",
"city": "HZ"
}
]
}
GET ckiss-company-002/_search
{
"query": {
"nested": {
"path": "area",
"query": {
"bool": {
"must": [
{
"term": {
"area.province": {
"value": "HN"
}
}
},
{
"term": {
"area.city": {
"value": "HZ"
}
}
}
]
}
}
}
}
}
无响应内容
字段别名
- alias: 字段类型关键字
- path:设定别名代理的字段
案例:假定基本信息,其中部分字段原来定义的是一个意思,后续又改变了
// TODO
fields 单值多字段
假设需要依据一个字段提供多种不同的检索要求,fields可以解决这种问题
案例: 假定一个基本信息,公司名称既能提供精确检锁,也能提供范围查询;公司收入也需要提供精确检索和数值比对的范围检索
DELETE ckiss-company-002
PUT ckiss-company-002
{
"mappings": {
"properties": {
"companyId": {
"type": "integer"
},
"companyName": {
"type": "keyword",
"fields": {
"text": {
"type": "text"
}
}
},
"income": {
"type": "integer",
"fields": {
"sortKey": {
"type": "keyword"
}
}
}
}
}
}
PUT ckiss-company-002/_doc/1
{
"companyId": 1,
"companyName" : "华住集团",
"income": 100000
}
PUT ckiss-company-002/_doc/2
{
"companyId": 2,
"companyName" : "美团集团",
"income": 1000
}
# 范围查询
GET ckiss-company-002/_search
{
"query": {
"bool": {
"filter": {
"range": {
"income": {
"gte": 1000,
"lte": 20000
}
}
}
}
}
}
# 键值查询
GET ckiss-company-002/_search
{
"query": {
"bool": {
"filter": {
"term": {
"income.sortKey": "1000"
}
}
}
}
}
Copy_to 字段复制
- 原始值 ,可以指定复制多个字段,且按照顺序复制
- 历史版本是_all字段,后来取消;增加了copy_to 弥补
- copy_to,数据字段关键字
案例:
假定公司基本信息有地址,地址信息需要省份、城市、区县、等组成
DELETE ckiss-company-002
PUT ckiss-company-002
{
"mappings": {
"properties": {
"companyId": {
"type": "integer"
},
"companyName": {
"type": "keyword"
},
"province": {
"type": "text",
"copy_to": "address"
},
"city": {
"type": "text",
"copy_to": "address"
},
"county":{
"type": "text",
"copy_to": "address"
},
"address": {
"type": "text"
}
}
}
}
PUT ckiss-company-002/_doc/1
{
"companyId": 1,
"companyName": "美团集团",
"province": "HN",
"city": "CS",
"county": "Yuelu"
}
PUT ckiss-company-002/_doc/2
{
"companyId": 2,
"companyName": "美团集团",
"province": "HN",
"city": "CS",
"county": "Yuelu",
"address": "Hello"
}
GET ckiss-company-002/_search
{
"query": {
"match": {
"address": "Hello"
}
}
}