首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >蛋疼的ElasticSearch(四)之基本用法、高级查询

蛋疼的ElasticSearch(四)之基本用法、高级查询

作者头像
用户2032165
发布2018-12-04 09:41:09
5260
发布2018-12-04 09:41:09
举报

ElasticSearch系列文章:

1.蛋疼的ElasticSearch(一)之安装ElasticSearch

2.蛋疼的ElasticSearch(二)之配置ElasticSearch Head插件

3.蛋疼的ElasticSearch(三)之配置elasticsearch-analysis-ik和集群

回顾

搭建好了elasticsearch集群,通过elasticsearch-head插件可以监控各节点健康状态。

通过health命令http://192.168.10.6:9200/_cat/health?v,可以看出集群处于健康状态。

image.png

创建索引

我们搭建的elasticsearch集群,一个master,2个slave。每一个节点的分片数是3个,备份数是1个。

image.png

ElasticSearch中的索引:是含有相同属性的文档集合。

ElasticSearch中的类型:索引可以定义一个或者多个类型,文档必须属于一个类型。比如一本图书的类型是语文。

ElasticSearch中的文档:文档是可以被索引的基本数据单位,比如一本高中人教版语文书。

分片:每个索引都有多个分片,每个分片都是一个Lucene索引。

备份:拷贝一份分片就完成了索引的备份。

下图中,边框加粗的分片是主分片,边框较浅的是备份分片。

image.png

ElasticSearch API基本格式:

http://<ip>:<port>/<索引>/<类型>/<文档id>

索引又分为结构化索引和非结构化索引。

在head插件,这样新建索引,就是一个非结构化索引。

image.png

非结构索引中的mappings属性是为空的,如下图所示。

image.png

基本用法

结构化索引,需要设置mappings属性的。ElasticSearch 6.0.0或者更高版本中创建的索引只能包含一个mapping。

结构化索引,类似MySQL,我们会对索引结构做预定义,包含字段名,字段类型。那么非结构化索引,就类似Mongo DB,索引结构未知。在根据具体的数据来update索引的mapping,结构化相比于非结构化,性能更好。非结构化比较灵活,只是频繁update索引mapping会有一定的性能损耗。

接下来,我们来创建一个结构化索引,索引名为people。

PUT 192.168.10.6:9200/pepole
{
    "settings" : {
        "number_of_shards":3,
        "number_of_replicas":1
    },
    "mappings":{
        "man":{
            "properties":{
                "name":{
                    "type":"text"
                },
                "country": {
                    "type":"keyword"
                },
                "age": {
                    "type":"integer"
                },
                "date": {
                    "type":"date",
                    "format":"yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
                }
            }
        }
    }
}

我们可以看到people索引是有mappings属性的。

image.png

people索引插入数据,指定文档id

POST 192.168.10.6:9200/pepole/man/2

{
    "name":"12345",
    "country":"guangzhou",
    "age":23,
    "date":"1995-06-06"
}

people索引插入数据,不指定文档id。elasticsearch会自动帮你创建一个文档id。

POST 192.168.10.6:9200/pepole/man/
{
    "name":"dada",
    "country":"guangzhou",
    "age":30,
    "date":"1987-06-06"
}

通过文档形式修改数据。

POST 192.168.10.6:9200/pepole/man/1/_update
{
    "doc":{
        "name":"123"
    }
}

通过脚本语言修改数据,painless是elasticsearch自带的脚本。

POST 192.168.10.6:9200/pepole/man/1/_update
{
    "script": {
        "lang":"painless",
        "inline":"ctx._source.age+=10"
    }
}
或者
{
    "script":{
        "lang":"painless",
        "inline":"ctx._source.age = params.age",
        "params":{
            "age":100
        }
    }
}

删除数据

DELETE 192.168.10.6:9200/pepole/man/1/

Query查询

在elasticsearch中查询,可以分为子条件查询和复合条件查询。

子条件查询:特定字段查询所特定值,子条件查询又可细分为Query Context和Filter Context。

复合条件查询:以一定的逻辑组合子条件查询。

Query Context:在查询过程中,除了判断文档是否满足查询条件外,es还会计算出一个_score来标识匹配的程序,旨在判断目标文档和查询条件匹配的有多好。

常用查询又分为全文本查询、字段级别查询。全文本查询是针对文本类型数据。字段级别查询是针对结构化数据,比如数字,日期。

我们按照上文的步骤,继续创建一个名为book的索引,mapping类型为novel。

PUT 192.168.10.6:9200/book
{
    "settings" : {
        "number_of_shards":3,
        "number_of_replicas":1
    },
    "mappings":{
        "man":{
            "properties":{
                "work_count":{
                    "type":"integer"
                },
                "author": {
                    "type":"keyword"
                },
                "title": {
                    "type":"text"
                },
                "publish_date": {
                    "type":"date",
                    "format":"yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
                }
            }
        }
    }
}

image.png

分页查询,在es中,from是从0开始的。

POST 192.168.10.6:9200/book/_search
{
    "query":{
        "match_all":{}
            
        },
        "from":2,
        "size":1
}
返回:
{
    "took": 17,
    "timed_out": false,
    "_shards": {
        "total": 3,
        "successful": 3,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 10,
        "max_score": 1,
        "hits": [
            {
                "_index": "book",
                "_type": "novel",
                "_id": "eg5rn2YBHFER5MTJQYIz",
                "_score": 1,
                "_source": {
                    "title": "教你怎么做麻辣香锅",
                    "author": "香锅",
                    "word_count": 8888,
                    "publish_date": "1996-03-01"
                }
            }
        ]
    }
}

按字段模糊匹配并按字段排序。因为我们指定了排序方式,_score字段都为null。

POST 192.168.10.6:9200/book/_search
{
    "query":{
        "match":{
            "title":"叫"
        }
    },
    "sort":[
        {
            "publish_date": {
                "order":"desc"
            }
        }
    ]
}
返回:
{
    "took": 199,
    "timed_out": false,
    "_shards": {
        "total": 3,
        "successful": 3,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 5,
        "max_score": null,
        "hits": [
            {
                "_index": "book",
                "_type": "novel",
                "_id": "eA5qn2YBHFER5MTJMoKT",
                "_score": null,
                "_source": {
                    "title": "叫你玩辅助",
                    "author": "卷毛",
                    "word_count": 5000,
                    "publish_date": "1997-03-01"
                },
                "sort": [
                    857174400000
                ]
            },
            {
                "_index": "book",
                "_type": "novel",
                "_id": "dw5pn2YBHFER5MTJ3oIc",
                "_score": null,
                "_source": {
                    "title": "叫你玩ADC",
                    "author": "微笑",
                    "word_count": 4000,
                    "publish_date": "1997-02-01"
                },
                "sort": [
                    854755200000
                ]
            },
            {
                "_index": "book",
                "_type": "novel",
                "_id": "dg5pn2YBHFER5MTJnYLt",
                "_score": null,
                "_source": {
                    "title": "叫你玩打野",
                    "author": "厂长",
                    "word_count": 3000,
                    "publish_date": "1996-02-01"
                },
                "sort": [
                    823132800000
                ]
            },
            {
                "_index": "book",
                "_type": "novel",
                "_id": "dQ5pn2YBHFER5MTJaoLT",
                "_score": null,
                "_source": {
                    "title": "叫你玩中单",
                    "author": "若风",
                    "word_count": 2000,
                    "publish_date": "1995-02-01"
                },
                "sort": [
                    791596800000
                ]
            },
            {
                "_index": "book",
                "_type": "novel",
                "_id": "1",
                "_score": null,
                "_source": {
                    "title": "叫你玩上单",
                    "author": "草莓",
                    "word_count": 1000,
                    "publish_date": "1995-01-01"
                },
                "sort": [
                    788918400000
                ]
            }
        ]
    }
}

按字段聚合查询。

POST  192.168.10.6:9200/book/_search
{
    "aggs":{
        "group_by_word_count": {
            "terms": {
                "field":"word_count"
            }
        },
        "group_by_publish_date":{
            "terms":{
                "field":"publish_date"
            }
        }
    }
}
返回:
{
    "took": 387,
    "timed_out": false,
    "_shards": {
        "total": 3,
        "successful": 3,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 10,
        "max_score": 1,
        "hits": [
            {
                "_index": "book",
                "_type": "novel",
                "_id": "dw5pn2YBHFER5MTJ3oIc",
                "_score": 1,
                "_source": {
                    "title": "叫你玩ADC",
                    "author": "微笑",
                    "word_count": 4000,
                    "publish_date": "1997-02-01"
                }
            },
            {
                "_index": "book",
                "_type": "novel",
                "_id": "eA5qn2YBHFER5MTJMoKT",
                "_score": 1,
                "_source": {
                    "title": "叫你玩辅助",
                    "author": "卷毛",
                    "word_count": 5000,
                    "publish_date": "1997-03-01"
                }
            },
            {
                "_index": "book",
                "_type": "novel",
                "_id": "eg5rn2YBHFER5MTJQYIz",
                "_score": 1,
                "_source": {
                    "title": "教你怎么做麻辣香锅",
                    "author": "香锅",
                    "word_count": 8888,
                    "publish_date": "1996-03-01"
                }
            },
            {
                "_index": "book",
                "_type": "novel",
                "_id": "ew57n2YBHFER5MTJt4Ky",
                "_score": 1,
                "_source": {
                    "title": "UZI的坑锅之路",
                    "author": "uzi",
                    "word_count": 1000,
                    "publish_date": "1996-03-01"
                }
            },
            {
                "_index": "book",
                "_type": "novel",
                "_id": "dg5pn2YBHFER5MTJnYLt",
                "_score": 1,
                "_source": {
                    "title": "叫你玩打野",
                    "author": "厂长",
                    "word_count": 3000,
                    "publish_date": "1996-02-01"
                }
            },
            {
                "_index": "book",
                "_type": "novel",
                "_id": "vxAspmYBI-Y9epepwYvX",
                "_score": 1,
                "_source": {
                    "title": "微博热点-uzi不配洗白+2",
                    "author": "知名博主",
                    "word_count": "0",
                    "publish_date": 1540339200000
                }
            },
            {
                "_index": "book",
                "_type": "novel",
                "_id": "1",
                "_score": 1,
                "_source": {
                    "title": "叫你玩上单",
                    "author": "草莓",
                    "word_count": 1000,
                    "publish_date": "1995-01-01"
                }
            },
            {
                "_index": "book",
                "_type": "novel",
                "_id": "dQ5pn2YBHFER5MTJaoLT",
                "_score": 1,
                "_source": {
                    "title": "叫你玩中单",
                    "author": "若风",
                    "word_count": 2000,
                    "publish_date": "1995-02-01"
                }
            },
            {
                "_index": "book",
                "_type": "novel",
                "_id": "eQ5qn2YBHFER5MTJi4LZ",
                "_score": 1,
                "_source": {
                    "title": "教你如何自闭",
                    "author": "uzi",
                    "word_count": 6666,
                    "publish_date": "1995-03-01"
                }
            },
            {
                "_index": "book",
                "_type": "novel",
                "_id": "1jZJpWYBKYrkmyVA-wAp",
                "_score": 1,
                "_source": {
                    "title": "微博热点-uzi是弟弟",
                    "author": "微博",
                    "word_count": "1234",
                    "publish_date": 1540166400000
                }
            }
        ]
    },
    "aggregations": {
        "group_by_publish_date": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
                {
                    "key": 825638400000,
                    "key_as_string": "1996-03-01 00:00:00",
                    "doc_count": 2
                },
                {
                    "key": 788918400000,
                    "key_as_string": "1995-01-01 00:00:00",
                    "doc_count": 1
                },
                {
                    "key": 791596800000,
                    "key_as_string": "1995-02-01 00:00:00",
                    "doc_count": 1
                },
                {
                    "key": 794016000000,
                    "key_as_string": "1995-03-01 00:00:00",
                    "doc_count": 1
                },
                {
                    "key": 823132800000,
                    "key_as_string": "1996-02-01 00:00:00",
                    "doc_count": 1
                },
                {
                    "key": 854755200000,
                    "key_as_string": "1997-02-01 00:00:00",
                    "doc_count": 1
                },
                {
                    "key": 857174400000,
                    "key_as_string": "1997-03-01 00:00:00",
                    "doc_count": 1
                },
                {
                    "key": 1540166400000,
                    "key_as_string": "2018-10-22 00:00:00",
                    "doc_count": 1
                },
                {
                    "key": 1540339200000,
                    "key_as_string": "2018-10-24 00:00:00",
                    "doc_count": 1
                }
            ]
        },
        "group_by_word_count": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
                {
                    "key": 1000,
                    "doc_count": 2
                },
                {
                    "key": 0,
                    "doc_count": 1
                },
                {
                    "key": 1234,
                    "doc_count": 1
                },
                {
                    "key": 2000,
                    "doc_count": 1
                },
                {
                    "key": 3000,
                    "doc_count": 1
                },
                {
                    "key": 4000,
                    "doc_count": 1
                },
                {
                    "key": 5000,
                    "doc_count": 1
                },
                {
                    "key": 6666,
                    "doc_count": 1
                },
                {
                    "key": 8888,
                    "doc_count": 1
                }
            ]
        }
    }
}

全文本匹配查询。

POST 192.168.10.6:9200/book/_search
{
    "query": {
        "match_phrase": {
            "title":"uzi的坑锅之路"
        }
    }
}
返回:
{
    "took": 16,
    "timed_out": false,
    "_shards": {
        "total": 3,
        "successful": 3,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 1,
        "max_score": 6.7130113,
        "hits": [
            {
                "_index": "book",
                "_type": "novel",
                "_id": "ew57n2YBHFER5MTJt4Ky",
                "_score": 6.7130113,
                "_source": {
                    "title": "UZI的坑锅之路",
                    "author": "uzi",
                    "word_count": 1000,
                    "publish_date": "1996-03-01"
                }
            }
        ]
    }
}

字段统计。

POST 192.168.10.6:9200/book/_search
{
    "aggs":{
        "grades_word_count": {
            "stats": {
                "field":"word_count"
            }
        }
    }
}
返回:
{
    "took": 47,
    "timed_out": false,
    "_shards": {
        "total": 3,
        "successful": 3,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 10,
        "max_score": 1,
        "hits": [
            {
                "_index": "book",
                "_type": "novel",
                "_id": "dw5pn2YBHFER5MTJ3oIc",
                "_score": 1,
                "_source": {
                    "title": "叫你玩ADC",
                    "author": "微笑",
                    "word_count": 4000,
                    "publish_date": "1997-02-01"
                }
            },
            {
                "_index": "book",
                "_type": "novel",
                "_id": "eA5qn2YBHFER5MTJMoKT",
                "_score": 1,
                "_source": {
                    "title": "叫你玩辅助",
                    "author": "卷毛",
                    "word_count": 5000,
                    "publish_date": "1997-03-01"
                }
            },
            {
                "_index": "book",
                "_type": "novel",
                "_id": "eg5rn2YBHFER5MTJQYIz",
                "_score": 1,
                "_source": {
                    "title": "教你怎么做麻辣香锅",
                    "author": "香锅",
                    "word_count": 8888,
                    "publish_date": "1996-03-01"
                }
            },
            {
                "_index": "book",
                "_type": "novel",
                "_id": "ew57n2YBHFER5MTJt4Ky",
                "_score": 1,
                "_source": {
                    "title": "UZI的坑锅之路",
                    "author": "uzi",
                    "word_count": 1000,
                    "publish_date": "1996-03-01"
                }
            },
            {
                "_index": "book",
                "_type": "novel",
                "_id": "dg5pn2YBHFER5MTJnYLt",
                "_score": 1,
                "_source": {
                    "title": "叫你玩打野",
                    "author": "厂长",
                    "word_count": 3000,
                    "publish_date": "1996-02-01"
                }
            },
            {
                "_index": "book",
                "_type": "novel",
                "_id": "vxAspmYBI-Y9epepwYvX",
                "_score": 1,
                "_source": {
                    "title": "微博热点-uzi不配洗白+2",
                    "author": "知名博主",
                    "word_count": "0",
                    "publish_date": 1540339200000
                }
            },
            {
                "_index": "book",
                "_type": "novel",
                "_id": "1",
                "_score": 1,
                "_source": {
                    "title": "叫你玩上单",
                    "author": "草莓",
                    "word_count": 1000,
                    "publish_date": "1995-01-01"
                }
            },
            {
                "_index": "book",
                "_type": "novel",
                "_id": "dQ5pn2YBHFER5MTJaoLT",
                "_score": 1,
                "_source": {
                    "title": "叫你玩中单",
                    "author": "若风",
                    "word_count": 2000,
                    "publish_date": "1995-02-01"
                }
            },
            {
                "_index": "book",
                "_type": "novel",
                "_id": "eQ5qn2YBHFER5MTJi4LZ",
                "_score": 1,
                "_source": {
                    "title": "教你如何自闭",
                    "author": "uzi",
                    "word_count": 6666,
                    "publish_date": "1995-03-01"
                }
            },
            {
                "_index": "book",
                "_type": "novel",
                "_id": "1jZJpWYBKYrkmyVA-wAp",
                "_score": 1,
                "_source": {
                    "title": "微博热点-uzi是弟弟",
                    "author": "微博",
                    "word_count": "1234",
                    "publish_date": 1540166400000
                }
            }
        ]
    },
    "aggregations": {
        "grades_word_count": {
            "count": 10,
            "min": 0,
            "max": 8888,
            "avg": 3278.8,
            "sum": 32788
        }
    }
}

多文本匹配查询。

POST 192.168.10.6:9200/book/_search
{
    "query": {
        "multi_match": {
            "query":"uzi",
            "fields":["title", "author"]
        }
    }
}
返回:
{
    "took": 56,
    "timed_out": false,
    "_shards": {
        "total": 3,
        "successful": 3,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 4,
        "max_score": 1.2039728,
        "hits": [
            {
                "_index": "book",
                "_type": "novel",
                "_id": "ew57n2YBHFER5MTJt4Ky",
                "_score": 1.2039728,
                "_source": {
                    "title": "UZI的坑锅之路",
                    "author": "uzi",
                    "word_count": 1000,
                    "publish_date": "1996-03-01"
                }
            },
            {
                "_index": "book",
                "_type": "novel",
                "_id": "eQ5qn2YBHFER5MTJi4LZ",
                "_score": 1.2039728,
                "_source": {
                    "title": "教你如何自闭",
                    "author": "uzi",
                    "word_count": 6666,
                    "publish_date": "1995-03-01"
                }
            },
            {
                "_index": "book",
                "_type": "novel",
                "_id": "1jZJpWYBKYrkmyVA-wAp",
                "_score": 1.0594962,
                "_source": {
                    "title": "微博热点-uzi是弟弟",
                    "author": "微博",
                    "word_count": "1234",
                    "publish_date": 1540166400000
                }
            },
            {
                "_index": "book",
                "_type": "novel",
                "_id": "vxAspmYBI-Y9epepwYvX",
                "_score": 0.6099695,
                "_source": {
                    "title": "微博热点-uzi不配洗白+2",
                    "author": "知名博主",
                    "word_count": "0",
                    "publish_date": 1540339200000
                }
            }
        ]
    }
}

多文本匹配查询

POST http://192.168.10.6:9200/book/_search
{
    "query":{
        "query_string": {
            "query":"uzi OR UZI OR 微笑",
            "fields":["title","author"]
        }
    }
}
返回:
{
    "took": 98,
    "timed_out": false,
    "_shards": {
        "total": 3,
        "successful": 3,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 5,
        "max_score": 3.1784885,
        "hits": [
            {
                "_index": "book",
                "_type": "novel",
                "_id": "1jZJpWYBKYrkmyVA-wAp",
                "_score": 3.1784885,
                "_source": {
                    "title": "微博热点-uzi是弟弟",
                    "author": "微博",
                    "word_count": "1234",
                    "publish_date": 1540166400000
                }
            },
            {
                "_index": "book",
                "_type": "novel",
                "_id": "ew57n2YBHFER5MTJt4Ky",
                "_score": 2.4079456,
                "_source": {
                    "title": "UZI的坑锅之路",
                    "author": "uzi",
                    "word_count": 1000,
                    "publish_date": "1996-03-01"
                }
            },
            {
                "_index": "book",
                "_type": "novel",
                "_id": "vxAspmYBI-Y9epepwYvX",
                "_score": 1.8299085,
                "_source": {
                    "title": "微博热点-uzi不配洗白+2",
                    "author": "知名博主",
                    "word_count": "0",
                    "publish_date": 1540339200000
                }
            },
            {
                "_index": "book",
                "_type": "novel",
                "_id": "dw5pn2YBHFER5MTJ3oIc",
                "_score": 1.2039728,
                "_source": {
                    "title": "叫你玩ADC",
                    "author": "微笑",
                    "word_count": 4000,
                    "publish_date": "1997-02-01"
                }
            },
            {
                "_index": "book",
                "_type": "novel",
                "_id": "eQ5qn2YBHFER5MTJi4LZ",
                "_score": 1.2039728,
                "_source": {
                    "title": "教你如何自闭",
                    "author": "uzi",
                    "word_count": 6666,
                    "publish_date": "1995-03-01"
                }
            }
        ]
    }
}

字段级别查询,keyword是关键字不可切分,是全匹配的。

POST http://192.168.10.6:9200/book/_search
{
    "query": {
        "term": {
            "author":"uzi"
        }
    }
}
返回:
{
    "took": 14,
    "timed_out": false,
    "_shards": {
        "total": 3,
        "successful": 3,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 2,
        "max_score": 1.2039728,
        "hits": [
            {
                "_index": "book",
                "_type": "novel",
                "_id": "ew57n2YBHFER5MTJt4Ky",
                "_score": 1.2039728,
                "_source": {
                    "title": "UZI的坑锅之路",
                    "author": "uzi",
                    "word_count": 1000,
                    "publish_date": "1996-03-01"
                }
            },
            {
                "_index": "book",
                "_type": "novel",
                "_id": "eQ5qn2YBHFER5MTJi4LZ",
                "_score": 1.2039728,
                "_source": {
                    "title": "教你如何自闭",
                    "author": "uzi",
                    "word_count": 6666,
                    "publish_date": "1995-03-01"
                }
            }
        ]
    }
}

范围查询,gte和lte是闭区间。gt和lt是开区间。

POST http://192.168.10.6:9200/book/_search
{
    "query": {
        "range": {
            "word_count": {
                "gte":1000,
                "lte":2000
            }
        }
    }
}
或者
{
    "query": {
        "range": {
            "word_count": {
                "from":0,
                "to":1000
            }
        }
    }
}
返回:
{
    "took": 35,
    "timed_out": false,
    "_shards": {
        "total": 3,
        "successful": 3,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 4,
        "max_score": 1,
        "hits": [
            {
                "_index": "book",
                "_type": "novel",
                "_id": "ew57n2YBHFER5MTJt4Ky",
                "_score": 1,
                "_source": {
                    "title": "UZI的坑锅之路",
                    "author": "uzi",
                    "word_count": 1000,
                    "publish_date": "1996-03-01"
                }
            },
            {
                "_index": "book",
                "_type": "novel",
                "_id": "1",
                "_score": 1,
                "_source": {
                    "title": "叫你玩上单",
                    "author": "草莓",
                    "word_count": 1000,
                    "publish_date": "1995-01-01"
                }
            },
            {
                "_index": "book",
                "_type": "novel",
                "_id": "dQ5pn2YBHFER5MTJaoLT",
                "_score": 1,
                "_source": {
                    "title": "叫你玩中单",
                    "author": "若风",
                    "word_count": 2000,
                    "publish_date": "1995-02-01"
                }
            },
            {
                "_index": "book",
                "_type": "novel",
                "_id": "1jZJpWYBKYrkmyVA-wAp",
                "_score": 1,
                "_source": {
                    "title": "微博热点-uzi是弟弟",
                    "author": "微博",
                    "word_count": "1234",
                    "publish_date": 1540166400000
                }
            }
        ]
    }
}

时间范围查询

POST http://192.168.10.6:9200/book/_search
{
    "query": {
        "range": {
            "publish_date": {
                "gte":"1997-01-01",
                "lte":"now"
            }
        }
    }
}
返回:
{
    "took": 28,
    "timed_out": false,
    "_shards": {
        "total": 3,
        "successful": 3,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 4,
        "max_score": 1,
        "hits": [
            {
                "_index": "book",
                "_type": "novel",
                "_id": "dw5pn2YBHFER5MTJ3oIc",
                "_score": 1,
                "_source": {
                    "title": "叫你玩ADC",
                    "author": "微笑",
                    "word_count": 4000,
                    "publish_date": "1997-02-01"
                }
            },
            {
                "_index": "book",
                "_type": "novel",
                "_id": "eA5qn2YBHFER5MTJMoKT",
                "_score": 1,
                "_source": {
                    "title": "叫你玩辅助",
                    "author": "卷毛",
                    "word_count": 5000,
                    "publish_date": "1997-03-01"
                }
            },
            {
                "_index": "book",
                "_type": "novel",
                "_id": "vxAspmYBI-Y9epepwYvX",
                "_score": 1,
                "_source": {
                    "title": "微博热点-uzi不配洗白+2",
                    "author": "知名博主",
                    "word_count": "0",
                    "publish_date": 1540339200000
                }
            },
            {
                "_index": "book",
                "_type": "novel",
                "_id": "1jZJpWYBKYrkmyVA-wAp",
                "_score": 1,
                "_source": {
                    "title": "微博热点-uzi是弟弟",
                    "author": "微博",
                    "word_count": "1234",
                    "publish_date": 1540166400000
                }
            }
        ]
    }
}

Filter查询

Filter查询是在查询过程中,只判断该文档是否满足条件,只有YES或者No。

POST http://192.168.10.6:9200/book/_search
{
    "query": {
        "bool": {
            "filter": {
                "term": {
                    "word_count":1000
                }
            }
        }
    }
}
返回:
{
    "took": 8,
    "timed_out": false,
    "_shards": {
        "total": 3,
        "successful": 3,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 2,
        "max_score": 0,
        "hits": [
            {
                "_index": "book",
                "_type": "novel",
                "_id": "ew57n2YBHFER5MTJt4Ky",
                "_score": 0,
                "_source": {
                    "title": "UZI的坑锅之路",
                    "author": "uzi",
                    "word_count": 1000,
                    "publish_date": "1996-03-01"
                }
            },
            {
                "_index": "book",
                "_type": "novel",
                "_id": "1",
                "_score": 0,
                "_source": {
                    "title": "叫你玩上单",
                    "author": "草莓",
                    "word_count": 1000,
                    "publish_date": "1995-01-01"
                }
            }
        ]
    }
}

固定分数查询

POST http://192.168.10.6:9200/book/_search
{
    "query": {
        "constant_score": {
            "filter": {
                "match": {
                    "title":"uzi欠香锅一个世界冠军"
                }
            }
        }
    }
}
返回:
{
    "took": 15,
    "timed_out": false,
    "_shards": {
        "total": 3,
        "successful": 3,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 4,
        "max_score": 1,
        "hits": [
            {
                "_index": "book",
                "_type": "novel",
                "_id": "eg5rn2YBHFER5MTJQYIz",
                "_score": 1,
                "_source": {
                    "title": "教你怎么做麻辣香锅",
                    "author": "香锅",
                    "word_count": 8888,
                    "publish_date": "1996-03-01"
                }
            },
            {
                "_index": "book",
                "_type": "novel",
                "_id": "ew57n2YBHFER5MTJt4Ky",
                "_score": 1,
                "_source": {
                    "title": "UZI的坑锅之路",
                    "author": "uzi",
                    "word_count": 1000,
                    "publish_date": "1996-03-01"
                }
            },
            {
                "_index": "book",
                "_type": "novel",
                "_id": "vxAspmYBI-Y9epepwYvX",
                "_score": 1,
                "_source": {
                    "title": "微博热点-uzi不配洗白+2",
                    "author": "知名博主",
                    "word_count": "0",
                    "publish_date": 1540339200000
                }
            },
            {
                "_index": "book",
                "_type": "novel",
                "_id": "1jZJpWYBKYrkmyVA-wAp",
                "_score": 1,
                "_source": {
                    "title": "微博热点-uzi是弟弟",
                    "author": "微博",
                    "word_count": "1234",
                    "publish_date": 1540166400000
                }
            }
        ]
    }
}

固定分数查询,指定boost

POST http://192.168.10.6:9200/book/_search
{
    "query": {
        "constant_score": {
            "filter": {
                "match": {
                    "title":"uzi欠香锅一个世界冠军"
                }
            },
            "boost":40000
        }
    }
}
返回:
{
    "took": 56,
    "timed_out": false,
    "_shards": {
        "total": 3,
        "successful": 3,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 4,
        "max_score": 40000,
        "hits": [
            {
                "_index": "book",
                "_type": "novel",
                "_id": "eg5rn2YBHFER5MTJQYIz",
                "_score": 40000,
                "_source": {
                    "title": "教你怎么做麻辣香锅",
                    "author": "香锅",
                    "word_count": 8888,
                    "publish_date": "1996-03-01"
                }
            },
            {
                "_index": "book",
                "_type": "novel",
                "_id": "ew57n2YBHFER5MTJt4Ky",
                "_score": 40000,
                "_source": {
                    "title": "UZI的坑锅之路",
                    "author": "uzi",
                    "word_count": 1000,
                    "publish_date": "1996-03-01"
                }
            },
            {
                "_index": "book",
                "_type": "novel",
                "_id": "vxAspmYBI-Y9epepwYvX",
                "_score": 40000,
                "_source": {
                    "title": "微博热点-uzi不配洗白+2",
                    "author": "知名博主",
                    "word_count": "0",
                    "publish_date": 1540339200000
                }
            },
            {
                "_index": "book",
                "_type": "novel",
                "_id": "1jZJpWYBKYrkmyVA-wAp",
                "_score": 40000,
                "_source": {
                    "title": "微博热点-uzi是弟弟",
                    "author": "微博",
                    "word_count": "1234",
                    "publish_date": 1540166400000
                }
            }
        ]
    }
}

复杂查询

should,可以满足的条件。

POST http://192.168.10.6:9200/_search
{
    "query": {
        "bool": {
            "should": [
                {
                    "match": {
                        "author":"uzi"
                    }
                },
                {
                    "match": {
                        "title":"香锅"
                    }
                }
            ]
        }
    }
}
返回:
{
    "took": 19,
    "timed_out": false,
    "_shards": {
        "total": 9,
        "successful": 9,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 3,
        "max_score": 1.89712,
        "hits": [
            {
                "_index": "book",
                "_type": "novel",
                "_id": "ew57n2YBHFER5MTJt4Ky",
                "_score": 1.89712,
                "_source": {
                    "title": "UZI的坑锅之路",
                    "author": "uzi",
                    "word_count": 1000,
                    "publish_date": "1996-03-01"
                }
            },
            {
                "_index": "book",
                "_type": "novel",
                "_id": "eg5rn2YBHFER5MTJQYIz",
                "_score": 1.5749675,
                "_source": {
                    "title": "教你怎么做麻辣香锅",
                    "author": "香锅",
                    "word_count": 8888,
                    "publish_date": "1996-03-01"
                }
            },
            {
                "_index": "book",
                "_type": "novel",
                "_id": "eQ5qn2YBHFER5MTJi4LZ",
                "_score": 1.2039728,
                "_source": {
                    "title": "教你如何自闭",
                    "author": "uzi",
                    "word_count": 6666,
                    "publish_date": "1995-03-01"
                }
            }
        ]
    }
}

must,必须要满足的条件

POST http://192.168.10.6:9200/_search
{
    "query": {
        "bool": {
            "must":[
                {
                    "match": {
                        "title":"uzi"
                    }
                    
                },
                {
                    "match": {
                        "author":"uzi"
                    }
                }
            ]
        }
    }
}
返回:
{
    "took": 17,
    "timed_out": false,
    "_shards": {
        "total": 9,
        "successful": 9,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 1,
        "max_score": 2.4079456,
        "hits": [
            {
                "_index": "book",
                "_type": "novel",
                "_id": "ew57n2YBHFER5MTJt4Ky",
                "_score": 2.4079456,
                "_source": {
                    "title": "UZI的坑锅之路",
                    "author": "uzi",
                    "word_count": 1000,
                    "publish_date": "1996-03-01"
                }
            }
        ]
    }
}

must + filter混合查询

POST http://192.168.10.6:9200/_search
{
    "query": {
        "bool": {
            "must": [
                {
                    "match": {
                        "title":"uzi"
                    }
                }
            ],
            "filter":[
                {
                    "term": {
                        "word_count":1000
                    }
                }
            ]
        }
    }
}
返回:
{
    "took": 46,
    "timed_out": false,
    "_shards": {
        "total": 9,
        "successful": 9,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 1,
        "max_score": 1.2039728,
        "hits": [
            {
                "_index": "book",
                "_type": "novel",
                "_id": "ew57n2YBHFER5MTJt4Ky",
                "_score": 1.2039728,
                "_source": {
                    "title": "UZI的坑锅之路",
                    "author": "uzi",
                    "word_count": 1000,
                    "publish_date": "1996-03-01"
                }
            }
        ]
    }
}

must_not 必须不满足的条件。

POST http://192.168.10.6:9200/_search
{
    "query": {
        "bool": {
            "must_not": {
                "term": {
                    "author":"uzi"
                }
            }
        }
    }
}
返回:
{
    "took": 39,
    "timed_out": false,
    "_shards": {
        "total": 9,
        "successful": 9,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 10,
        "max_score": 1,
        "hits": [
            {
                "_index": "book",
                "_type": "novel",
                "_id": "dw5pn2YBHFER5MTJ3oIc",
                "_score": 1,
                "_source": {
                    "title": "叫你玩ADC",
                    "author": "微笑",
                    "word_count": 4000,
                    "publish_date": "1997-02-01"
                }
            },
            {
                "_index": "book",
                "_type": "novel",
                "_id": "eA5qn2YBHFER5MTJMoKT",
                "_score": 1,
                "_source": {
                    "title": "叫你玩辅助",
                    "author": "卷毛",
                    "word_count": 5000,
                    "publish_date": "1997-03-01"
                }
            },
            {
                "_index": "book",
                "_type": "novel",
                "_id": "eg5rn2YBHFER5MTJQYIz",
                "_score": 1,
                "_source": {
                    "title": "教你怎么做麻辣香锅",
                    "author": "香锅",
                    "word_count": 8888,
                    "publish_date": "1996-03-01"
                }
            },
            {
                "_index": "pepole",
                "_type": "man",
                "_id": "2",
                "_score": 1,
                "_source": {
                    "name": "12345",
                    "country": "guangzhou",
                    "age": 23,
                    "date": "1995-06-06"
                }
            },
            {
                "_index": "book",
                "_type": "novel",
                "_id": "dg5pn2YBHFER5MTJnYLt",
                "_score": 1,
                "_source": {
                    "title": "叫你玩打野",
                    "author": "厂长",
                    "word_count": 3000,
                    "publish_date": "1996-02-01"
                }
            },
            {
                "_index": "book",
                "_type": "novel",
                "_id": "vxAspmYBI-Y9epepwYvX",
                "_score": 1,
                "_source": {
                    "title": "微博热点-uzi不配洗白+2",
                    "author": "知名博主",
                    "word_count": "0",
                    "publish_date": 1540339200000
                }
            },
            {
                "_index": "pepole",
                "_type": "man",
                "_id": "dA4En2YBHFER5MTJ_oKo",
                "_score": 1,
                "_source": {
                    "name": "dada",
                    "country": "guangzhou",
                    "age": 30,
                    "date": "1987-06-06"
                }
            },
            {
                "_index": "book",
                "_type": "novel",
                "_id": "1",
                "_score": 1,
                "_source": {
                    "title": "叫你玩上单",
                    "author": "草莓",
                    "word_count": 1000,
                    "publish_date": "1995-01-01"
                }
            },
            {
                "_index": "book",
                "_type": "novel",
                "_id": "dQ5pn2YBHFER5MTJaoLT",
                "_score": 1,
                "_source": {
                    "title": "叫你玩中单",
                    "author": "若风",
                    "word_count": 2000,
                    "publish_date": "1995-02-01"
                }
            },
            {
                "_index": "book",
                "_type": "novel",
                "_id": "1jZJpWYBKYrkmyVA-wAp",
                "_score": 1,
                "_source": {
                    "title": "微博热点-uzi是弟弟",
                    "author": "微博",
                    "word_count": "1234",
                    "publish_date": 1540166400000
                }
            }
        ]
    }
}

分页+范围+全文本查询

POST  192.168.10.6:9200/_search
{
    "from":0,
    "size":10,
    "query":{
        "bool":{
            "must":[
                {
                    "match":{
                        "author":{
                            "query":"uzi"
                        }
                    }
                },
                {
                    "match":{
                        "title":{
                            "query":"如"
                        }
                    }
                }
            ],
            "filter":[
                {
                    "range":{
                        "word_count":{
                            "from":0,
                            "to":10000
                        }
                    }
                }
            ]
        }
    }
}
返回:
{
    "took": 56,
    "timed_out": false,
    "_shards": {
        "total": 9,
        "successful": 9,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 1,
        "max_score": 2.4079456,
        "hits": [
            {
                "_index": "book",
                "_type": "novel",
                "_id": "eQ5qn2YBHFER5MTJi4LZ",
                "_score": 2.4079456,
                "_source": {
                    "title": "教你如何自闭",
                    "author": "uzi",
                    "word_count": 6666,
                    "publish_date": "1995-03-01"
                }
            }
        ]
    }
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018.10.25 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 回顾
  • 创建索引
  • 基本用法
  • Query查询
  • Filter查询
  • 复杂查询
相关产品与服务
Elasticsearch Service
腾讯云 Elasticsearch Service(ES)是云端全托管海量数据检索分析服务,拥有高性能自研内核,集成X-Pack。ES 支持通过自治索引、存算分离、集群巡检等特性轻松管理集群,也支持免运维、自动弹性、按需使用的 Serverless 模式。使用 ES 您可以高效构建信息检索、日志分析、运维监控等服务,它独特的向量检索还可助您构建基于语义、图像的AI深度应用。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档