elasticsearch笔记


一、安装

文件源:https://mirrors.tuna.tsinghua.edu.cn/elasticstack/7.x/yum/

wget https://mirrors.tuna.tsinghua.edu.cn/elasticstack/7.x/yum/7.8.1/elasticsearch-7.8.1-x86_64.rpm

rpm -ivh elasticsearch-7.8.1-x86_64.rpm

编辑:/etc/elasticsearch/elasticsearch.yml

# cat /etc/elasticsearch/elasticsearch.yml|grep -v "^#"
path.data: /var/lib/elasticsearch
path.logs: /var/log/elasticsearch
network.host: 0.0.0.0
http.port: 9200
discovery.seed_hosts: ["0.0.0.0", "::"]

service elasticsearch.service restart

二、基本使用

[root@baihua ~]# curl http://39.104.117.27:9200/
{
    "name":"temp802",
    "cluster_name":"elasticsearch",
    "cluster_uuid":"GpWqqxKfSIOnmzbtmuMnvA",
    "version":{
        "number":"7.8.1",
        "build_flavor":"default",
        "build_type":"rpm",
        "build_hash":"b5ca9c58fb664ca8bf9e4057fc229b3396bf3a89",
        "build_date":"2020-07-21T16:40:44.668009Z",
        "build_snapshot":false,
        "lucene_version":"8.5.1",
        "minimum_wire_compatibility_version":"6.8.0",
        "minimum_index_compatibility_version":"6.0.0-beta1"
    },
    "tagline":"You Know, for Search"
}

写入数据

POST http://39.104.117.27:9200/home/index

{
    "id":5,
    "name":"李四"
}

查询数据

GET http://39.104.117.27:9200/home/index/_search

{
    "query":{
        "match":{
            "id":7
        }
    }
}
{
    "took": 381,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 1,
            "relation": "eq"
        },
        "max_score": 1.0,
        "hits": [
            {
                "_index": "home",
                "_type": "index",
                "_id": "o4VHmnMBfIxNROFX3s7B",
                "_score": 1.0,
                "_source": {
                    "id": 7,
                    "name": "张三"
                }
            }
        ]
    }
}

查看有哪些库

GET http://39.104.117.27:9200/_cat/indices?v

删除数据库

DELETE http://39.104.117.27:9200/home

{
    "acknowledged": true
}

三、汇聚查询

(1)WHERE查询

{"query":{"match":{"yid":"yid\_5cf367e9ac0b3"}}}

(2)范围查询

{"query":{"range":{"stop":{"gte":7,"lte":10}}}}

(3)计算总和

{"aggs":{"sum\_stop":{"sum":{"field":"stop"}}}}

(4)数据分组

{"size":0,"aggs":{"all\_stop":{"terms":{"field":"stop"}}}}

{"size":0,"aggs":{"yids":{"terms":{"field":"yid.keyword","size":10}}}}

{"size":0,"aggs":{"urls":{"terms":{"field":"url.keyword"},"aggs":{"yids":{"terms":{"field":"yid.keyword","size":1}}}}}}

(5)数据计数

es.yongdongli.net:9100/home/index/\_count

(6)正则查询某个字段

{"size":20,"query":{"regexp":{"url":"find(.\*)"}}}

(7)查询某个字段总和

{"size":0,"aggs":{"sum\_stop":{"sum":{"field":"stop"}}}}

{"size":0,"aggs":{"sum\_stop":{"terms":{"field":"url.keyword","size":60}}}}

(8)时间分组按小时

{
    "size":0,
    "aggs":{
        "group_by_create_time":{
            "date_histogram":{
                "field":"create_time",
                "interval":"1h",
                "format":"yyyy-MM-dd HH",
                "min_doc_count":0,
                "extended_bounds":{
                    "min":1588000000000,
                    "max":1588134534000
                }
            }
        }
    },
    "query":{
        "bool":{
            "must":{
                "range":{
                    "create_time":{
                        "gte":1588000000000,
                        "lte":1588134534000
                    }
                }
            }
        }
    }
}

(9)去重计数

{
    "size":0,
    "aggs":{
        "all_stop":{
            "cardinality":{
                "field":"yid.keyword"
            }
        }
    }
}

(10)综合匹配

根据某个字段倒序,某个字段匹配正则,某个字段为空时查询,去重yid字段

{
    "size":20,
    "query":{
        "bool":{
            "must":{
                "regexp":{
                    "url":"find(.*)"
                }
            },
            "must_not":{
                "term":{
                    "yid":""
                }
            }
        }
    },
    "collapse":{
        "field":"yid.keyword"
    },
    "sort":[
        {
            "create_time":{
                "order":"desc"
            }
        }
    ]
}

四、项目使用

PHP项目composer安装插件

composer require elasticsearch/elasticsearch

五、总结

暂无总结