Installing and Configuring Elasticsearch for Site Search with IK Analyzer and Node.js Client
This article walks through installing Elasticsearch on Ubuntu, adding the IK Chinese analyzer plugin, configuring synonym filters, and using the official Node.js client to create indices, mappings, and perform full‑text searches with custom analyzers and highlighting.
Installation Elasticsearch
The simplest deployment uses the Elasticsearch Dockerfile, but the author installs it manually on Ubuntu 14.04.3 LTS, version 2.1.1. First ensure Java is present:
sudo apt-get install openjdk-7-jre-headlessDownload and unzip the package, rename the directory to ~/es_root , make the binary executable, and start the service:
cd ~/es_root/bin/
chmod a+x elasticsearch
./elasticsearchVerify with:
curl -XGET http://127.0.0.1:9200/?prettyAdjust ~/es_root/config/elasticsearch.yml to bind to all interfaces for debugging (not recommended in production):
network.bind_host: "0.0.0.0"
network.publish_host: _non_loopback:ipv4_Install IK Analysis
Elasticsearch’s default tokenizer splits Chinese characters individually, so the IK analyzer plugin is required. Download the matching version, compile with Maven, and copy the built zip into the plugins directory:
wget -c https://github.com/medcl/elasticsearch-analysis-ik/archive/master.zip
unzip master.zip
sudo apt-get install maven
cd elasticsearch-analysis-ik-master/
mvn package
mkdir -p ~/es_root/plugins/ik/
unzip target/releases/elasticsearch-analysis-ik-1.6.2.zip -d ~/es_root/plugins/ik/Copy the configuration files to ~/es_root/config/ik . After restarting Elasticsearch you should see:
[plugins] [Libra] loaded [elasticsearch-analysis-ik]Configure Synonyms
Add a custom analyzer that combines the IK tokenizer with a synonym filter by editing ~/es_root/config/elasticsearch.yml :
index:
analysis:
analyzer:
ik_syno:
type: custom
tokenizer: ik_max_word
filter: [my_synonym_filter]
ik_syno_smart:
type: custom
tokenizer: ik_smart
filter: [my_synonym_filter]
filter:
my_synonym_filter:
type: synonym
synonyms_path: analysis/synonym.txtCreate ~/es_root/config/analysis/synonym.txt with entries such as:
ua,user-agent,userAgent
js,javascript
internet explore=>ieUse JavaScript API
Install the official Node.js client:
npm install elasticsearch --saveInstantiate the client:
var elasticsearch = require('elasticsearch');
var client = new elasticsearch.Client({
host: '10.211.55.23:9200',
log: 'trace'
});Both callback and promise styles are supported; the article uses promises for brevity.
Full‑Text Search
Create an index and mapping that uses the custom ik_syno analyzer for title , content , and tags fields:
client.indices.create({index : 'test'});
client.indices.putMapping({
index : 'test',
type : 'article',
body : {
article: {
properties: {
title: {type: 'string', term_vector: 'with_positions_offsets', analyzer: 'ik_syno', search_analyzer: 'ik_syno'},
content: {type: 'string', term_vector: 'with_positions_offsets', analyzer: 'ik_syno', search_analyzer: 'ik_syno'},
tags: {type: 'string', index: 'not_analyzed'},
slug: {type: 'string'},
update_date: {type: 'date', index: 'not_analyzed'}
}
}
}
});Index a sample document:
client.index({
index : 'test',
type : 'article',
id : '100',
body : {
title : '什么是 JS?',
slug :'what-is-js',
tags : ['JS', 'JavaScript', 'TEST'],
content : 'JS 是 JavaScript 的缩写!',
update_date : '2015-12-15T13:05:55Z'
}
});Search with a simple query:
client.search({index : 'test', type : 'article', q : 'JS'});For more advanced queries, the article provides a complex Query DSL that combines match clauses on title , content , tags , and slug with boosting, minimum‑should‑match thresholds, and result highlighting.
The author notes that the setup is a minimal example; production use should include more data, additional synonym files, and careful security configuration.
Architect
Professional architect sharing high‑quality architecture insights. Topics include high‑availability, high‑performance, high‑stability architectures, big data, machine learning, Java, system and distributed architecture, AI, and practical large‑scale architecture case studies. Open to ideas‑driven architects who enjoy sharing and learning.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.