es - elasticsearch mapping - meta fields - 4

83 阅读1分钟

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

问 :_source有什么特点?
答 :
在这里插入图片描述

问 :_source如何使用?
答 :

# _source
# 禁用 _source - 不推荐
PUT /source_test_1
{
  "mappings" : {
    "_source" : {
      "enabled" : false
    }
  }
}

# 索引
POST /source_test_1/_doc/1
{
  "name" : "hello"
}

# 可以查询到,但是不显示内容
GET /source_test_1/_search
{
  "query" : {
    "term": {
      "name.keyword" : {
        "value" : "hello"
      }
    }
  }
}

# 结果
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 0.2876821,
    "hits" : [
      {
        "_index" : "source_test_1",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.2876821
      }
    ]
  }
}

# 指定索引存储字段 - 不推荐
PUT /source_test_2
{
  "mappings" : {
    "_source" : {
      "includes" : ["user.*", "custom.*"],
      "excludes" : ["*.age"]
    }
  }
}

# 索引
POST /source_test_2/_doc/2
{
  "user" : {
    "id"   : "1",
    "name" : "hello",
    "age"  : 18,
    "date" : "2021-03-05"
  },
  "custom" : {
    "name" : "good"
  }
}

# 指定字段索引后的搜索
GET /source_test_2/_search
{
  "query" : {
    "match_all" : {}
  }
}

# 结果
{
  "took" : 10,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "source_test_2",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 1.0,
        "_source" : {
          "custom" : {
            "name" : "good"
          },
          "user" : {
            "date" : "2021-03-05",
            "name" : "hello",
            "id" : "1"
          }
        }
      }
    ]
  }
}


# 索引
POST /source_test_3/_doc/1
{
  "user" : {
    "id"   : "1",
    "name" : "hello",
    "age"  : 18,
    "date" : "2021-03-05"
  },
  "custom" : {
    "name" : "good"
  }
}

# 搜索,通过includes和excludes指定搜索字段
GET /source_test_3/_search
{
  "_source" : {
    "includes" : ["user.*", "custom.*"],
    "excludes" : ["*.age"]
  },
  "query" : {
    "match_all" : {}
  }
}

# 结果
{
  "took" : 10,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "source_test_3",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "custom" : {
            "name" : "good"
          },
          "user" : {
            "date" : "2021-03-05",
            "name" : "hello",
            "id" : "1"
          }
        }
      }
    ]
  }
}