如何通过elasticsearch-js获得类似Kibana的建议?

时间:2019-04-17 09:49:53

标签: elasticsearch

在kibana中,它可以通过发送请求字段的建议

{"field":"service"}

/api/kibana/suggestions/values/index-*

并且通过elasticsearch-js我可以使用聚合

  await client.search({
    index: "index-*",
    body: {
      query: {
        bool: {
          must: [{
            range: {
              "@timestamp": {
                gte: "now-16m",
                lte: "now-1m"
              }
            }
          }, {
            query_string: {
              query: `beat.environment:${variables.environment}`
            }
          }]
        }
      },
      aggs: {
        resources: {
          terms: {
            field: "service",
            size: 100
          }
        }
      }
    },
    size: 0
  });

以获得类似的结果,但我宁愿使用其内置的建议功能:

  await client.search({
    index: "index-*",
    body: {
      query: {
        bool: {
          must: [{
            range: {
              "@timestamp": {
                gte: "now-16m",
                lte: "now-1m"
              }
            }
          }, {
            query_string: {
              query: `beat.environment:${variables.environment}`
            }
          }]
        }
      },
      suggest: {
        service: {
          term : {
            text: "",
            field : "service"
          }
        }
      }
    },
    size: 0
  });

不幸的是,这失败了。

0 个答案:

没有答案