• ES基础(四十六)Elasticsearch 数据建模最佳实践


     

     

     

     

     

     

     

     

     

    课程demo

    ###### Cookie Service
    
    ##索引数据,dynamic mapping 会不断加入新增字段
    PUT cookie_service/_doc/1
    {
     "url":"www.google.com",
     "cookies":{
       "username":"tom",
       "age":32
     }
    }
    
    PUT cookie_service/_doc/2
    {
     "url":"www.amazon.com",
     "cookies":{
       "login":"2019-01-01",
       "email":"xyz@abc.com"
     }
    }
    
    
    DELETE cookie_service
    #使用 Nested 对象,增加key/value
    PUT cookie_service
    {
      "mappings": {
        "properties": {
          "cookies": {
            "type": "nested",
            "properties": {
              "name": {
                "type": "keyword"
              },
              "dateValue": {
                "type": "date"
              },
              "keywordValue": {
                "type": "keyword"
              },
              "IntValue": {
                "type": "integer"
              }
            }
          },
          "url": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          }
        }
      }
    }
    
    
    ##写入数据,使用key和合适类型的value字段
    PUT cookie_service/_doc/1
    {
     "url":"www.google.com",
     "cookies":[
        {
          "name":"username",
          "keywordValue":"tom"
        },
        {
           "name":"age",
          "intValue":32
    
        }
    
       ]
     }
    
    
    PUT cookie_service/_doc/2
    {
     "url":"www.amazon.com",
     "cookies":[
        {
          "name":"login",
          "dateValue":"2019-01-01"
        },
        {
           "name":"email",
          "IntValue":32
    
        }
    
       ]
     }
    
    
    # Nested 查询,通过bool查询进行过滤
    POST cookie_service/_search
    {
      "query": {
        "nested": {
          "path": "cookies",
          "query": {
            "bool": {
              "filter": [
                {
                "term": {
                  "cookies.name": "age"
                }},
                {
                  "range":{
                    "cookies.intValue":{
                      "gte":30
                    }
                  }
                }
              ]
            }
          }
        }
      }
    }
    
    
    
    # 在Mapping中加入元信息,便于管理
    PUT softwares/
    {
      "mappings": {
        "_meta": {
          "software_version_mapping": "1.0"
        }
      }
    }
    
    GET softwares/_mapping
    PUT softwares/_doc/1
    {
      "software_version":"7.1.0"
    }
    
    DELETE softwares
    # 优化,使用inner object
    PUT softwares/
    {
      "mappings": {
        "_meta": {
          "software_version_mapping": "1.1"
        },
        "properties": {
          "version": {
            "properties": {
              "display_name": {
                "type": "keyword"
              },
              "hot_fix": {
                "type": "byte"
              },
              "marjor": {
                "type": "byte"
              },
              "minor": {
                "type": "byte"
              }
            }
          }
        }
      }
    }
    
    
    #通过 Inner Object 写入多个文档
    PUT softwares/_doc/1
    {
      "version":{
      "display_name":"7.1.0",
      "marjor":7,
      "minor":1,
      "hot_fix":0  
      }
    
    }
    
    PUT softwares/_doc/2
    {
      "version":{
      "display_name":"7.2.0",
      "marjor":7,
      "minor":2,
      "hot_fix":0  
      }
    }
    
    PUT softwares/_doc/3
    {
      "version":{
      "display_name":"7.2.1",
      "marjor":7,
      "minor":2,
      "hot_fix":1  
      }
    }
    
    
    # 通过 bool 查询,
    POST softwares/_search
    {
      "query": {
        "bool": {
          "filter": [
            {
              "match":{
                "version.marjor":7
              }
            },
            {
              "match":{
                "version.minor":2
              }
            }
    
          ]
        }
      }
    }
    
    
    
    
    # Not Null 解决聚合的问题
    DELETE ratings
    PUT ratings
    {
      "mappings": {
          "properties": {
            "rating": {
              "type": "float",
              "null_value": 1.0
            }
          }
        }
    }
    
    
    PUT ratings/_doc/1
    {
     "rating":5
    }
    PUT ratings/_doc/2
    {
     "rating":null
    }
    
    
    POST ratings/_search
    POST ratings/_search
    {
      "size": 0,
      "aggs": {
        "avg": {
          "avg": {
            "field": "rating"
          }
        }
      }
    }
    
    POST ratings/_search
    {
      "query": {
        "term": {
          "rating": {
            "value": 1
          }
        }
      }
    }

    本文来自博客园,作者:秋华,转载请注明原文链接:https://www.cnblogs.com/qiu-hua/p/14198006.html

  • 相关阅读:
    asp.net 内部重定向
    配置元素customErrors
    路由配置4-伪静态
    路由配置3
    路由配置2(转)
    ASP.NET实现图片防盗链(转)
    global中拦截404错误的实现方法
    使用一个HttpModule拦截Http请求,来检测页面刷新(F5或正常的请求)
    HttpApplication事件执行顺序(转)
    Android Studio NDK 新手教程(5)--Java对象的传递与改动
  • 原文地址:https://www.cnblogs.com/qiu-hua/p/14198006.html
Copyright © 2020-2023  润新知