10
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Node.jsからElasticSearch検索

Posted at

はじめに

曖昧検索、全文検索はElasticSearchのほうがよいと思います。
Node.jsからの利用をする際には、直接ElasticSearchのAPIを呼ぶか、elasticsearchモジュールを利用するかなどの選択肢がありますが、
elasticsearchモジュールを使ったほうが簡単で効率です。

ElasticSearchインストール

npm install elasticsearch

クライアント生成

const elasticsearch = require('elasticsearch');
const client = new elasticsearch.Client({
  host: 'localhost:9200',
  log: 'trace'
});

検索例

const response = await client.search({
  index: 'blogs',
  body: {
    query: {
      "multi_match" : {
        "query":    "Node.js",
        "fields": [ "title", "*_tag" ] 
      }
    }
  }
})

 
for (const blog of response.hits.hits) {
  console.log('blog:', blog);
}

検索クエリの書き方は下記記事参照:
https://www.elastic.co/guide/en/elasticsearch/reference/current/full-text-queries.html

AWSのElasticSearchの場合は開発ガイドがあります。
https://docs.aws.amazon.com/elasticsearch-service/latest/developerguide/what-is-amazon-elasticsearch-service.html

ElasticSearch新しいJavascriptのクライアントがリリースれました。
https://www.elastic.co/blog/new-elasticsearch-javascript-client-released

以上

10
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?