How to use elasticsearch - 10 common examples

To help you get started, we’ve selected a few elasticsearch examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github awslabs / aws-data-lake-solution / source / api / services / search / lib / metadata.js View on Github external
data.Groups.map(group => {filter.push({match: {"groups": group.GroupName}});});
                        accessible_packages.query.bool.filter = {
                            bool: {
                                should: filter
                            }
                        };
                    }
                    body.push({});
                    body.push(owned_packages);
                    body.push({});
                    body.push(accessible_packages);

                    //-------------------------------------------------------------
                    // Execute Search
                    //-------------------------------------------------------------
                    let client = require('elasticsearch').Client({
                        hosts: `${config.Item.setting.esurl}`,
                        connectionClass: require('http-aws-es'),
                        amazonES: {
                            region: process.env.AWS_REGION,
                            credentials: creds
                        }
                    });
                    client.msearch({
                        body: body
                    }).then(function(body) {
                        console.log(body);
                        let _results = {
                            owned_packages: body.responses[0].hits.total,
                            accessible_packages: body.responses[1].hits.total
                        };
                        cb(null, _results);
github sat-utils / sat-api / packages / api-lib / libs / es.js View on Github external
async function connect() {
  let esConfig
  let client

  // use local client
  if (!process.env.ES_HOST) {
    client = new elasticsearch.Client({ host: 'localhost:9200' })
  } else {
    await new Promise((resolve, reject) => AWS.config.getCredentials((err) => {
      if (err) return reject(err)
      return resolve()
    }))

    AWS.config.update({
      credentials: new AWS.Credentials(process.env.AWS_ACCESS_KEY_ID,
        process.env.AWS_SECRET_ACCESS_KEY),
      region: process.env.AWS_REGION || 'us-east-1'
    })

    esConfig = {
      hosts: [process.env.ES_HOST],
      connectionClass: httpAwsEs,
      awsConfig: new AWS.Config({ region: process.env.AWS_REGION || 'us-east-1' }),
github elastic / timelion / server / parser / function_runner.js View on Github external
var aggspretion = require('../parser/function.js');
var _ = require('lodash');
var Promise = require('bluebird');
var elasticsearch = require('elasticsearch');
var client = new elasticsearch.Client({
  host: 'localhost:9200',
});

// Contains the parsed sheet;
var sheet;

function getRequest (config) {
  var body = {
    query: {
      query_string: {
        query: config.query
      }
    },
    aggs: {
      series: {
        date_histogram: {
github artsmia / collection-elasticsearch / search.js View on Github external
const fs = require('fs')

var es = new require('elasticsearch').Client({
  host: process.env.ES_URL,
  log: false,
  requestTimeout: 3000,
})

var search = function(query, size, sort, filters, isApp, from, req, callback) {
  var fields = ["artist.artist^15", "artist.folded^15", "title^11", "title.folded^5", "description^3", "text.*^2", "accession_number", "_all", "artist.ngram^2", "title.ngram"]
  if(query.match(/".*"/)) fields = fields.slice(0, -2)
  if(filters) query += ' '+filters
  var limitToPublicAccess = req.query.token != process.env.PRIVATE_ACCESS_TOKEN
  if(limitToPublicAccess && [query, filters].indexOf('deaccessioned:true') + [query, filters].indexOf('deaccessioned:"true"') === -2) query += ' public_access:1'
  // if(isApp) query += ' room:G*' // restrict searches from the journeys app to only on view objects
  var isMoreArtsmia = req.headers.origin && req.headers.origin.match('//more.artsmia.org')
    || req.query.tag && req.query.tag == "more"
  var boostOnViewArtworks = isApp || isMoreArtsmia
github elastic / kibana / src / core / server / elasticsearch / retry_call_cluster.ts View on Github external
* Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

import { retryWhen, concatMap } from 'rxjs/operators';
import { defer, throwError, iif, timer } from 'rxjs';
import * as legacyElasticsearch from 'elasticsearch';

import { CallAPIOptions } from '.';
import { Logger } from '../logging';

const esErrors = legacyElasticsearch.errors;

/**
 * Retries the provided Elasticsearch API call when an error such as
 * `AuthenticationException` `NoConnections`, `ConnectionFault`,
 * `ServiceUnavailable` or `RequestTimeout` are encountered. The API call will
 * be retried once a second, indefinitely, until a successful response or a
 * different error is received.
 *
 * @param apiCaller
 */

// TODO: Replace with APICaller from './scoped_cluster_client' once #46668 is merged
export function migrationsRetryCallCluster(
  apiCaller: (
    endpoint: string,
    clientParams: Record,
github pelias / api / test / unit / middleware / sendJSON.js View on Github external
test('generic elasticsearch error', function(t) {
    var res = { body: { geocoding: {
      errors: [ new es.errors.Generic('an error') ]
    }}};

    res.status = function( code ){
      return { json: function( body ){
        t.equal( code, 500, 'Internal Server Error' );
        t.deepEqual( body, res.body, 'body set' );
        t.end();
      }};
    };

    middleware(null, res);
  });
};
github pelias / api / test / unit / middleware / sendJSON.js View on Github external
test('request timeout', function(t) {
    var res = { body: { geocoding: {
      errors: [ new es.errors.RequestTimeout('an error') ]
    }}};

    res.status = function( code ){
      return { json: function( body ){
        t.equal( code, 502, 'Bad Gateway' );
        t.deepEqual( body, res.body, 'body set' );
        t.end();
      }};
    };

    middleware(null, res);
  });
};
github elastic / kibana / src / plugins / kibana / server / lib / __tests__ / handle_es_error.js View on Github external
it('should return a boom 503 server timeout error for ES connection errors', function () {
    expect(handleESError(new esErrors.ConnectionFault()).output.statusCode).to.be(503);
    expect(handleESError(new esErrors.ServiceUnavailable()).output.statusCode).to.be(503);
    expect(handleESError(new esErrors.NoConnections()).output.statusCode).to.be(503);
    expect(handleESError(new esErrors.RequestTimeout()).output.statusCode).to.be(503);
  });
github elastic / kibana / src / core_plugins / kibana / server / lib / __tests__ / handle_es_error.js View on Github external
it('should return a boom 503 server timeout error for ES connection errors', function () {
    expect(handleESError(new esErrors.ConnectionFault()).output.statusCode).to.be(503);
    expect(handleESError(new esErrors.ServiceUnavailable()).output.statusCode).to.be(503);
    expect(handleESError(new esErrors.NoConnections()).output.statusCode).to.be(503);
    expect(handleESError(new esErrors.RequestTimeout()).output.statusCode).to.be(503);
  });
github elastic / kibana / src / server / saved_objects / client / lib / __tests__ / decorate_es_error.js View on Github external
it('makes es.ConnectionFault a SavedObjectsClient/EsUnavailable error', () => {
    const error = new esErrors.ConnectionFault();
    expect(isEsUnavailableError(error)).to.be(false);
    expect(decorateEsError(error)).to.be(error);
    expect(isEsUnavailableError(error)).to.be(true);
  });