elasticsearch笔记

elasticsearch笔记,第1张

elasticsearch笔记

文章目录
      • 索引
        • 创建索引
        • 检索索引
        • 基本搜索
        • DSL搜索

索引
  • 创建索引 index/type/id
    index = 关系数据库database
    type = 关系型数据库table
    id = 关系型数据库主键id 必须唯一 不指定则自动生成
    PUT /index/type/1
    {
        "first_name" : "John",
        "last_name" :  "Smith",
        "age" :        25,
        "about" :      "I love to go rock climbing",
        "interests": [ "sports", "music" ]
    }
    
  • 检索索引
    GET index/type/1
    {
    	  "_index" :   "megacorp", //索引
    	  "_type" :    "employee", //索引type
    	  "_id" :      "1", //索引id
    	  "_version" : 1,   //修改次数
    	  "found" :    true, //是否找到
    	  "_source" :  {   //内容
    		  "first_name" :  "John",
    		  "last_name" :   "Smith",
    		  "age" :         25,
    		  "about" :       "I love to go rock climbing",
    		  "interests":  [ "sports", "music" ]
    	  }
    }
    
  • 基本搜索
    GET index/type/_search  全文搜索
    GET index/type/_search?q=last_name:Smith  query_string 字符串搜索
    {
        "took": 1,
        "timed_out": false,
        "_shards": {
            "total": 2,
            "successful": 2,
            "failed": 0
        },
        "hits": {
            "total": 1,
            "max_score": 1.0,
            "hits": [
                {
                    "_index": "test_database",
                    "_type": "test_table1",
                    "_id": "_search",
                    "_score": 1.0,
                    "_source": {
                        "first_name": "John",
                        "last_name": "Smith",
                        "age": 25,
                        "about": "I love to go rock climbing",
                        "interests": [
                            "sports",
                            "music"
                        ]
                    }
                }
            ]
        }
    }
    
  • DSL搜索 搜索last_name为smith的
      	GET /megacorp/employee/_search  
      	{
      	  "query" : {
      	      "match" : {
      	          "last_name" : "Smith"
      	      }
      	  	}
      	}
    
    搜索last_name为smith 切年龄大于30的
    	GET /megacorp/employee/_search 
        {
          "query" : {
              "bool": {
                  "must": {
                      "match" : {
                          "last_name" : "smith" 
                      }
                  },
                  "filter": {
                      "range" : {
                          "age" : { "gt" : 30 } 
                      }
                  }
              }
          }
      }
    
    短语搜索
    GET /megacorp/employee/_search  完全匹配搜索的短语
    {
        "query" : {
            "match_phrase" : {
                "about" : "rock climbing"
            }
        }
    }
    
    聚合搜索
      {
        "aggs": {
      	    "all_interests": {
      	      "terms": { "field": "interests" }
          	}
        }
      }
      {
     ...
        "hits": { ... },
         "aggregations": {
            "all_interests": {
               "buckets": [
                  {
                     "key":       "music",
                     "doc_count": 2
                  },
                  {
                     "key":       "forestry",
                     "doc_count": 1
                  },
                  {
                     "key":       "sports",
                     "doc_count": 1
                  }
               ]
            }
         }
      }
    

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5635368.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存