Before launching queries, ensure your server environment permits necessary cross-origin requests and establish a management interface.
- CORS Configuration: In the
config/elasticsearch.ymlfile of your installation directory, enable HTTP Cross-Origin Resource Sharing to allow frontend access:
# Enable HTTP CORS support
http.cors.enabled: true
# Allow all origins (use '*' cautiously)
http.cors.allow-origin: "*"
- Installation Manager Tool: To visualize clusters and documents, deploy the
elasticsearch-headplugin via NPM.
# Install global CLI tools
npm i -g grunt-cli
# Navigate to project root and run installer
npm i .
# Launch the server interface
grunt server
Core Indexing Operations
The following steps demonstrate managing an index named articles_db, including schema definition and document manipulation.
- Create Index: Define shard count, replica settings, and field mappings.
PUT localhost:9200/articles_db
Content-Type: application/json
{
"settings": {
"index": {
"number_of_shards": 3,
"number_of_replicas": 1
}
},
"mappings": {
"properties": {
"doc_uid": {
"type": "keyword"
},
"headline": {
"type": "text",
"store": true,
"analyzer": "standard"
},
"main_body": {
"type": "text",
"store": true,
"analyzer": "standard"
}
}
}
}
- Delete Index: Permanently remove the index.
DELETE localhost:9200/articles_db
- Index Document: Create or update a specific document by ID.
POST localhost:9200/articles_db/_doc/101
Content-Type: application/json
{
"doc_uid": 101,
"headline": "New Document Entry",
"main_body": "This represents sample content for testing purposes"
}
- Delete Document: Remove a specific entry.
DELETE localhost:9200/articles_db/_doc/101
- Retrieve Document: Fetch details for a single record.
GET localhost:9200/articles_db/_doc/101
- Keyword Search (Term Match): Exact matching on specific tokens.
POST localhost:9200/articles_db/_search
Content-Type: application/json
{
"query": {
"term": {
"main_body": "sample"
}
}
}
- Phrase/Query Search: Search across multiple fields with operator logic.
POST localhost:9200/articles_db/_search
Content-Type: application/json
{
"query": {
"query_string": {
"default_field": "headline",
"query": "entry testing"
}
}
}
- Analyze API Test: Inspect how a text segment is tokenized.
POST localhost:9200/_analyze
Content-Type: application/json
{
"analyzer": "standard",
"text": "This is a simple test"
}
Standard analyzers often split Chinese text inefficiently. Integrate the analysis-ik plugin to optimize search relevance for Chinese characters.
- Download Plugin: Obtain the latest release archive from the official GitHub repository.
https://github.com/medcl/elasticsearch-analysis-ik/archive/master.zip
- Compilation: Unzip the package and navigate to the directory to build the artifacts.
mvn package
-
Deployment: Move the compiled zip located in
target/releases/to the plugin folder (plugins/) of your Elasticsearch installation. Extract it and name the folderik. -
Version Verification: Open
plugin-descriptor.propertiesinside the new plugin directory to confirm compatibility with your running Elasticsearch version number. -
Verification: Restart the service and validate splitting granularity. Options include
ik_smart(coarse) andik_max_word(fine-grained).
POST localhost:9200/_analyze
Content-Type: application/json
{
"analyzer": "ik_smart",
"text": "这是一段测试文本"
}
To create a resilient cluster, multiple nodes must communicate securely. Ensure storage directories are cleaned before starting a fresh topology.
Configure the elasticsearch.yml file for each participating node to define unique identifiers and network interfaces.
# Unique identifier for the cluster group
cluster.name: my-elasticsearch-cluster
# Distinct name for this specific node instance
node.name: node-alpha
# Bind address (Use local IP or wildcard in production)
network.host: 0.0.0.0
# HTTP transport port (Must be unique per host)
http.port: 9205
# Inter-node communication port (Must be unique per host)
transport.tcp.port: 9305
# Discovery hosts for initial seed list
discovery.zen.ping.unicast.hosts: ["127.0.0.1:9305", "127.0.0.1:9306", "127.0.0.1:9307"]
# Designate the initial master node for bootstrapping
cluster.initial_master_nodes: ["127.0.0.1:9305"]
Repeat this configuration on other nodes, altering only node.name, http.port, transport.tcp.port, and the relevant master list entry to prevent conflicts.