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

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"
                }
            }
        ]
    }
}

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏bboysoul

linux 服务器带宽测试脚本ZBench

很郁闷,今天我的vultr服务器的ip被ban了,无奈只能换服务器,今天给大家推荐一个vps的带宽测速脚本ZBench可以一键测试你的服务器到国内和国外的速度

52420
来自专栏AI-vell

黑科技:程序员如何打造属于自己的分体键盘

为了不要这么纠结,就自己diy一个咯,正好有台3D打印机(又一个装B神器,你值得拥有,O(∩_∩)O哈哈~),全部外壳自己打印,控制板用Arduino Leon...

1.1K80
来自专栏铭毅天下

抢先 | 支持sql的Elasticsearch6.3全景概览

像操作Mysql一样使用Elasticsearch,缩减DSL的学习成本,更多人爱上ES的特性。

24940
来自专栏杨建荣的学习笔记

dg的奇怪问题终结和分区问题答疑 (r7笔记第77天)

今天来说几个问题,一个是对昨天《让我焦灼的四个问题》的升华,不能起博眼球的题目,技术分析给大家兜底了,你们看看有没有类似的问题。 还有几个小问题说说今天的感受和...

35650
来自专栏杨建荣的学习笔记

关于抓取session信息的一个脚本(r3笔记第8天)

关于session的诊断,可以基于动态性能视图,ash,awr.. 自己也写过一些简单的脚本,在平时的工作中也能够完成一些基本的工作。今天在看taner分享的脚...

35360
来自专栏13blog.site

Spring+SpringMVC+MyBatis+easyUI整合优化篇(六)easyUI与富文本编辑器UEditor整合

前言 本来这一篇和接下来的几篇是打算讲一下JDBC和数据库优化的,但是最近很多朋友加我好友也讨论了一些问题,我发现大家似乎都是拿这个项目作为练手项目,作为脚手架...

35660
来自专栏用户画像

实验室3 sql server 产品销售数据库

某公司的产品销售数据库company,company数据库中存在人事表employee、客户表customer、销售表sales、销售明细表sale_item、...

9130
来自专栏腾讯Bugly的专栏

黑科技:程序员如何打造属于自己的分体键盘

前言 作为一名程序员,键盘在手,天下我有啊,不整个高大上的键盘怎么提升B 格。之前一直想买个机械键盘,听说机械键盘敲代码时格外舒爽,实在是提升效率与 B 格的神...

38650
来自专栏zhisheng

Elasticsearch 可用的中文分词器使用

采用了特有的“正向迭代最细粒度切分算法“,支持细粒度和最大词长两种切分模式;具有83万字/秒(1600KB/S)的高速处理能力。

1.3K20
来自专栏freesan44

关于 iOS 10 中 ATS 的问题

WWDC 15 提出的 ATS (App Transport Security) 是 Apple 在推进网络通讯安全的一个重要方式。在 iOS 9 和 OS X...

21820

扫码关注云+社区

领取腾讯云代金券