緩存
POST /school/student/_bulk
{ "index": { "_id": 1 }}
{ "name" : "liubei", "age" : 20 , "sex": "boy", "birth": "1996-01-02" , "about": "i like diaocan he girl" }
{ "index": { "_id": 2 }}
{ "name" : "guanyu", "age" : 21 , "sex": "boy", "birth": "1995-01-02" , "about": "i like diaocan" }
{ "index": { "_id": 3 }}
{ "name" : "zhangfei", "age" : 18 , "sex": "boy", "birth": "1998-01-02" , "about": "i like travel" }
{ "index": { "_id": 4 }}
{ "name" : "diaocan", "age" : 20 , "sex": "girl", "birth": "1996-01-02" , "about": "i like travel and sport" }
{ "index": { "_id": 5 }}
{ "name" : "panjinlian", "age" : 25 , "sex": "girl", "birth": "1991-01-02" , "about": "i like travel and wusong" }
{ "index": { "_id": 6 }}
{ "name" : "caocao", "age" : 30 , "sex": "boy", "birth": "1988-01-02" , "about": "i like xiaoqiao" }
{ "index": { "_id": 7 }}
{ "name" : "zhaoyun", "age" : 31 , "sex": "boy", "birth": "1997-01-02" , "about": "i like travel and music" }
{ "index": { "_id": 8 }}
{ "name" : "xiaoqiao", "age" : 18 , "sex": "girl", "birth": "1998-01-02" , "about": "i like caocao" }
{ "index": { "_id": 9 }}
{ "name" : "daqiao", "age" : 20 , "sex": "girl", "birth": "1996-01-02" , "about": "i like travel and history" }
GET /school/student/_search?pretty
{
"query": {
"match_all": {}
}
}
問題:經過match_all匹配後,會把全部的數據檢索出來,可是每每真正的業務需求並不是要找所有的數據,而是檢索出本身想要的;而且對於es集羣來講,直接檢索所有的數據,很容易形成GC現象。因此,咱們要學會如何進行高效的檢索數據spa
GET /school/student/_search?pretty
{
"query": {
"match": {"about": "travel"}
}
}
若是此時想查詢喜歡旅遊的,而且不能是男孩的,怎麼辦?ip
【這種方式是錯誤的,由於一個match下,不能出現多個字段值[match] query doesn't support multiple fields】,須要使用複合查詢文檔
當出現多個查詢語句組合的時候,能夠用bool來包含。bool合併聚包含:must,must_not或者should, should表示or的意思字符串
例子:查詢非男性中喜歡旅行的人string
GET /school/student/_search?pretty
{
"query": {
"bool": {
"must": { "match": {"about": "travel"}},
"must_not": {"match": {"sex": "boy"}}
}
}
}
should表示無關緊要的(若是should匹配到了就展現,不然就不展現)class
例子:集羣
查詢喜歡旅行的,若是有男性的則顯示,不然不顯示date
GET /school/student/_search?pretty
{
"query": {
"bool": {
"must": { "match": {"about": "travel"}},
"should": {"match": {"sex": "boy"}}
}
}
}
使用term進行精確匹配(好比數字,日期,布爾值或 not_analyzed的字符串(未經分析的文本數據類型))數據類型
語法
{ "term": { "age": 20 }}
{ "term": { "date": "2018-04-01" }}
{ "term": { "sex": 「boy」 }}
{ "term": { "about": "trivel" }}
例子:
查詢喜歡旅行的
GET /school/student/_search?pretty
{
"query": {
"bool": {
"must": { "term": {"about": "travel"}},
"should": {"term": {"sex": "boy"}}
}}
}
GET /school/student/_search?pretty
{
"query": {
"bool": {
"must": { "terms": {"about": ["travel","history"]}}
}
}
}
term主要是用於精確的過濾好比說:」我愛你」
在match下面匹配能夠爲包含:我、愛、你、我愛等等的解析器
在term語法下面就精準匹配到:」我愛你」
Range過濾容許咱們按照指定的範圍查找一些數據:操做範圍:gt::大於,gae::大於等於,lt::小於,lte::小於等於
例子:
查找出大於20歲,小於等於25歲的學生
GET /school/student/_search?pretty
{
"query": {
"range": {
"age": {"gt":20,"lte":25}
}
}
}
}
exists和missing過濾能夠找到文檔中是否包含某個字段或者是沒有某個字段
例子:
查找字段中包含age的文檔
GET /school/student/_search?pretty
{
"query": {
"exists": {
"field": "age"
}
}
}
}
用bool也能夠像以前match同樣來過濾多行條件:
must :: 多個查詢條件的徹底匹配,至關於 and 。
must_not :: 多個查詢條件的相反匹配,至關於 not 。
should :: 至少有一個查詢條件匹配, 至關於 or
例子:
過濾出about字段包含travel而且年齡大於20歲小於30歲的同窗
GET /school/student/_search?pretty
{
"query": {
"bool": {
"must": [
{"term": {
"about": {
"value": "travel"
}
}},{"range": {
"age": {
"gte": 20,
"lte": 30
}
}}
]
}
}
}
一般複雜的查詢語句,咱們也要配合過濾語句來實現緩存,用filter語句就能夠來實現
例子:
查詢出喜歡旅行的,而且年齡是20歲的文檔
GET /school/student/_search?pretty
{
"query": {
"bool": {
"must": {"match": {"about": "travel"}},
"filter": [{"term":{"age": 20}}]
}
}
}