PUT bank/_bulk
{"index":{"_id":"1"}}
{"account_number":1,"balance":39225,"firstname":"Amber","lastname":"杜克","age":32,"gender":"M","address":"880 Holmes Lane","employer":"Pyrami","email":"[email protected]","city":"Brogan","state":"IL","obj1":{"name":"blue"}}
{"index":{"_id":"6"}}
{"account_number":6,"balance":5686,"firstname":"Hattie","lastname":"伯纳德","age":36,"gender":"M","address":"671 Bristol Street","employer":"Netagy","email":"[email protected]","city":"Dante","state":"TN","obj1":{"name":"green"}}
{"index":{"_id":"13"}}
{"account_number":13,"balance":32838,"firstname":"Nanette","lastname":"巴特斯","age":28,"gender":"F","address":"789 Madison Street","employer":"Quility","email":"[email protected]","city":"Nogal","state":"VA","obj1":{"name":"blue"}}
{"index":{"_id":"18"}}
{"account_number":18,"balance":4180,"firstname":"Dale","lastname":"奥达曼斯","age":33,"gender":"M","address":"467 Hutchinson Court","employer":"Boink","email":"[email protected]","city":"Orick","state":"MD"}
数据使用https://raw.githubusercontent.com/elastic/elasticsearch/master/docs/src/test/resources/accounts.json
我在索引中新加了"obj1" : {“type” : “nested”} 类型;
bool查询 must 必须 匹配gender为M并且state为MD的数据
GET bank/_search
{"query": {"bool": {"must": [
{"match": {
"gender": "M"
}},{"match": {
"state": "MD"
}}
]}}}
如果我需要查询gender 为M 或者 gender为F的数据
GET bank/_search
{"query": {"bool": {"should": [
{"match": {
"gender": "F"
}} ,{"match": {
"gender": "M"
}}
]}
}
}
must 和should组合使用
GET bank/_search
{"query": {"bool": {"must": [
{"match": {
"gender": "M"
}},{"bool": {"should": [
{"match": {
"gender": "F"
}} ,{"match": {
"gender": "M"
}}
]}}
]}}}
agg分组
GET bank/_search
{"query": {"match_all": {}},"aggs": {
"age_avg": {
"avg": {
"field": "age"
}
}
},"size": 0}
nested 查询
GET bank/_search
{"query": {"nested": {
"path": "obj1",
"query": {"bool": {"must": [
{"exists": {"field" : "obj1" }}
]}}
}} }
script_field 查询
GET bank/_search
{"query": {"bool": {"must": [
{"match_all": {}}
]}},"script_fields": {
"firstname_length": {
"script": {"lang": "painless",
"source": "doc['firstname'].length"}
}
},"_source": "*"}
想要计算下firstname 的长度 结果报错
"reason": "Fielddata is disabled on text fields by default. Set fielddata=true on [firstname] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory. Alternatively use a keyword field instead."
英文firstname 为text类型 进行了分词 我们换成keyword字段试一试
GET bank/_search
{"query": {"bool": {"must": [
{"match_all": {}}
]}},"script_fields": {
"email_length": {
"script": {"lang": "painless",
"source": "doc['email'].length"}
}
},"_source": "*"}
随便用一个keyword字段email试一下 结果是成功的
但是好像返回长度都为1。。。
script的api可以访问链接 link.