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.

Architect
Architect
Architect
Installing and Configuring Elasticsearch for Site Search with IK Analyzer and Node.js Client

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-headless Download 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
./elasticsearch

Verify with: curl -XGET http://127.0.0.1:9200/?pretty Adjust ~/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.txt

Create ~/es_root/config/analysis/synonym.txt with entries such as:

ua,user-agent,userAgent
js,javascript
internet explore=>ie

Use JavaScript API

Install the official Node.js client: npm install elasticsearch --save Instantiate 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.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

search engineElasticsearchNode.jsInstallationIK AnalyzerSynonyms
Architect
Written by

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.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.