1. 全文搜索 #

2. 安装 #

  1. 安装 jdk
  2. 安装 elasticsearch

3. 访问 #

http://localhost:9200/

{
  "name" : "PC-201704292335", //
  "cluster_name" : "elasticsearch"//集群名称
  "cluster_uuid" : "pb4rTAeoSxyLgJGtyz4fAg",//集群ID
  "version" : {
    "number" : "5.6.10",
    "build_hash" : "b727a60",
    "build_date" : "2018-06-06T15:48:34.860Z",
    "build_snapshot" : false,
    "lucene_version" : "6.6.1"
  },
  "tagline" : "You Know, for Search"
}

4. 基本概念 #

4.1 节点和集群 #

4.2 索引 #

4.3 文档 #

4.4 类型(Type) #

5. 操作Index #

5.1 创建索引 #

curl -X PUT 'http://localhost:9200/student'

5.2 删除索引 #

curl -X DELETE 'http://localhost:9200/student'

6. 数据操作 #

6.1 新增文档 #

curl -X PUT 'http://localhost:9200/student/city/1' -d`
{
    "name":"张三",
    "age":5,
    "city":"北京"
}
`
curl -X POST 'http://localhost:9200/student/city' -d`
{
    "name":"赵六",
    "age":7,
    "city":"江苏"
}
`

6.2 查看文档 #

curl 'http://localhost:9200/student/city/1'

6.3 更新记录 #

curl -X PUT 'http://localhost:9200/student/city/1' -d`
{
    "name":"张三2",
    "age":55,
    "city":"北京2"
}

6.4 删除文档 #

curl -X DELETE 'http://localhost:9200/student/city/1'

7. 数据查询 #

7.1 查询全部 #

curl 'http://localhost:9200/student/city/_search'

7.2 全文搜索 #

curl 'http://localhost:9200/student/city/_search' -d `
{
  "query" : { "match" : { "name" : "李" }},
  "size":1,
  "from":1
}
`

7.3 OR #

curl 'http://localhost:9200/student/city/_search' -d `
{
  "query" : { "match" : { "name" : "赵 李" }}
}
`

7.4 AND #

curl 'http://localhost:9200/student/city/_search' -d `
{
  "query": {
    "bool": {
      "must": [
        { "match": { "name": "赵" } },
        { "match": { "name": "六" } }
      ]
    }
  }
}
`

8. node中如何用 #

var elasticsearch = require('elasticsearch');
var client = new elasticsearch.Client({
    host: 'localhost:9200',
    log: 'trace'
});
/**
client.search({
    index: 'student',
    type: 'city',
    body: {
        query: {
            match: {
                name: '赵'
            }
        }
    }
}).then(ret => {
    console.log(ret.hits.hits);
});
 */
(async function () {
    let name = Date.now();
    let id = Date.now();
    const created = await client.create({
        index: 'student',
        type: 'city',
        id,
        body: {
            name,
            age: 10
        }
    });
    console.log(created);
    const updated = await client.update({
        index: 'student',
        type: 'city',
        id,
        body: {
            doc: {
                name: name,
                age: 101
            }
        }
    });
    console.log(created);
    //shift+alt+a
    /*  const deleted = await client.delete({
         index: 'student',
         type: 'city',
         id
     }); */
    console.log(deleted);
})().then(ret => console.log(ret), err => console.log(err));

// 

9. 参考 #