脚本查询

编辑

运行时字段 提供了一个非常相似且更灵活的功能。您编写一个脚本来创建字段值,并且它们在任何地方都可用,例如 fields所有查询聚合

根据提供的 脚本 过滤文档。script 查询通常在 过滤上下文 中使用。

使用脚本可能会导致搜索速度变慢。请参阅 脚本、缓存和搜索速度

示例请求

编辑
resp = client.search(
    query={
        "bool": {
            "filter": {
                "script": {
                    "script": "\n            double amount = doc['amount'].value;\n            if (doc['type'].value == 'expense') {\n              amount *= -1;\n            }\n            return amount < 10;\n          "
                }
            }
        }
    },
)
print(resp)
response = client.search(
  body: {
    query: {
      bool: {
        filter: {
          script: {
            script: "\n            double amount = doc['amount'].value;\n            if (doc['type'].value == 'expense') {\n              amount *= -1;\n            }\n            return amount < 10;\n          "
          }
        }
      }
    }
  }
)
puts response
const response = await client.search({
  query: {
    bool: {
      filter: {
        script: {
          script:
            "\n            double amount = doc['amount'].value;\n            if (doc['type'].value == 'expense') {\n              amount *= -1;\n            }\n            return amount < 10;\n          ",
        },
      },
    },
  },
});
console.log(response);
GET /_search
{
  "query": {
    "bool": {
      "filter": {
        "script": {
          "script": """
            double amount = doc['amount'].value;
            if (doc['type'].value == 'expense') {
              amount *= -1;
            }
            return amount < 10;
          """
        }
      }
    }
  }
}

您可以使用运行时字段在搜索查询中实现相同的结果。使用 _search API 上的 fields 参数来获取作为同一查询一部分的值

resp = client.search(
    runtime_mappings={
        "amount.signed": {
            "type": "double",
            "script": "\n        double amount = doc['amount'].value;\n        if (doc['type'].value == 'expense') {\n          amount *= -1;\n        }\n        emit(amount);\n      "
        }
    },
    query={
        "bool": {
            "filter": {
                "range": {
                    "amount.signed": {
                        "lt": 10
                    }
                }
            }
        }
    },
    fields=[
        {
            "field": "amount.signed"
        }
    ],
)
print(resp)
response = client.search(
  body: {
    runtime_mappings: {
      'amount.signed' => {
        type: 'double',
        script: "\n        double amount = doc['amount'].value;\n        if (doc['type'].value == 'expense') {\n          amount *= -1;\n        }\n        emit(amount);\n      "
      }
    },
    query: {
      bool: {
        filter: {
          range: {
            'amount.signed' => {
              lt: 10
            }
          }
        }
      }
    },
    fields: [
      {
        field: 'amount.signed'
      }
    ]
  }
)
puts response
const response = await client.search({
  runtime_mappings: {
    "amount.signed": {
      type: "double",
      script:
        "\n        double amount = doc['amount'].value;\n        if (doc['type'].value == 'expense') {\n          amount *= -1;\n        }\n        emit(amount);\n      ",
    },
  },
  query: {
    bool: {
      filter: {
        range: {
          "amount.signed": {
            lt: 10,
          },
        },
      },
    },
  },
  fields: [
    {
      field: "amount.signed",
    },
  ],
});
console.log(response);
GET /_search
{
  "runtime_mappings": {
    "amount.signed": {
      "type": "double",
      "script": """
        double amount = doc['amount'].value;
        if (doc['type'].value == 'expense') {
          amount *= -1;
        }
        emit(amount);
      """
    }
  },
  "query": {
    "bool": {
      "filter": {
        "range": {
          "amount.signed": { "lt": 10 }
        }
      }
    }
  },
  "fields": [{"field": "amount.signed"}]
}

script 的顶级参数

编辑
script
(必需,脚本对象)包含要作为查询运行的脚本。此脚本必须返回布尔值,truefalse

注意

编辑

自定义参数

编辑

过滤器 一样,脚本会被缓存以加快执行速度。如果您频繁更改脚本的参数,我们建议您将其存储在脚本的 params 参数中。例如

resp = client.search(
    query={
        "bool": {
            "filter": {
                "script": {
                    "script": {
                        "source": "doc['num1'].value > params.param1",
                        "lang": "painless",
                        "params": {
                            "param1": 5
                        }
                    }
                }
            }
        }
    },
)
print(resp)
response = client.search(
  body: {
    query: {
      bool: {
        filter: {
          script: {
            script: {
              source: "doc['num1'].value > params.param1",
              lang: 'painless',
              params: {
                "param1": 5
              }
            }
          }
        }
      }
    }
  }
)
puts response
const response = await client.search({
  query: {
    bool: {
      filter: {
        script: {
          script: {
            source: "doc['num1'].value > params.param1",
            lang: "painless",
            params: {
              param1: 5,
            },
          },
        },
      },
    },
  },
});
console.log(response);
GET /_search
{
  "query": {
    "bool": {
      "filter": {
        "script": {
          "script": {
            "source": "doc['num1'].value > params.param1",
            "lang": "painless",
            "params": {
              "param1": 5
            }
          }
        }
      }
    }
  }
}

允许昂贵的查询

编辑

如果 search.allow_expensive_queries 设置为 false,则不会执行脚本查询。