repEs.js 3.4 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'
W
weixin_44463441 已提交
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
        this.loadToken()
    }

    loadToken () {
        axios.get('/token.json', {
            headers: {
              'Accept': 'application/json'
            }
          }).then(res => {
            debugger
            const token = res?.data??''
            if (token) {
                this.token = token.trim()
                console.info('get token from env [' + this.token + ']')

            }
            
        })
29 30 31 32
    }

    query (keyword) {

33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
        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
54
                }
55 56 57 58 59
            })
        }

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

61 62 63 64
                const result = []
                const total = res?.data?.hits?.total?.value??0
                if (res?.status === 200 && total > 0) {
                    res?.data?.hits?.hits.forEach(element => {
65
                        result.push({
66
                            ...element, ... element?._source
67 68 69 70
                        })
                    });
                }

71 72 73 74
                resolve({
                    total: total,
                    list: result
                })
75 76 77 78 79 80 81 82 83

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

84
    add (content) {
85
        const url = this.index_url + '/' + this.index_name + '/_doc'
86
        const token = this.token
87
          
88
        return new Promise(function(resolve,rejcet){
89
            axios.post(url,
90 91 92
                {
                user_token: token,
                content: content,
93
                }).then(res => {
94
                        console.info(res)
95
                        resolve(res)
96 97 98 99 100 101 102 103 104 105


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

    }

106 107 108 109 110 111 112 113 114
    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)
115
                resolve(res)
116 117 118 119 120 121 122 123 124 125

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

    }

    update (id, content) {
W
weixin_44463441 已提交
126
        debugger
127 128 129 130 131
        const url = this.index_url + '/' + this.index_name + '/_doc/' + id

        const token = this.token

        return new Promise(function(resolve,rejcet){
W
weixin_44463441 已提交
132 133 134 135
            axios.put(url, {
                user_token: token,
                content: content
            }).then(res => {
136 137

                console.info(res)
138
                resolve(res)
139 140 141 142 143 144 145 146 147 148

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

    }


149 150 151
}

export default RepEs