Elastic Api 的React实现

Es Api 的React实现

demo地址:https://github.com/jiuweidao/es-demo

Elastic查询 官方文档 https://www.elastic.co/guide/en/elasticsearch/reference/6.7/query-dsl.html

es的封装实现

import React from 'react';

import {post} from "./http";


export class Es {


    constructor(url) {
        this.url = url;
        this.body = {
            "query": {
                "bool": {
                    "filter": {
                        "range": {
                            "@timestamp": {}
                        }
                    }
                },


            },
            "sort": [
                {
                    "@timestamp": {
                        "order": "desc",
                        "unmapped_type": "boolean"
                    },
                }
            ],
        };
    }


    match(field, value) {
        return {"match": {[field]: value}}
    }

    queryString(query, fields = []) {
        return {
            "query_string": {
                "query": query,
                "fields": fields
            }
        }
    }

    //type must->and mustnot an33333d not should-> or
    search(type, value) {
        if (!this.body.query.bool[type]) {
            this.body.query.bool[type] = [];
        }
        this.body.query.bool[type].push(value)
    }

    from(from) {
        this.body.from = from;
    }

    size(size) {
        this.body.size = size;
    }

    timeRange(begin, end, formate = "yy-MM-dd") {
        if (begin)
            this.body.bool.filter.range["@timestamp"].gt = begin;
        if (end)
            this.body.bool.filter.range["@timestamp"].lt = end;
        this.body.bool.filter.range["@timestamp"].format = formate;
    }

    fetch() {
        let url = this.url;
        let body = JSON.stringify(this.body);
            // this.body;//JSON.stringify(this.body);

        return new Promise(function (resolve, reject) {
            post(url, body)
                .then((response) => {
                    resolve(response);
                })
                .catch((error) => {
                    reject(error);
                });
        })
    }

}

es 调用

  let es = new Es("http://localhost:9200/push_log-*/_search");
        es.search("must", es.match("category", "ConfigureDeviceNetwork"));
        es.search("must", es.queryString("*", ["label"]));
        es.from(0);
        es.size(10);
        es.fetch().then((data) => {
            console.log(data)
        });

猜你喜欢

转载自blog.csdn.net/jiuweiC/article/details/88050338