genkid2020

Untitled

Aug 11th, 2020
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.28 KB | None | 0 0
  1. const es = require('elasticsearch');
  2. const search = async (search_keyword, data) => {
  3. const esClient = new es.Client({
  4. host: 'localhost:9200',
  5. log: 'trace'
  6. });
  7.  
  8. await esClient.indices.create({
  9. index: 'test',
  10. body: data,
  11. });
  12. await esClient.search({
  13. index: 'test',
  14. body: {
  15. query: {
  16. multi_match: {
  17. query: search_keyword,
  18. fields: ["id", "name", "snippet"],
  19. operator: "or"
  20. }
  21. }
  22. }
  23. })
  24. };
  25. module.exports=search;
  26. в сервере
  27. const express = require("express");
  28. const app = express();
  29. const parser = require('./parser/parser');
  30. const routes=require('./routes/routes');
  31. const search=require('./elasticsearch/search')
  32. ;(async () => {
  33. const result = await parser();
  34. result.forEach(item => {
  35. const data = JSON.parse(item.data);
  36. const output = data.items.map(({id, name, snippet}) => ({
  37. id,
  38. name,
  39. snippet
  40. }));
  41. //console.log(output)
  42. search("JS",output);
  43. });
  44.  
  45. })();
  46. routes(app);
  47. const server = app.listen(3001, () => {
  48. console.log("listening on port %s...", server.address().port);
  49. });
  50.  
Advertisement
Add Comment
Please, Sign In to add comment