Advertisement
RaelLopes368

Untitled

Jun 19th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Exporta os módulos necessários
  2. module.exports = function (fastify, opts, next) {
  3.     //Cria uma rota para uma requisição POST ,rota definida como /related
  4.     fastify.post('/related', function (request, reply) {
  5.         //Cria uma função search que irá fazer uma busca no elasticsearch
  6.         const search = (index, body) => this.elasticsearch.search({ index, body });
  7.        
  8.         body(function(body){
  9.             //faz uma busca por publishers
  10.             search('publishers', body).then((results) => {
  11.                 resultado(results);
  12.             });
  13.         });
  14.                
  15.  
  16.     });
  17.  
  18.     next();
  19.  
  20. };
  21.  
  22.  
  23.  
  24. function insereArray(urls,titles,images,descriptions,itens,callback){
  25.     for (const i in titles) {
  26.  
  27.         const url = urls[i];
  28.         const title = titles[i];
  29.         const image = images[i];
  30.         const description = descriptions[i];
  31.         itens.push({
  32.             title,
  33.             link: url,
  34.             image,
  35.             description,
  36.         });
  37.  
  38.     }
  39.     callback(itens);
  40. }
  41.  
  42.  
  43. function resultado(results){
  44.     const itens = [];
  45.             const urls = [];
  46.             const titles = [];
  47.             const images = [];
  48.             const descriptions = [];
  49.  
  50.             for (let i = 0; i < results.hits.hits.length; i++) {
  51.                 //Cria uma regex para substituir as palavras More ou Mais
  52.                 regexReplace = '/(More|Mais)/';
  53.                 //Variavel que recebe os valores vindo de results.hits.hits[indice do for]._source.description
  54.                 const description = results.hits.hits[
  55.                     i
  56.                 ]._source.description.replace(regexReplace, '');
  57.  
  58.                 //verifica se no array titles contém o elemento results.hits.hits
  59.                 if (!titles.includes(results.hits.hits[i]._source.title)) {
  60.                     //adiciona o valor do resultado da busca aos arrays titles ,,urls ,images e descriptions
  61.                     titles.push(results.hits.hits[i]._source.title);
  62.                     urls.push(results.hits.hits[i]._source.url);
  63.                     images.push(results.hits.hits[i]._source.image);
  64.                     descriptions.push(description);
  65.  
  66.                 }
  67.                 if (titles.length == 5) {
  68.  
  69.                     break;
  70.  
  71.                 }
  72.  
  73.             }
  74.             //função para inserir os dados no array itens
  75.             insereArray(urls,titles,images,descriptions,itens,function(itens){
  76.                 //retorna os itens
  77.                 reply.send(itens);
  78.             });
  79.            
  80. }
  81.  
  82.  
  83.  
  84. function body(callback){
  85.     const body = {
  86.         size: 10,
  87.         from: 0,
  88.         _source: ['title', 'url', 'image', 'description'],
  89.         query: {
  90.             bool: {
  91.                 must: [
  92.                     {
  93.                         query_string: {
  94.                             query: `(content:${
  95.                                 request.body.term
  96.                             }) AND (domain:*${request.body.domain}*)`,
  97.                         },
  98.                     },
  99.                 ],
  100.             },
  101.         },
  102.     };
  103.     callback(body);
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement