App.js
1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import React, {Fragment, useState} from "react";
import { Client } from "elasticsearch"
import "./App.css";
function App() {
const [answer, setAnswer] = useState('');
const [inputValue, setInputValue] = useState('');
const client = new Client({
host: 'http://localhost:9200'
});
const onKeyUpHandler = async (event) => {
if (event.charCode === 13) {
setInputValue(event.target.value);
if (inputValue !== '' || inputValue !== undefined || inputValue.length !== 0) {
console.log(inputValue);
try {
const result = await client.search({
index: "wiki-qna2",
body: {
query: {
match: {
"질문(원문, 하 난이도)": inputValue
}
}
}
});
if (result !== null || result !== undefined || result.length !== 0) {
setAnswer(result.hits.hits);
}
} catch (error) {
console.log(error);
}
}
}
}
const showAnswer = (answer) => {
return (
<div>
<b>{answer !== undefined ? answer._source.정답 : 'loading ....'}</b>
</div>
)
}
return (
<div className="App">
<header className="App-header">
<Fragment>
Ask a Question
<input size="80" onKeyPress={onKeyUpHandler}/>
<div>
-- Answers --
<div>
{answer === '' ? " loading answer..." : showAnswer(answer[0])}
</div>
</div>
</Fragment>
</header>
</div>
);
}
export default App;