Created
July 24, 2020 10:17
-
-
Save insekticid/452fc7e5619d3a2dff2d9c6d924c50b9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sitemapModule from 'sitemap'; | |
import axios from 'axios'; | |
const { buildSitemapIndex, createSitemapsAndIndex } = sitemapModule; | |
let elasticsearch = axios.create({ | |
baseURL: 'http://' + process.env.ELASTIC_SERVER + ':' + process.env.ELASTIC_PORT, | |
responseType: 'json' | |
}); | |
elasticsearch.interceptors.request.use(request => { | |
//console.log('Starting Request', request) | |
return request | |
}) | |
elasticsearch.interceptors.response.use(response => { | |
//console.log('Response:', response) | |
return response | |
}) | |
let elasticData = (field) => { | |
return { | |
"_source": { | |
"includes": [ "name", "url" ] | |
}, | |
"size": 0, | |
"aggs": { | |
"group_by_state": { | |
"terms": { | |
"field": field, | |
"size": 500000 | |
} | |
} | |
} | |
} | |
} | |
const sitemapPaths = []; | |
let createIndex = (urls) => createSitemapsAndIndex({ | |
urls: urls, | |
lastmod: new Date().toISOString(), | |
targetFolder: 'sitemap-search', | |
hostname: process.env.SITEMAP_URL, | |
cacheTime: 600, | |
sitemapName: 'sitemap', | |
sitemapSize: 10000, // number of urls to allow in each sitemap | |
gzip: true, // whether to gzip the files | |
}) | |
let prepareUrl = (bucket) => { | |
sitemapPaths.push( { url: process.env.SITEMAP_KEY_URL + bucket.key }); | |
} | |
let getData = async() => { | |
await elasticsearch.post(process.env.ELASTIC_INDEX + '/_search', elasticData('name.keyword')) | |
.then(async response => | |
{ | |
let buckets = response.data.aggregations.group_by_state.buckets; | |
await Promise.all(buckets.map(prepareUrl)); | |
console.log('done keyword', buckets.length, sitemapPaths.length) | |
}) | |
await elasticsearch.post(process.env.ELASTIC_INDEX + '/_search', elasticData('name.analyzed')) | |
.then(async response => | |
{ | |
let buckets = response.data.aggregations.group_by_state.buckets; | |
await Promise.all(buckets.map(prepareUrl)); | |
console.log('done analyzed', buckets.length, sitemapPaths.length) | |
}) | |
await createIndex(sitemapPaths); | |
console.log('done all', sitemapPaths.length) | |
} | |
getData() |
Author
insekticid
commented
Jul 24, 2020
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment