Skip to content

mub4shir/elasticsearch

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Elasticsearch Operations

This code snippet demonstrates basic Elasticsearch operations using the @elastic/elasticsearch package. It covers the following operations:

  • Create an index
  • Delete an index
  • Index a document
  • Search documents
  • Delete a document

Prerequisites

Before running the code, make sure you have the following:

  • Elasticsearch instance running. Update the Elasticsearch node URL in the code (elasticSearchConfig.node) to match your Elasticsearch node URL.

Installation

  1. Clone the repository.
git clone https://github.com/mub4shir/elasticsearch.git
cd elasticsearch
  1. Install the dependencies.
npm install

Usage

To use the Elasticsearch operations, follow these steps:

  1. Import the required functions into your script.
import {
  createIndex,
  deleteIndex,
  indexDocument,
  searchDocuments,
  deleteDocument,
} from "./elasticsearch";

// Replace "./elasticsearch" with the path to the Elasticsearch module in your project.

  1. Initialize the Elasticsearch client.
const elasticSearchConfig = {
  node: "http://localhost:9200", // Replace with your Elasticsearch node URL
};

const esClient = new Client(elasticSearchConfig);
  1. Call the desired functions as needed.
// Example: Create an index
await createIndex("your_index_name");

// Example: Index a document
const document = { id: 1, title: "Example Document" };
await indexDocument("your_index_name", document);

// Example: Search documents
const query = { query: { match_all: {} } };
const results = await searchDocuments("your_index_name", query);
console.log(results);

// Example: Delete a document
await deleteDocument("your_index_name", "1");

// Example: Delete an index
await deleteIndex("your_index_name");
  1. Replace "your_index_name" with the actual index name you want to work with.

License

This code is open source and available under the MIT License.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published