import axios from "axios"; class RepEs { constructor(config) { this.config = config this.index_url = config.index_url this.index_name = config.index_name this.token = process.env?.INSCODE_API_KEY??'default' } query (keyword) { const url = this.index_url + '?user_token=' + this.token + '&index_name=' + this.index_name return new Promise(function(resolve,rejcet){ axios.get(url,{ params: { keyword: encodeURI(keyword), } }).then(res => { 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) }).catch((err) => { console.error(err) rejcet() }) }) } add (content) { const name = '/' + this.index_name const url = this.index_url + '/' + this.index_name const token = this.token const instance = axios.create({ baseURL: this.index_url, timeout: 5000, withCredentials: true, // 允许跨域请求 }); return new Promise(function(resolve,rejcet){ instance.put(name, { user_token: token, content: content, }, { headers: { 'Content-Type': 'application/json' } }).then(res => { 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) }).catch((err) => { console.error(err) rejcet() }) }) } } export default RepEs