天天看点

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"
          ]
        }
      }
    ]
  }
}
           

继续阅读