您的位置:

Mac Elasticsearch详细解析

一、简介

Mac Elasticsearch是一款用于实时搜索和分析的开源搜索引擎,由Apache Lucene构建而成,可通过HTTP请求进行数据搜索、存储及分析。该搜索引擎具有快速、稳定、可扩展性强等特点,广泛应用于全文搜索、日志分析、应用程序性能监测等领域。

二、部署和配置

1、安装Java运行环境


brew cask install java

2、下载elasticsearch安装包,并解压


wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.13.1-darwin-x86_64.tar.gz
tar zxvf elasticsearch-7.13.1-darwin-x86_64.tar.gz

3、修改配置文件


cd elasticsearch-7.13.1/config
vim elasticsearch.yml

在文件中添加以下内容:


cluster.name: my-application
node.name: node-1
path.data: /path/to/data
path.logs: /path/to/logs
network.host: 0.0.0.0
http.port: 9200

4、启动elasticsearch


cd elasticsearch-7.13.1/bin
./elasticsearch

三、索引和搜索

1、创建索引


PUT /my-index
{
  "settings": {
    "number_of_shards": 3,
    "number_of_replicas": 2
  }
}

2、批量添加数据


POST /my-index/_bulk
{ "index": { "_id": "1" }}
{ "title": "Mac Elasticsearch", "body": "This is a great search engine." }
{ "index": { "_id": "2" }}
{ "title": "Java Programming", "body": "Java is a popular programming language." }

3、搜索数据


GET /my-index/_search?q=title:elasticsearch

返回结果:


{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 3,
    "successful": 3,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 1,
      "relation": "eq"
    },
    "max_score": 0.18232156,
    "hits": [
      {
        "_index": "my-index",
        "_type": "_doc",
        "_id": "1",
        "_score": 0.18232156,
        "_source": {
          "title": "Mac Elasticsearch",
          "body": "This is a great search engine."
        }
      }
    ]
  }
}

四、聚合和过滤

1、按照年龄分组,统计每组人数


GET /people/_search
{
  "size": 0,
  "aggs": {
    "age_groups": {
      "terms": {
        "field": "age"
      }
    }
  }
}

2、按照年龄分组,统计每个分组中身高最低的人的信息


GET /people/_search
{
  "size": 0,
  "aggs": {
    "age_groups": {
      "terms": {
        "field": "age"
      },
      "aggs": {
        "shortest_person": {
          "top_hits": {
            "sort": [
              {
                "height": {
                  "order": "asc"
                }
              }
            ],
            "size": 1
          }
        }
      }
    }
  }
}

3、过滤结果,只显示身高在170cm以上的人


GET /people/_search
{
  "query": {
    "bool": {
      "must": [
        { "match_all": {} }
      ],
      "filter": [
        { "range": { "height": { "gte": 170 } } }
      ]
    }
  }
}

五、常用API

1、索引API


PUT /my-index
POST /my-index/_doc/1
{
  "title": "Mac Elasticsearch",
  "body": "This is a great search engine."
}
GET /my-index/_doc/1
DELETE /my-index/_doc/1

2、搜索API


GET /my-index/_search?q=title:elasticsearch
GET /my-index/_search
{
  "query": {
    "match": {
      "title": "elasticsearch"
    }
  }
}

3、聚合API


GET /my-index/_search
{
  "size": 0,
  "aggs": {
    "age_groups": {
      "terms": {
        "field": "age"
      }
    }
  }
}

4、更新API


POST /my-index/_doc/1/_update
{
  "doc": {
    "title": "New title",
    "body": "This is a great search engine."
  }
}

5、删除API


DELETE /my-index

六、结论

Mac Elasticsearch是一款功能强大的搜索引擎,支持实时搜索和分析等功能,而且配置简单、API丰富,应用范围广泛。开发者可以根据自己的需求,通过索引和搜索实现全文搜索、日志分析、数据监测等功能,让应用程序性能更加优越。