repEs.js 3.3 KB
Newer Older
1 2 3 4 5 6 7 8 9
import axios from "axios";


class RepEs {

    constructor(config) {
        this.config = config
        this.index_url = config.index_url
        this.index_name = config.index_name
10
        this.token = process.env?.INSCODE_API_KEY??'default'
11 12 13 14
    }

    query (keyword) {

15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
        const token = this.token
        const url = this.index_url + '/' + this.index_name + '/_search' 

        const data = {
            "query": {
                "bool": {
                "must": [
                    {
                    "term": {
                        "user_token": token
                    }
                    }
                ]
                }
            }
        }

        if (keyword) {
            data.query.bool.must.push({
                "match": {
                    "content": keyword
36
                }
37 38 39 40 41
            })
        }

        return new Promise(function(resolve,rejcet){
            axios.post(url, data).then(res => {
42

43 44 45 46
                const result = []
                const total = res?.data?.hits?.total?.value??0
                if (res?.status === 200 && total > 0) {
                    res?.data?.hits?.hits.forEach(element => {
47
                        result.push({
48
                            ...element, ... element?._source
49 50 51 52
                        })
                    });
                }

53
                resolve(result)
54 55 56 57 58 59 60 61 62

            }).catch((err) => {
                console.error(err)
                rejcet()
            })
        })
        
    }

63
    add (content) {
64
        const url = this.index_url + '/' + this.index_name + '/_doc'
65
        const token = this.token
66
          
67
        return new Promise(function(resolve,rejcet){
68
            axios.post(url,
69 70 71
                {
                user_token: token,
                content: content,
72
                }).then(res => {
73 74 75 76 77 78 79 80 81 82 83 84

                        if (res?.status === 200 && res?.data?.code === 200) {
                            const result = []
                            res?.data?.data?.hits.forEach(element => {
                                result.push({
                                    page_content: element?._source?.content
                                })
                            });
                            resolve(result)

                        }
                        console.info(res)
85 86 87 88 89 90 91 92 93 94


            }).catch((err) => {
                console.error(err)
                rejcet()
            })
        })

    }

95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
    delete (id) {
        const url = this.index_url + '/' + this.index_name + '/_doc/' + id

        const token = this.token

        return new Promise(function(resolve,rejcet){
            axios.delete(url).then(res => {

                console.info(res)
                resolve(result)

            }).catch((err) => {
                console.error(err)
                rejcet()
            })
        })

    }

    update (id, content) {
        const url = this.index_url + '/' + this.index_name + '/_doc/' + id

        const token = this.token

        return new Promise(function(resolve,rejcet){
            axios.put(url, {content: content}).then(res => {

                console.info(res)
                resolve(result)

            }).catch((err) => {
                console.error(err)
                rejcet()
            })
        })

    }


134 135 136
}

export default RepEs