Demo entry 6644207

Elasticsearch

   

Submitted by anonymous on Oct 02, 2017 at 09:43
Language: JavaScript. Code size: 890 Bytes.

import elasticsearch from "elasticsearch";

const elasticClient = new elasticsearch.Client({
    host: "localhost:9200",
    log: "info"
});

const elasticService = {};
const index = "persons";
const type = "person";

/**
 * Add a person
 */

elasticService.addPerson = name => {
    return elasticClient.index({
        index,
        type,
        body: {
            name,
            suggest: name.split(" ")
        }
    });
};

/**
 * Fetch suggestions
 */

elasticService.getSuggestions = input => {
    return elasticClient.suggest({
        index,
        body: {
            persons: {
                prefix: input,
                completion: {
                    field: "suggest",
                    fuzzy: true,
                    size: 10
                }
            }
        }
    });
};

export default elasticService;

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).