• ES基础(四十一)对象及Nested对象


     

     

     

     

     

     

     

     

     

     

    课程demos

    DELETE blog
    # 设置blog的 Mapping
    PUT /blog
    {
      "mappings": {
        "properties": {
          "content": {
            "type": "text"
          },
          "time": {
            "type": "date"
          },
          "user": {
            "properties": {
              "city": {
                "type": "text"
              },
              "userid": {
                "type": "long"
              },
              "username": {
                "type": "keyword"
              }
            }
          }
        }
      }
    }
    
    
    # 插入一条 Blog 信息
    PUT blog/_doc/1
    {
      "content":"I like Elasticsearch",
      "time":"2019-01-01T00:00:00",
      "user":{
        "userid":1,
        "username":"Jack",
        "city":"Shanghai"
      }
    }
    
    
    # 查询 Blog 信息
    POST blog/_search
    {
      "query": {
        "bool": {
          "must": [
            {"match": {"content": "Elasticsearch"}},
            {"match": {"user.username": "Jack"}}
          ]
        }
      }
    }
    
    
    DELETE my_movies
    
    # 电影的Mapping信息
    PUT my_movies
    {
          "mappings" : {
          "properties" : {
            "actors" : {
              "properties" : {
                "first_name" : {
                  "type" : "keyword"
                },
                "last_name" : {
                  "type" : "keyword"
                }
              }
            },
            "title" : {
              "type" : "text",
              "fields" : {
                "keyword" : {
                  "type" : "keyword",
                  "ignore_above" : 256
                }
              }
            }
          }
        }
    }
    
    
    # 写入一条电影信息
    POST my_movies/_doc/1
    {
      "title":"Speed",
      "actors":[
        {
          "first_name":"Keanu",
          "last_name":"Reeves"
        },
    
        {
          "first_name":"Dennis",
          "last_name":"Hopper"
        }
    
      ]
    }
    
    # 查询电影信息
    POST my_movies/_search
    {
      "query": {
        "bool": {
          "must": [
            {"match": {"actors.first_name": "Keanu"}},
            {"match": {"actors.last_name": "Hopper"}}
          ]
        }
      }
    
    }
    
    DELETE my_movies
    # 创建 Nested 对象 Mapping
    PUT my_movies
    {
          "mappings" : {
          "properties" : {
            "actors" : {
              "type": "nested",
              "properties" : {
                "first_name" : {"type" : "keyword"},
                "last_name" : {"type" : "keyword"}
              }},
            "title" : {
              "type" : "text",
              "fields" : {"keyword":{"type":"keyword","ignore_above":256}}
            }
          }
        }
    }
    
    
    POST my_movies/_doc/1
    {
      "title":"Speed",
      "actors":[
        {
          "first_name":"Keanu",
          "last_name":"Reeves"
        },
    
        {
          "first_name":"Dennis",
          "last_name":"Hopper"
        }
    
      ]
    }
    
    # Nested 查询
    POST my_movies/_search
    {
      "query": {
        "bool": {
          "must": [
            {"match": {"title": "Speed"}},
            {
              "nested": {
                "path": "actors",
                "query": {
                  "bool": {
                    "must": [
                      {"match": {
                        "actors.first_name": "Keanu"
                      }},
    
                      {"match": {
                        "actors.last_name": "Hopper"
                      }}
                    ]
                  }
                }
              }
            }
          ]
        }
      }
    }
    
    
    # Nested Aggregation
    POST my_movies/_search
    {
      "size": 0,
      "aggs": {
        "actors": {
          "nested": {
            "path": "actors"
          },
          "aggs": {
            "actor_name": {
              "terms": {
                "field": "actors.first_name",
                "size": 10
              }
            }
          }
        }
      }
    }
    
    
    # 普通 aggregation不工作
    POST my_movies/_search
    {
      "size": 0,
      "aggs": {
        "NAME": {
          "terms": {
            "field": "actors.first_name",
            "size": 10
          }
        }
      }
    }

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

  • 相关阅读:
    BZOJ 1823: [JSOI2010]满汉全席 [2-SAT]
    POJ 3683 Priest John's Busiest Day[2-SAT 构造解]
    Centos出现-bash: unzip: command not found的解决办法
    物理cpu与逻辑cpu概述
    如何正确查看Linux机器内存使用情况
    connections java.net.BindException: Address already in use_解决方案
    Linux查看端口、进程情况及kill进程
    linux如何查看端口被哪个进程占用?
    如何正确查看Linux机器内存使用情况
    TPS和事务响应时间的关系
  • 原文地址:https://www.cnblogs.com/qiu-hua/p/14197850.html
Copyright © 2020-2023  润新知