Elastic search查询
建模和准备数据
由于赛事机构的参加人员较多,且使用比较频繁,为了避免单一索引引起频繁操作,这里我们构建动态索引。索引的命名规则为:mock_data3_ + agencyId.
为了保证检索的效率,采用冗余的模型。在document中,建立nested的类型。(其原因如下:1. 对业务使用场景来说,对查询的效率要求较高;使用parent child的关联,其效率为单一文档的十分之一左右;2. 冗余的数据,在elastic search中保存在磁盘,都经过压缩,且磁盘空间代价较小;3. 对较大的文档,可能在建立index的耗时相对较长,这是业务可以接受的情形。)
相关的配置如下:
- 配置命令
PUT mock_data3_180103
{
"mappings": {
"doc": {
"properties": {
"registrations": {
"type": "nested"
}
}
}
}
}
- 建立index
PUT mock_data3_180103/doc/08C0B894-5CEE-4787-9DBB-00014256B7CE
{
"agencyId": 180103,
"addOnRevenue": 0.00,
"openRate": 0,
"emailRevenue": 0.00,
"lastName":"Allabach",
"address":{
"city":"Centreville",
"postalCode":"49032",
"stateProvince":"MI",
"line1":"27095 Marvin Rd"
},
"registrations": [
{
"eventName": "color run 2018",
"priceCategoryName": "run 12k",
"eventId": 12323,
"priceCategoryId": 456,
"id":123,
"orderNumber": "C-0000119C",
"registrationNumber": "R-00000DYB",
"age": 18,
"eventStartDate": "2009-07-20 20:07:52",
"eventEndDate": "2018-07-21 20:07:52",
"registrationDate": "2009-07-21 20:0:52",
"activeUrl": "http://www.active.com",
"location": "test location",
"division": "division",
"bib": "111111111111111111111",
"wave": "tessdsdsds",
"category": "registration category",
"sport": "Running",
"distance": "Marathon"
},
{
"eventName": "eventName",
"priceCategoryName": "priceCategoryName",
"eventId": 2382323,
"priceCategoryId": 32322,
"id":456,
"orderNumber": "C-00002323C",
"registrationNumber": "R-089DYB",
"age": 20,
"eventStartDate": "2010-07-20 20:07:52",
"eventEndDate": "2018-07-21 20:07:52",
"registrationDate": "2009-07-21 20:0:52",
"activeUrl": "http://www.active.com",
"location": "test location",
"division": "division",
"bib": "111111111111111111111",
"wave": "tessdsdsds",
"teamId": 123456,
"teamName": "test team",
"teamCaptainName": "test team captain",
"category": "registration category",
"sport": "Running",
"distance": "5K"
}
],
"gender":"FEMALE",
"registrationRevenue": 29.99,
"clickedCount":0,
"clickedRate": 0,
"dateOfBirth":"1975-09-11",
"stars": 3,
"sendCount":1,
"engagementRate":"F",
"firstName":"Shellie",
"revenue": 29.99,
"phoneNumber":"2697182152",
"registrationCount":1,
"openCount":0,
"email":"MSSQLSERVERB128D6A6@null.activenetwork.com",
"agencyName": "the color run"
}
Elastic search查询相关命令
简单的嵌套查询
GET mock_data3_180103/_search
{
"query": {
"nested": {
"path": "registrations",
"query": {
"bool": {
"must": [
{ "match": { "registrations.eventName": "color run 2018" }},
{ "match": { "registrations.eventId": 123232112 }}
]
}
}
}
}
}
布尔过滤器编辑
一个 bool 过滤器由三部分组成:
{
"bool" : {
"must" : [],
"should" : [],
"must_not" : [],
}
}
must
所有的语句都 必须(must) 匹配,与 AND 等价。
must_not
所有的语句都 不能(must not) 匹配,与 NOT 等价。
should
至少有一个语句要匹配,与 OR 等价。
范围
Elasticsearch 有 range 查询
gt: > 大于(greater than)
lt: < 小于(less than)
gte: >= 大于或等于(greater than or equal to)
lte: <= 小于或等于(less than or equal to)
缺失查询编辑
这个 missing 查询本质上与 exists 恰好相反: 它返回某个特定 无 值字段的文档,与以下 SQL 表达的意思类似:
GET /my_store/products/_search
{
"query" : {
"filtered" : {
"filter" : {
"bool" : {
"should" : [
{ "term" : {"price" : 20}},
{ "term" : {"productID" : "XHDK-A-1293-#fJ3"}}
],
"must_not" : {
"term" : {"price" : 30}
}
}
}
}
}
}
案列
- 查询满足以下条件:
eventId= 2382323
stars between 3 to 30
first name like Shellie
GET mock_data3_180103/_search
{
"query": {
"bool": {
"must": [
{"nested": {
"path": "registrations",
"query": {
"bool": {
"must": [
{ "match": { "registrations.eventId": 2382323 }}
]
}
}
}
},
{"match": {
"firstName": {
"query": "Shellie",
"type": "phrase"
}
}
},
{"range": {
"stars": {
"gte": 3,
"lte": 30
}
}
}
]
}
}
}