Link Search Menu Expand Document Documentation Menu

包装器

wrapper 查询允许您以 Base64 编码的 JSON 格式提交完整的查询。当查询必须嵌入到仅支持字符串值的上下文中时,这非常有用。

仅当您需要管理系统约束时才使用此查询。为了可读性和可维护性,尽可能使用标准的基于 JSON 的查询会更好。

示例

创建名为 products 的索引,并使用以下映射:

PUT /products
{
  "mappings": {
    "properties": {
      "title": { "type": "text" }
    }
  }
}

索引示例文档:

POST /products/_bulk
{ "index": { "_id": 1 } }
{ "title": "Wireless headphones with noise cancellation" }
{ "index": { "_id": 2 } }
{ "title": "Bluetooth speaker" }
{ "index": { "_id": 3 } }
{ "title": "Over-ear headphones with rich bass" }

将以下查询编码为 Base64 格式:

echo -n '{ "match": { "title": "headphones" } }' | base64

执行编码后的查询:

POST /products/_search
{
  "query": {
    "wrapper": {
      "query": "eyAibWF0Y2giOiB7ICJ0aXRsZSI6ICJoZWFkcGhvbmVzIiB9IH0="
    }
  }
}

响应包含两个匹配文档:

{
  ...
  "hits": {
    "total": {
      "value": 2,
      "relation": "eq"
    },
    "max_score": 0.20098841,
    "hits": [
      {
        "_index": "products",
        "_id": "1",
        "_score": 0.20098841,
        "_source": {
          "title": "Wireless headphones with noise cancellation"
        }
      },
      {
        "_index": "products",
        "_id": "3",
        "_score": 0.18459359,
        "_source": {
          "title": "Over-ear headphones with rich bass"
        }
      }
    ]
  }
}
剩余 350 字符

有问题?

想要贡献?