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
    }
  }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章