Elasticsearch笔记第二十篇

119 阅读2分钟

Elasticsearch核心知识篇(46)

初识搜索引擎_mapping复杂数据类型以及object类型数据底层结构大揭秘

multivalue field

 { "tags": [ "tag1", "tag2" ]}
  • 建立索引时与string是一样的,数据类型不能混

empty field

 null,[],[null]

object field

 PUT /company/employee/1
 {
   "address": {
     "country": "china",
     "province": "guangdong",
     "city": "guangzhou"
   },
   "name": "jack",
   "age": 27,
   "join_date": "2017-01-01"
 }
 
 GET /company/_mapping/employee
 # address:object类型
 
 {
   "company": {
     "mappings": {
       "employee": {
         "properties": {
           "address": {
             "properties": {
               "city": {
                 "type": "text",
                 "fields": {
                   "keyword": {
                     "type": "keyword",
                     "ignore_above": 256
                   }
                 }
               },
               "country": {
                 "type": "text",
                 "fields": {
                   "keyword": {
                     "type": "keyword",
                     "ignore_above": 256
                   }
                 }
               },
               "province": {
                 "type": "text",
                 "fields": {
                   "keyword": {
                     "type": "keyword",
                     "ignore_above": 256
                   }
                 }
               }
             }
           },
           "age": {
             "type": "long"
           },
           "join_date": {
             "type": "date"
           },
           "name": {
             "type": "text",
             "fields": {
               "keyword": {
                 "type": "keyword",
                 "ignore_above": 256
               }
             }
           }
         }
       }
     }
   }
 }
  • 底层数据存储
 {
   "address": {
     "country": "china",
     "province": "guangdong",
     "city": "guangzhou"
   },
   "name": "jack",
   "age": 27,
   "join_date": "2017-01-01"
 }
 
 # 数据底层存储
 {
     "name":            [jack],
     "age":          [27],
     "join_date":      [2017-01-01],
     "address.country":         [china],
     "address.province":   [guangdong],
     "address.city":  [guangzhou]
 }
 
 # 更加复杂一点数据
 {
     "authors": [
         { "age": 26, "name": "Jack White"},
         { "age": 55, "name": "Tom Jones"},
         { "age": 39, "name": "Kitty Smith"}
     ]
 }
 
 {
     "authors.age":    [26, 55, 39],
     "authors.name":   [jack, white, tom, jones, kitty, smith]
 }

Elasticsearch核心知识篇(47)

初识搜索引擎_search api的基础语法介绍

search api的基本语法

 # 前面已经使用过了,不在详解
 GET /_search
 {}
 
 GET /index1,index2/type1,type2/_search
 {}
 
 GET /_search
 {
   "from": 0,
   "size": 10
 }

http协议中get是否可以带上request body

HTTP协议,一般不允许get请求带上request body,但是因为get更加适合描述查询数据的操作,因此还是这么用了

 GET /_search?from=0&size=10
 
 POST /_search
 {
   "from":0,
   "size":10
 }

碰巧,很多浏览器,或者是服务器,也都支持GET+request body模式

如果遇到不支持的场景,也可以用POST /_search

Elasticsearch核心知识篇(48)

初识搜索引擎_快速上机动手实战Query DSL搜索语法

一个例子让你明白什么是Query DSL

 GET /_search
 {
     "query": {
         "match_all": {}
     }
 }

Query DSL的基本语法

 {
     QUERY_NAME: {
         ARGUMENT: VALUE,
         ARGUMENT: VALUE,...
     }
 }
 
 {
     QUERY_NAME: {
         FIELD_NAME: {
             ARGUMENT: VALUE,
             ARGUMENT: VALUE,...
         }
     }
 }

示例:

 GET /waws_index/waws_type/_search 
 {
   "query": {
     "match": {
       "test_field": "test"
     }
   }
 }

如何组合多个搜索条件

搜索需求:title必须包含elasticsearch,content可以包含elasticsearch也可以不包含,author_id必须不为111

  • must : 必须匹配
  • should : 可以匹配也可以不进行匹配
  • must not : 不能包含

重新生成一批数据

 PUT /waws_data/waws_type/1
 {
   "title": "my hadoop article",
   "content": "hadoop is very bad",
   "author_id": 111
 }
 
 PUT /waws_data/waws_type/2
 {
   "title": "my elasticsearch article",
   "content": "es is very bad",
   "author_id": 110
 }
 
 PUT /waws_data/waws_type/3
 {
   "title": "my elasticsearch article",
   "content": "es is very goods",
   "author_id": 111
 }
 
 GET /waws_data/waws_type/_search
 
 {
   "took": 1,
   "timed_out": false,
   "_shards": {
     "total": 5,
     "successful": 5,
     "failed": 0
   },
   "hits": {
     "total": 3,
     "max_score": 1,
     "hits": [
       {
         "_index": "waws_data",
         "_type": "waws_type",
         "_id": "2",
         "_score": 1,
         "_source": {
           "title": "my elasticsearch article",
           "content": "es is very goods",
           "author_id": 111
         }
       },
       {
         "_index": "waws_data",
         "_type": "waws_type",
         "_id": "1",
         "_score": 1,
         "_source": {
           "title": "my hadoop article",
           "content": "hadoop is very bad",
           "author_id": 111
         }
       },
       {
         "_index": "waws_data",
         "_type": "waws_type",
         "_id": "3",
         "_score": 1,
         "_source": {
           "title": "my elasticsearch article",
           "content": "es is very goods",
           "author_id": 111
         }
       }
     ]
   }
 }

满足上面需求的查找语句

 GET /waws_data/waws_type/_search
 {
   "query": {
     "bool": {
       "must": [
         {
           "match": {
             "title": "elasticsearch"
           }
         }
       ],
       "should": [
         {
           "match": {
             "content": "elasticsearch"
           }
         }
       ],
       "must_not": [
         {
           "match": {
             "author_id": 111
           }
         }
       ]
     }
   }
 }
 
 {
   "took": 1,
   "timed_out": false,
   "_shards": {
     "total": 5,
     "successful": 5,
     "failed": 0
   },
   "hits": {
     "total": 1,
     "max_score": 0.25316024,
     "hits": [
       {
         "_index": "waws_data",
         "_type": "waws_type",
         "_id": "3",
         "_score": 0.25316024,
         "_source": {
           "title": "my elasticsearch article",
           "content": "es is very goods",
           "author_id": 110
         }
       }
     ]
   }
 }
  • 复杂的查询语句
 GET /test_index/_search
 {
     "query": {
             "bool": {
                 "must": { "match":   { "name": "tom" }},
                 "should": [
                     { "match":       { "hired": true }},
                     { "bool": {
                         "must":      { "match": { "personality": "good" }},
                         "must_not":  { "match": { "rude": true }}
                     }}
                 ],
                 "minimum_should_match": 1   # 至少要匹配到一条
             }
     }
 }