es - elasticsearch mapping - meta field - 2

127 阅读1分钟

世界上并没有完美的程序,但是我们并不因此而沮丧,因为写程序就是一个不断追求完美的过程。

问 :_index有哪些特点?
答 :
在这里插入图片描述
问 :_index如何使用?
答 :

# _index
POST /index_1/_doc/1
{
  "my_idx" : "document index 1"
}

POST /index_2/_doc/2
{
  "my_idx" : "document index 2"
}

# 搜索 支持查询 聚合 排序 脚本
GET /index_1,index_2/_search
{
  "query" : {
    "terms" : {
      "_index" : ["index_1", "index_2"]
    }
  },
  "aggs" : {
    "indices" : {
      "terms" : {
        "field" : "_index",
        "size"  : 10
      }
    }
  },
  "sort" : {
    "_index" : {
      "order" : "desc"
    }
  },
  "script_fields" : {
    "index_name" : {
      "script" : {
        "lang"   : "painless",
        "source" : "doc['_index']"
      }
    }
  }
}

# 结果
{
  "took" : 48,
  "timed_out" : false,
  "_shards" : {
    "total" : 2,
    "successful" : 2,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [
      {
        "_index" : "index_2",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : null,
        "fields" : {
          "index_name" : [
            "index_2"
          ]
        },
        "sort" : [
          "index_2"
        ]
      },
      {
        "_index" : "index_1",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : null,
        "fields" : {
          "index_name" : [
            "index_1"
          ]
        },
        "sort" : [
          "index_1"
        ]
      }
    ]
  },
  "aggregations" : {
    "indices" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : "index_1",
          "doc_count" : 1
        },
        {
          "key" : "index_2",
          "doc_count" : 1
        }
      ]
    }
  }
}