es - elasticsearch search - missing value and unmapped fields

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

问:missing value和unmapped fields的配置有什么作用?
答:
在这里插入图片描述
问:missing和unmapped如何使用?
答:

# sort 
# missing value and ignore unmapped fields

# unmapped type
DELETE /sort_test_6

# 映射
PUT /sort_test_6
{
  "mappings": {
    "properties": {
      "name": {"type": "text"}
    }
  }
}

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

# 搜索 - 没有age的map映射,报错
GET /sort_test_6/_search
{
  "query": {
    "match_all": {}
  },
  "sort": ["age"]
}

# 结果
{
  "error" : {
    "root_cause" : [
      {
        "type" : "query_shard_exception",
        "reason" : "No mapping found for [age] in order to sort on",
        "index_uuid" : "5UvUVsd7Q7uxocP1OGeuvw",
        "index" : "sort_test_6"
      }
    ],
    "type" : "search_phase_execution_exception",
    "reason" : "all shards failed",
    "phase" : "query",
    "grouped" : true,
    "failed_shards" : [
      {
        "shard" : 0,
        "index" : "sort_test_6",
        "node" : "VK0GF1KyQGKmM_0KvgHHnw",
        "reason" : {
          "type" : "query_shard_exception",
          "reason" : "No mapping found for [age] in order to sort on",
          "index_uuid" : "5UvUVsd7Q7uxocP1OGeuvw",
          "index" : "sort_test_6"
        }
      }
    ]
  },
  "status" : 400
}

# 搜索 - 没有age的map映射,默认long
GET /sort_test_6/_search
{
  "query": {
    "match_all": {}
  },
  "sort": [
    {
      "age": {
        "unmapped_type": "long"
      }
    }
  ]
}

# 结果
{
  "took" : 10,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [
      {
        "_index" : "sort_test_6",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : null,
        "_source" : {
          "name" : "hello"
        },
        "sort" : [
          9223372036854775807
        ]
      }
    ]
  }
}

# missing value
PUT /sort_test_7
{
  "mappings": {
    "properties": {
      "age": {"type": "long"}
    }
  }
}

# 索引
PUT /sort_test_7/_doc/1
{
  "age": 18
}

# 索引
PUT /sort_test_7/_doc/2
{
  "age": 19
}

# 索引
PUT /sort_test_7/_doc/3
{
  "name": "good"
}

# 搜索 - 如果文档没有字段值,missing可以指定
# 没有字段的文档在排序时是处在最上(_first)
# 或最下(_last)的位置
GET /sort_test_7/_search
{
  "query": {
    "match_all": {}
  },
  "sort": [
    {
      "age": {
        "order"  : "desc",
        "missing": "_first"
      }
    }
  ]
}

# 结果
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [
      {
        "_index" : "sort_test_7",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : null,
        "_source" : {
          "name" : "good"
        },
        "sort" : [
          9223372036854775807
        ]
      },
      {
        "_index" : "sort_test_7",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : null,
        "_source" : {
          "age" : 19
        },
        "sort" : [
          19
        ]
      },
      {
        "_index" : "sort_test_7",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : null,
        "_source" : {
          "age" : 18
        },
        "sort" : [
          18
        ]
      }
    ]
  }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

这是谁的博客?

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值