Index Options

  index索引配置项使用。

index_options

  Index 有4中配置,可以控制倒排索引的内容。

  Text类型默认记录positions,其他默认docs。记录的内容越多,所占用的空间越大。

  Index 有4中配置如下:

  • docs
      记录doc id
  • freqs
      记录doc idterm frequencies
  • positions
      记录doc id / term frequencies / term positions
  • offsets
      记录doc id / term frequencies / term positions /character offsets
配置使用

  建立索引时使用index_options配置

put mapping_test
{
  "mappings":{
    "dynamic":"strict",
    "properties":{
      "title":{
        "type":"text",
        "index_options":"freqs"
      },
      "address":{
        "type":"object",
        "dynamic":"true"
      },
      "year":{
        "type":"long"
      }
    }
  }
}

  查询建立配置结果:

get mapping_test/_mappings
{
  "mapping_test" : {
    "mappings" : {
      "dynamic" : "strict",
      "properties" : {
        "address" : {
          "type" : "object",
          "dynamic" : "true"
        },
        "title" : {
          "type" : "text",
          "index_options" : "freqs"
        },
        "year" : {
          "type" : "long"
        }
      }
    }
  }
}

null_value

  如果需要对null值进行搜索,那么只有keyword类型支持设置null_value

  建立索引,在type=keyword属性增加null_value=null设置。

put mapping_test
{
  "mappings":{
    "dynamic":"strict",
    "properties":{
      "title":{
        "type":"text",
        "index_options":"freqs"
      },
      "content":{
        "type":"keyword",
        "null_value":"NULL"
      },
      "author":{
        "type":"keyword",
        "null_value":"NULL"
      },
      "address":{
        "type":"object",
        "dynamic":"true"
      },
      "year":{
        "type":"long"
      }
    }
  }
}

  写入数据,author字段要设置为null,否则doc没有该字段。

POST mapping_test/_doc/
{
    "title" : "es index options",
    "content" : "index_options|null_value",
    "author": null,
    "address" : {
      "province":"广东省",
      "city":"广州市"
      },
    "year":2022
}

  查询1

get mapping_test/_search?q=year:2022
# 输出结果
{
        "_index" : "mapping_test",
        "_type" : "_doc",
        "_id" : "F-PcPIABdHVKdYPW-cjt",
        "_score" : 1.0,
        "_source" : {
          "title" : "es index options",
          "content" : "index_options|null_value",
          "author" : null,
          "address" : {
            "province" : "广东省",
            "city" : "广州市"
          },
          "year" : 2022
        }
      }

  查询2

get mapping_test/_search?q=author:NULL
# 输出结果
{
        "_index" : "mapping_test",
        "_type" : "_doc",
        "_id" : "F-PcPIABdHVKdYPW-cjt",
        "_score" : 0.2876821,
        "_source" : {
          "title" : "es index options",
          "content" : "index_options|null_value",
          "author" : null,
          "address" : {
            "province" : "广东省",
            "city" : "广州市"
          },
          "year" : 2022
        }
      }

copy_to

  _all在ES 7中被copy_to所代替,目的是满足一些特定的搜索要求。

  copy_to 的作用是把数据拷贝到特定的字段。

  copy_to 描述的字段不会出现在_source 里面。

  copy_to使用,copy_to="full_date",把year、month、day字段数据复制到full_date字段,并且字段值通过空格分隔。例如:year=2022,month=4,day=18 full_date=2022 4 18.

put mapping_test
{
  "mappings":{
    "dynamic":"true",
    "properties":{
      "title":{
        "type":"text",
        "index_options":"freqs"
      },
      "content":{
        "type":"keyword",
        "null_value":"NULL"
      },
      "author":{
        "type":"keyword",
        "null_value":"NULL"
      },
      "address":{
        "type":"object",
        "dynamic":"true"
      },
      "year":{
        "type":"long",
        "copy_to":"full_date"
      },
      "month":{
        "type":"long",
        "copy_to":"full_date"
        
      },
      "day":{
        "type":"long",
        "copy_to":"full_date"
        
      }
    }
  }
}

  写入数据并且查询:

POST mapping_test/_doc/
{
    "title" : "es index options",
    "content" : "index_options|null_value",
    "author": null,
    "address" : {
      "province":"广东省",
      "city":"广州市"
      },
    "year":2022,
    "month":4,
    "day":18
}
# 通过full_date查询,能查询到数据,但没有full_date字段返回
get mapping_test/_search?q=full_date:2022 4 18
{
	"_index": "mapping_test",
	"_type": "_doc",
	"_id": "GuPsPIABdHVKdYPWRMhg",
	"_score": 1.0,
	"_source": {
		"title": "es index options",
		"content": "index_options|null_value",
		"author": null,
		"address": {
			"province": "广东省",
			"city": "广州市"
		},
		"year": 2022,
		"month": 4,
		"day": 18
	}
}
Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐