search tool

by fxi

JavaScript

// search tool
const search_host = 'https://search.staging.mapx.org:443/indexes/views_en/search';
const apiToken = 'ba42fa8d4564234e242a6fcb661eedfc484766d8b13e4570239f7b85aee54ce8';
var query = {
  "q": "",
  "limit": 10000
};

// list of projects
const endpoint_projects = 'https://api.staging.mapx.org/get/project/search?';
const regex = '^(WESR|Minamata Convention on Mercury|Chemicals Observatory).*$'
const ids = [
  'MX-KVV-ANS-WBN-QXR-S71',
  'MX-U83-0SB-Y2T-A48-GFS',
  'MX-XNI-RMZ-KKL-FMS-DVH',
  'MX-6D4-562-1UT-E3X-4YY',
  'MX-3ZK-82N-DY8-WU2-IGF',
  'MX-2LD-FBB-58N-ROK-8RH'
];
const ids_str = ids.join(',');
const args_projects = `titleRegex=${regex}&language=en&idProjects=${ids_str}&maxByPage=2000&pageNumber=1`


fetch(`${endpoint_projects}${args_projects}`)
  .then(response => {
    if (!response.ok) {
      throw new Error(`erreur HTTP! statut: ${reponse.status}`);
    }
    return response.json();
  })
  .then(j => {
    if (typeof(j) === 'object' && j.type === 'error') {
      throw new Error(j.message);
    }
    const all_ids = j.hits.map(el => el.id);
    const filters = all_ids.map(i => `projects_id = ${i}`);
    const filters_str = filters.join(' OR ');
    query.filters = filters_str;
   
    fetch(search_host, {
        method: 'POST',
        headers: {
          'Accept': 'application/json',
          'Content-Type': 'application/json',
          'X-Meili-Api-Key': apiToken
        },
        body: JSON.stringify(query)
      })
      .then(response => {
        if (!response.ok) {
          throw new Error(`erreur HTTP! statut: ${reponse.status}`);
        }
        return response.json();
      })
      .then(j => {
        if (typeof(j) === 'object' && j.type === 'error') {
          throw new Error(j.message);
        }
        const wesr_catalog = j.hits;
        console.log(wesr_catalog);
      })
  })
  .catch(err => {
    throw new Error(err.message);
  });