|
1 | | -const elasticsearch = require('elasticsearch'); |
| 1 | +const elasticsearch = require("elasticsearch"); |
2 | 2 | var client = new elasticsearch.Client({ |
3 | | - host: 'localhost:9200', |
| 3 | + host: "localhost:9200", |
4 | 4 | }); |
5 | | -const express = require('express'); |
6 | | -const app = express(); |
| 5 | +const express = require("express"); |
| 6 | +const app = express(); |
7 | 7 |
|
8 | | -app.set('port', process.env.PORT || 8000); |
| 8 | +app.set("port", process.env.PORT || 8000); |
9 | 9 |
|
10 | | -app.get('/status', function(req, res) { |
| 10 | +app.get("/status", function (req, res) { |
11 | 11 | if (req.query.s == MY_SECRET_KEY) { |
12 | | - client.ping({ |
13 | | - requestTimeout: 1000, |
14 | | - }, function (error) { |
15 | | - if (error) { |
16 | | - console.error('elasticsearch cluster is down!'); |
17 | | - res.end('elasticsearch cluster is down!'); |
18 | | - } else { |
19 | | - console.log('All is well'); |
20 | | - res.end('All is well'); |
21 | | - } |
22 | | - }); |
| 12 | + client.ping( |
| 13 | + { |
| 14 | + requestTimeout: 1000, |
| 15 | + }, |
| 16 | + function (error) { |
| 17 | + if (error) { |
| 18 | + console.error("elasticsearch cluster is down!"); |
| 19 | + res.end("elasticsearch cluster is down!"); |
| 20 | + } else { |
| 21 | + console.log("All is well"); |
| 22 | + res.end("All is well"); |
| 23 | + } |
| 24 | + }, |
| 25 | + ); |
23 | 26 | } |
24 | 27 | }); |
25 | 28 |
|
26 | | -app.get('/', function(req, res) { |
| 29 | +app.get("/", function (req, res) { |
27 | 30 | if (!req.query.s) { |
28 | 31 | res.send([]); |
29 | 32 | return; |
30 | 33 | } |
31 | 34 | let keyword = req.query.s.slice(0, 50); |
32 | 35 | console.log(keyword); |
33 | | - client.search({ |
34 | | - index: "oiwiki", |
35 | | - from: 0, |
36 | | - size: 12, |
37 | | - body: { |
38 | | - query: { |
39 | | - dis_max: { |
40 | | - queries: [ |
41 | | - { |
42 | | - match: { |
43 | | - title: { |
44 | | - query: keyword, |
45 | | - minimum_should_match: "75%", |
46 | | - boost: 4 |
47 | | - } |
48 | | - } |
49 | | - }, |
50 | | - { |
51 | | - match: { |
52 | | - h2: { |
53 | | - query: keyword, |
54 | | - minimum_should_match: "75%", |
55 | | - boost: 3 |
56 | | - } |
57 | | - } |
58 | | - }, |
59 | | - { |
60 | | - match: { |
61 | | - content: { |
62 | | - query: keyword, |
63 | | - minimum_should_match: "75%", |
64 | | - boost: 2 |
65 | | - } |
66 | | - } |
67 | | - }, |
68 | | - { |
69 | | - match: { |
70 | | - url: { |
71 | | - query: keyword, |
72 | | - minimum_should_match: "75%", |
73 | | - boost: 3 |
74 | | - } |
75 | | - } |
76 | | - }, |
77 | | - { |
78 | | - match: { |
79 | | - standard_content: { |
80 | | - query: keyword, |
81 | | - minimum_should_match: "75%", |
82 | | - boost: 2 |
83 | | - } |
84 | | - } |
85 | | - } |
86 | | - ], |
87 | | - tie_breaker: 0.3 |
88 | | - } |
89 | | - }, |
90 | | - highlight: { |
91 | | - pre_tags: ["<em>"], |
92 | | - post_tags: ["</em>"], |
93 | | - fields: { |
94 | | - title: { number_of_fragments: 1 }, |
95 | | - h2: { number_of_fragments: 1 }, |
96 | | - content: { number_of_fragments: 1 }, |
97 | | - url: { number_of_fragments: 1 } |
| 36 | + client |
| 37 | + .search({ |
| 38 | + index: "oiwiki", |
| 39 | + from: 0, |
| 40 | + size: 12, |
| 41 | + body: { |
| 42 | + query: { |
| 43 | + dis_max: { |
| 44 | + queries: [ |
| 45 | + { |
| 46 | + match: { |
| 47 | + title: { |
| 48 | + query: keyword, |
| 49 | + minimum_should_match: "75%", |
| 50 | + boost: 4, |
| 51 | + }, |
| 52 | + }, |
| 53 | + }, |
| 54 | + { |
| 55 | + match: { |
| 56 | + h2: { |
| 57 | + query: keyword, |
| 58 | + minimum_should_match: "75%", |
| 59 | + boost: 3, |
| 60 | + }, |
| 61 | + }, |
| 62 | + }, |
| 63 | + { |
| 64 | + match: { |
| 65 | + content: { |
| 66 | + query: keyword, |
| 67 | + minimum_should_match: "75%", |
| 68 | + boost: 2, |
| 69 | + }, |
| 70 | + }, |
| 71 | + }, |
| 72 | + { |
| 73 | + match: { |
| 74 | + url: { |
| 75 | + query: keyword, |
| 76 | + minimum_should_match: "75%", |
| 77 | + boost: 3, |
| 78 | + }, |
| 79 | + }, |
| 80 | + }, |
| 81 | + { |
| 82 | + match: { |
| 83 | + standard_content: { |
| 84 | + query: keyword, |
| 85 | + minimum_should_match: "75%", |
| 86 | + boost: 2, |
| 87 | + }, |
| 88 | + }, |
| 89 | + }, |
| 90 | + ], |
| 91 | + tie_breaker: 0.3, |
| 92 | + }, |
98 | 93 | }, |
99 | | - fragment_size: 20, |
100 | | - } |
101 | | - } |
102 | | - }) |
103 | | - .then(results => { |
104 | | - results = results.hits.hits; |
105 | | - results = results.map((e) => { |
106 | | - let highlight = []; |
107 | | - if (!e.highlight || !e.highlight.content) { |
108 | | - highlight = [e._source.content.substring(0, 50)]; |
109 | | - } else { |
110 | | - highlight = e.highlight.content; |
111 | | - } |
112 | | - return { |
113 | | - url: e._source.url, |
114 | | - title: e._source.title, |
115 | | - highlight: highlight |
116 | | - } |
| 94 | + highlight: { |
| 95 | + pre_tags: ["<em>"], |
| 96 | + post_tags: ["</em>"], |
| 97 | + fields: { |
| 98 | + title: { number_of_fragments: 1 }, |
| 99 | + h2: { number_of_fragments: 1 }, |
| 100 | + content: { number_of_fragments: 1 }, |
| 101 | + url: { number_of_fragments: 1 }, |
| 102 | + }, |
| 103 | + fragment_size: 20, |
| 104 | + }, |
| 105 | + }, |
| 106 | + }) |
| 107 | + .then((results) => { |
| 108 | + results = results.hits.hits; |
| 109 | + results = results.map((e) => { |
| 110 | + let highlight = []; |
| 111 | + if (!e.highlight || !e.highlight.content) { |
| 112 | + highlight = [e._source.content.substring(0, 50)]; |
| 113 | + } else { |
| 114 | + highlight = e.highlight.content; |
| 115 | + } |
| 116 | + return { |
| 117 | + url: e._source.url, |
| 118 | + title: e._source.title, |
| 119 | + highlight: highlight, |
| 120 | + }; |
| 121 | + }); |
| 122 | + res.send(results); |
| 123 | + }) |
| 124 | + .catch((err) => { |
| 125 | + console.log(err); |
| 126 | + res.send([]); |
117 | 127 | }); |
118 | | - res.send(results); |
119 | | - }) |
120 | | - .catch(err=> { |
121 | | - console.log(err); |
122 | | - res.send([]); |
123 | | - }); |
124 | 128 | }); |
125 | 129 |
|
126 | | -app.listen(app.get('port'), function() { |
127 | | - console.log('Search server running on port ' + app.get('port')); |
| 130 | +app.listen(app.get("port"), function () { |
| 131 | + console.log("Search server running on port " + app.get("port")); |
128 | 132 | }); |
129 | | - |
|
0 commit comments