Elasticsearch-关于数组的简单使用

前言

本文基于elasticsearch7.3.0版本

实例

# 创建索引
PUT my_index
{
    
    
  "mappings": {
    
    
    "properties": {
    
    
      "company":{
    
    
        "type": "keyword"
      }
    }
  }
}

# 添加数据
PUT my_index/_doc/1
{
    
    
  "company":"alibaba"
}

# 添加数据
PUT my_index/_doc/2
{
    
    
  "company":["baidu","tengxun"]
}

简单查询

GET my_index/_search
{
    
    
  "query": {
    
    
    "term": {
    
    
      "company": {
    
    
        "value": "baidu"
      }
    }
  }
}

# 响应
{
    
    
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    
    
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    
    
    "total" : {
    
    
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 0.8025915,
    "hits" : [
      {
    
    
        "_index" : "my_index",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 0.8025915,
        "_source" : {
    
    
          "company" : [
            "baidu",
            "tengxun"
          ]
        }
      }
    ]
  }
}

使用脚本对数组长度进行过滤

GET my_index/_search
{
    
    
  "query": {
    
    
    "bool": {
    
    
      "filter": {
    
    
        "script": {
    
    
          "script": {
    
    
            "source": "doc['company'].length == 2",
            "lang": "painless"
          }
        }
      }
    }
  }
}

# 结果
{
    
    
  "took" : 6,
  "timed_out" : false,
  "_shards" : {
    
    
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    
    
    "total" : {
    
    
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 0.0,
    "hits" : [
      {
    
    
        "_index" : "my_index",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 0.0,
        "_source" : {
    
    
          "company" : [
            "baidu",
            "tengxun"
          ]
        }
      }
    ]
  }
}

对company进行terms聚合

GET my_index/_search
{
    
    
  "size": 0, 
  "aggs": {
    
    
    "company_terms": {
    
    
      "terms": {
    
    
        "field": "company",
        "size": 10
      }
    }
  }
}

# 响应
{
    
    
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
    
    
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    
    
    "total" : {
    
    
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "aggregations" : {
    
    
    "company_terms" : {
    
    
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      // 注意这里的结果是三条,不是两条
      "buckets" : [
        {
    
    
          "key" : "alibaba",
          "doc_count" : 1
        },
        {
    
    
          "key" : "baidu",
          "doc_count" : 1
        },
        {
    
    
          "key" : "tengxun",
          "doc_count" : 1
        }
      ]
    }
  }
}

对数组长度进行聚合

GET my_index/_search
{
    
    
  "size": 0,
  "aggs": {
    
    
    "vendorCode_terms": {
    
    
      "terms": {
    
    
        "script": {
    
    
          "source": "doc['company'].length",
          "lang": "painless"
        }
      }
    }
  }
}

# 响应
{
    
    
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    
    
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    
    
    "total" : {
    
    
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "aggregations" : {
    
    
    "vendorCode_terms" : {
    
    
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
    
    
          "key" : "1",
          "doc_count" : 1
        },
        {
    
    
          "key" : "2",
          "doc_count" : 1
        }
      ]
    }
  }
}

去重

GET my_index/_search
{
    
    
  "size": 0, 
  "aggs": {
    
    
    "company_terms": {
    
    
      "cardinality": {
    
    
        "field": "company"
      }
    }
  }
}

# 响应
{
    
    
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
    
    
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    
    
    "total" : {
    
    
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "aggregations" : {
    
    
  	 // 注意这里的结果是三条,不是两条
    "company_terms" : {
    
    
      "value" : 3
    }
  }
}

猜你喜欢

转载自blog.csdn.net/qq_28988969/article/details/104017568