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
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.
- Clone the repository.
git clone https://github.com/mub4shir/elasticsearch.git
cd elasticsearch- Install the dependencies.
npm installTo use the Elasticsearch operations, follow these steps:
- 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.
- Initialize the Elasticsearch client.
const elasticSearchConfig = {
node: "http://localhost:9200", // Replace with your Elasticsearch node URL
};
const esClient = new Client(elasticSearchConfig);
- 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");
- Replace "your_index_name" with the actual index name you want to work with.
This code is open source and available under the MIT License.