天天看點

es - mapping - 關于mapping中store的作用

世界上并沒有完美的程式,但是我們并不是以而沮喪,因為寫程式就是一個不斷追求完美的過程。

官網位址 : https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-store.html

mapping的store有兩個作用 :

1. 在_source設定為false時,存儲需要的字段,并用store指定顯示結果

2. 在_source設定為true時,使用store指定傳回的字段,避免傳回資料量大的無用字段,如:不想傳回content(官網提到的方式)

3. 我的使用思路是 :

    1. 開啟 _source

    2. _source 排除(excludes) content字段

    3. content 字段的 store 設定為 true

    4. 其他字段的 store 設定為 false

    5. 正常查詢時不會顯示大文本量的 content 字段,進而提高查詢效率

    6. 但是需要時可以通過 store 查找 content 的值

具體操作 :

# 建立索引,配置_source為 false
# title   的 store 配置為 true
# content 的 store 配置為 false
PUT /test1
{
  "settings": {"number_of_shards": 1}
  , 
  "mappings": {
    "_source": {
      "enabled": false
    }, 
    "properties": {
      "title" : {
        "type": "keyword", 
        "store": true
      },
      "content" : {
        "type": "text", 
        "store": false
      }
    }
  }
}

# 索引資料
POST /test1/_doc
{
  "title" : "hello",
  "content" : "this is my hello"
}

# 查詢 存儲的資料
GET /test1/_search
{
  "stored_fields": ["title"]
}

# 傳回結果,傳回結果以array的形式顯示
{
  "took" : 11,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "test1",
        "_type" : "_doc",
        "_id" : "qmtjI3cBnJvha9PMkYxX",
        "_score" : 1.0,
        "fields" : {
          "title" : [
            "hello"
          ]
        }
      }
    ]
  }
}
           
# 建立索引,配置_source為 true
# title   的 store 配置為 true
# content 的 store 配置為 false
PUT /test1
{
  "settings": {"number_of_shards": 1}
  , 
  "mappings": {
    "_source": {
      "enabled": true
    }, 
    "properties": {
      "title" : {
        "type": "keyword", 
        "store": true
      },
      "content" : {
        "type": "text", 
        "store": false
      }
    }
  }
}

# 索引資料
POST /test1/_doc
{
  "title" : "hello",
  "content" : "this is my hello"
}

# 查詢 存儲的資料
GET /test1/_search
{
  "stored_fields": ["title"]
}

# 結果
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "test1",
        "_type" : "_doc",
        "_id" : "rGtpI3cBnJvha9PM-4yb",
        "_score" : 1.0,
        "fields" : {
          "title" : [
            "hello"
          ]
        }
      }
    ]
  }
}
           
# 建立索引,配置_source為 true
# _source excludes content
# title   的 store 配置為 false
# content 的 store 配置為 true
PUT /test1
{
  "settings": {"number_of_shards": 1}
  , 
  "mappings": {
    "_source": {
      "enabled": true,
      "excludes": [
          "content"
        ]
    }, 
    "properties": {
      "title" : {
        "type": "keyword", 
        "store": false
      },
      "content" : {
        "type": "text", 
        "store": true
      }
    }
  }
}

# 索引資料
POST /test1/_doc
{
  "title" : "hello",
  "content" : "this is my hello"
}

# 查詢
GET /test1/_search

# 傳回結果
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "test1",
        "_type" : "_doc",
        "_id" : "rmtxI3cBnJvha9PMDoxU",
        "_score" : 1.0,
        "_source" : {
          "title" : "hello"
        }
      }
    ]
  }
}

# store 查詢
GET /test1/_search
{
  "stored_fields": ["content"]
}

{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "test1",
        "_type" : "_doc",
        "_id" : "rmtxI3cBnJvha9PMDoxU",
        "_score" : 1.0,
        "fields" : {
          "content" : [
            "this is my hello"
          ]
        }
      }
    ]
  }
}
           

繼續閱讀