node使用elasticsearch
实例化对象
npm i - S elasticsearch
const elasticsearch = require ( 'elasticsearch' ) ;
host: http: / / ip: port
host: http: / / user: password@ip: port
const client = new elasticsearch. Client ( {
host: 'http://test:test@127.0.0.1:9500' ,
log: 'trace' ,
apiVersion: '7.6' ,
} ) ;
const client = new elasticsearch. Client ( {
hosts: [
'https://user:pass@box1.server.org:9200' ,
'https://user:pass@box2.server.org:9200'
]
} ) ;
添加数据
await client. create ( {
index: 'database' ,
type: 'datasheet' ,
id: '1' ,
body: {
test: 'add...'
}
} ) ;
await client. index ( {
index: 'database' ,
type: 'datasheet' ,
body: {
character: 'Daenerys Targaryen' ,
quote: 'I am the blood of the dragon.'
}
} )
获取数据
const response = await client. get ( {
index: 'myindex' ,
type: 'mytype' ,
id: 1
} ) ;
const response = await client. mget ( {
body: {
docs: [
{ _index: 'indexA' , _type: 'typeA' , _id: '1' } ,
{ _index: 'indexB' , _type: 'typeB' , _id: '1' } ,
{ _index: 'indexC' , _type: 'typeC' , _id: '1' }
]
}
} )
const response = await client. mget ( {
index: 'myindex' ,
type: 'mytype' ,
body: {
ids: [ 1 , 2 , 3 ]
}
} ) ;
删除数据
await client. delete ( {
index: 'database' ,
type: 'datasheet' ,
id: '1'
} ) ;
await client. update ( {
index: 'rest' ,
id: '1v2zG34BnY7le6vibRiv' ,
body: {
script : {
inline: "ctx._source.arr.removeIf(item -> item.name == '邓紫棋')"
}
}
} )
更新数据
await client. index ( {
index: 'database' ,
type: 'datasheet' ,
id: '1' ,
body: {
title: 'cascsa'
}
} ) ;
await client. update ( {
index: 'database' ,
type: 'datasheet' ,
id: '1' ,
body: {
title: 'cascsa'
}
} ) ;
const response = await client. update ( {
index: 'database' ,
type: 'datasheet' ,
id: '1' ,
body: {
script: 'ctx._source.title += title' ,
params: { title: '字符串追加内容' }
}
} ) ;
await client. update ( {
index: 'rest' ,
id: '1v2zG34BnY7le6vibRiv' ,
body: {
script : {
inline: "ctx._source.arr.add(params.item)" ,
params: {
item: {
name: '阿细' ,
title: '一双手'
}
}
}
}
} )
await client. update ( {
index: 'rest' ,
id: '1v2zG34BnY7le6vibRiv' ,
body: {
script : {
inline: "for(e in ctx._source.arr){if (e.name == '阿细') {e.name=params.name}}" ,
params: {
name: '邓紫棋'
}
}
}
} )
await client. update ( {
index: 'database' ,
type: 'datasheet' ,
id: '6' ,
body: {
doc: {
title: 'test' ,
tags: '51213'
}
}
} )
搜索数据
match
const response = await client. search ( {
index: 'database' ,
type: 'datasheet' ,
body: {
query: {
match: {
title: '测试'
}
}
}
} ) ;
match: {
title: '*测试*'
}
match_all
const response = await client. search ( {
index: 'database' ,
type: 'datasheet' ,
body: {
query: {
match_all: { }
}
}
} )
bool
filter
const response = await client. search ( {
index: 'database' ,
type: 'datasheet' ,
body: {
query: {
bool: {
filter: {
range: {
count: {
gte: 90
}
}
}
}
} ,
}
} )
gt
大于
gte
大于等于
lt
小于
lte
小于等于
must
const response = await client. search ( {
index: 'database' ,
type: 'datasheet' ,
body: {
query: {
bool: {
must: [
{
match: {
count: 86
}
} ,
{
match: {
test: '*试*'
}
}
]
}
} ,
}
} )
should
const response = await client. search ( {
index: 'database' ,
type: 'datasheet' ,
size: 100 ,
from : 0 ,
body: {
query: {
bool: {
should: [
{
match: {
count: 100
}
} ,
{
match: {
test: 'title'
}
}
]
}
} ,
}
} )
must_not
const response = await client. search ( {
index: 'database' ,
type: 'datasheet' ,
size: 100 ,
from : 0 ,
body: {
query: {
bool: {
must_not: [
{
match: {
count: 86
}
} ,
{
match: {
title: '745' ,
}
}
]
}
} ,
}
} )
const response = await client. search ( {
index: 'database' ,
type: 'datasheet' ,
size: 100 ,
from : 0 ,
body: {
query: {
bool: {
must_not: [
{
term: {
title: 'test'
}
} ,
{
term: {
title: '745' ,
}
} ,
{
term: {
title: 'mysql' ,
}
}
]
}
} ,
}
} )
term
const response = await client. search ( {
index: 'database' ,
type: 'datasheet' ,
size: 100 ,
from : 0 ,
body: {
query: {
term: {
title: 'mysql'
}
}
}
} )
terms
const response = await client. search ( {
index: 'database' ,
type: 'datasheet' ,
size: 100 ,
from : 0 ,
body: {
query: {
terms: {
title: [ 'mysql' , '745' ]
}
} ,
}
} )
sort
const response = await client. search ( {
index: 'databases' ,
type: 'datasheet' ,
size: 100 ,
from : 0 ,
body: {
sort: {
"_id" : {
"order" : "desc"
}
}
}
} )
desc 倒序
asc 升序
分页
const response = await client. search ( {
index: 'database' ,
type: 'datasheet' ,
size: 30 ,
from : 0 ,
body: {
query: {
match_all: { }
}
}
} )
嵌套对象查询
const response = await client. search ( {
index: 'rest02' ,
body: {
query: {
match: {
"demo.name" : "邓紫棋"
}
}
}
} )
const response = await client. search ( {
index: 'rest' ,
body: {
query: {
nested: {
path: "arr" ,
query: {
match: {
"arr.name" : "邓紫棋"
}
}
}
}
}
} )
过滤返回字段
const response = await client. search ( {
index: 'database' ,
type: 'datasheet' ,
size: 100 ,
from : 0 ,
body: {
_source: [ 'title' ]
}
} )
const response = await client. search ( {
index: 'database' ,
type: 'datasheet' ,
size: 100 ,
from : 0 ,
body: {
_source: {
includes: [ 'title' , 'test' ] ,
excludes: [ 'test' ]
}
}
} )
获取数量
const { count } = await client. count ( )
const { count } = await client. count ( {
index: 'index_name'
} )
判断是否存在
await client. exists ( {
index: 'database' ,
type: 'datasheet' ,
id: 1
} ) ;
执行多个搜索
const response = await client. msearch ( {
body: [
{ index: 'database' , type: 'datasheet' } ,
{ query: { match_all: { } } } ,
{ index: 'databases' , type: 'datasheet' } ,
{ query: { match_all: { } } }
]
} )
批量执行请求
client. bulk ( {
body: [
{ index: { _index: 'myindex' , _type: 'mytype' , _id: 1 } } ,
{ title: 'foo' } ,
{ update: { _index: 'myindex' , _type: 'mytype' , _id: 2 } } ,
{ doc: { title: 'foo' } } ,
{ delete : { _index: 'myindex' , _type: 'mytype' , _id: 3 } } ,
]
} )
数据类型
字符串类型:text、keyword
数值型:long、integer、short、byte、double、float、half、scaled
日期型:date
布尔型:boolean
二进制型:binary
对象:object
数组对象:nested
等等...
创建索引
await client. indices. create ( {
index: 'rest' ,
body: {
settings : {
"index" : {
"number_of_shards" : 3 ,
"number_of_replicas" : 2
}
} ,
mappings : {
properties : {
title : {
type : "text" ,
analyzer: "ik_max_word" ,
search_analyzer: "ik_max_word"
} ,
demo: {
type: 'object' ,
properties: {
"name" : { type: "text" } ,
"title" : { type: "text" }
}
} ,
arr: {
type: "nested" ,
properties: {
name: { type: "text" } ,
age: { type: "long" }
}
} ,
age : {
type : "long"
} ,
log_time : {
"type" : "date" ,
"format" : "yyyy-MM-dd HH:mm:ss||epoch_millis"
}
}
}
}
} )
获取索引结构
const response = await client. indices. getMapping ( {
index: 'rest'
} )
追加索引
await client. indices. putMapping ( {
index: 'rest' ,
body: {
properties: {
demo_test: {
type: 'text'
}
}
}
} )
await client. indices. putMapping ( {
index: 'rest' ,
body: {
properties: {
arr: {
type: "nested" ,
properties: {
test: { type: "text" }
}
}
}
}
} )
await client. indices. putMapping ( {
index: 'rest' ,
body: {
properties: {
demo: {
type: 'object' ,
properties: {
text: { type: "text" }
}
}
}
}
} )
删除索引
client. indices. delete ( {
index: 'test'
} )
关于
由于type属性已经在7.0 + 版本废除,所以以上的type可以没有,本文档借鉴 https: / / github. com/ elastic/ elasticsearch- js- legacy/ blob/ 16. x/ docs/ api_methods_7_5. asciidoc