es - elasticsearch mapping - field data type - 2

212 阅读2分钟

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

问 :keyword 包括哪三种类型?各自的作用是什么?
答 :

1. 包含三种类型 : keyword、constant_keyword、wildcard
2. 各自作用 :
    1. keyword          : 用于存储不需要分词的域,查询速度快
    2. constant_keyword : 用于存储单个索引中值相同的域
    3. wildcard         : 用于存储需要通配符查询的域,占用空间小,但查询速度慢

问 :keyword如何使用?
答 :

# keyword
# 配置项 :
#   1. boost                        :默认1.0
#   2. doc_values                   :默认true  
#   3. eager_global_ordinals        :是否在刷新时加载全局序数,默认false,适用于平凡使用的用于聚合的term
#   4. fields                       :可以配置域值相同的多个域以用作不同的用途
#   5. igonre_above                 :不索引超过长度的值,默认2147483647 ,动态mapping默认256
#   6. index                        :默认true
#   7. index_options                :指定索引时存储什么信息,默认docs
#   8. norms                        :评分查询时是否考虑字段长度,默认false
#   9. null_value                   :默认null
#   10. store                       :默认false
#   11. similarity                  :使用哪个评分算法或相似度,默认BM25
#   12. normalizer                  :索引前如何预处理关键字,默认null
#   13. split_queries_on_whitespace :查询时是否空格分词,默认false
#   14. meta                        : 元数据信息

PUT /keyword_test
{
  "mappings" : {
    "properties" : {
      "my_keyword" : {"type" : "keyword"}
    }
  }
}

POST /keyword_test/_doc
{
  "my_keyword" : "hello this is me"
}

GET /keyword_test/_search
{
  "query": {
    "match": {
      "my_keyword" : "hello this is me"
    }
  }
}

# 结果
{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 0.2876821,
    "hits" : [
      {
        "_index" : "keyword_test",
        "_type" : "_doc",
        "_id" : "FWemx3cB3ed5oQc7B86R",
        "_score" : 0.2876821,
        "_source" : {
          "my_keyword" : "hello this is me"
        }
      }
    ]
  }
}

问 :constant_keyword 如何使用?
答 :

# constant_keyword
# 配置项 :
#   1. meta  :元数据信息
#   2. value :值

PUT /constant_keyword_test
{
  "mappings" : {
    "properties" : {
      "my_constant_keyword" : {
        "type"  : "constant_keyword",
        "value" : "hello good"
      }
    }
  }
}

POST /constant_keyword_test/_doc
{
  "my_constant_keyword" : "hello good"
}

GET /constant_keyword_test/_search
{
  "query" : {
    "match" : {
      "my_constant_keyword" : "hello good"
    }
  }
}

# 结果
{
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "constant_keyword_test",
        "_type" : "_doc",
        "_id" : "GGeux3cB3ed5oQc7Q85t",
        "_score" : 1.0,
        "_source" : {
          "my_constant_keyword" : "hello good"
        }
      },
      {
        "_index" : "constant_keyword_test",
        "_type" : "_doc",
        "_id" : "GWeux3cB3ed5oQc7Rs5a",
        "_score" : 1.0,
        "_source" : {
          "my_constant_keyword" : "hello good"
        }
      },
      {
        "_index" : "constant_keyword_test",
        "_type" : "_doc",
        "_id" : "Gmeux3cB3ed5oQc7SM6x",
        "_score" : 1.0,
        "_source" : {
          "my_constant_keyword" : "hello good"
        }
      }
    ]
  }
}

问 :wildcard如何使用?
答 :

# wildcard
# 配置项 :
#   1. null_value   :默认null
#   2. ignore_above :超出长度的值不索引,默认2147483647

PUT /wildcard_test
{
  "mappings" : {
    "properties" : {
      "my_wildcard" : {
        "type" : "wildcard"
      }
    }
  }
}

POST /wildcard_test/_doc
{
  "my_wildcard" : "hello gooding me"
}

GET /wildcard_test/_search
{
  "query" : {
    "wildcard" : {
      "my_wildcard" : {
        "value" : "hello*me"
      }
    }
  }
}

# 结果
{
  "took" : 3,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 3.8067765,
    "hits" : [
      {
        "_index" : "wildcard_test",
        "_type" : "_doc",
        "_id" : "G2e1x3cB3ed5oQc7J87J",
        "_score" : 3.8067765,
        "_source" : {
          "my_wildcard" : "hello gooding me"
        }
      }
    ]
  }
}