Elasticsearch笔记(6)

137 阅读2分钟

这是我参与8月更文挑战的第16天,活动详情查看:8月更文挑战

备注:继Elasticsearch笔记(5)

DSL搜索详解

  1. 预先创建index以及properties
#PUT     http://192.168.123.64:9200/jacquesh
{
    "settings": {
        "index": {
            "number_of_shards": "5", #主分片数量
            "number_of_replicas": "0" #备份分片数量
        }
    }
}
#POST     http://192.168.123.64:9200/jacquesh
{
    "properties": {
        "id": {
            "type": "long"
        },
        "age": {
            "type": "integer"
        },
        "username": {
            "type": "keyword"
        },
        "nickname": {
            "type": "text",
            "analyzer": "ik_max_word"
        },
     
        "comefrom": {
            "type": "text",
            "analyzer": "ik_max_word"
        },
        "sex": {
            "type": "byte"
        },
        "birthday": {
            "type": "date"
        },
        "class": {
            "type": "text"
       
        }
    }
}
  1. 填充测试数据

#POST     http://192.168.123.64:9200/jacuesh/_doc/1001
{
    "id": 1001,
    "age": 31,
    "username": "黄某人",
    "nickname": "老黄",
    "comefrom": "广东省深圳市",
    "sex": 1,
    "birthday": "1995-10-29",
    "class": "计算机科学与技术1班"
}
#POST     http://192.168.123.64:9200/jacuesh/_doc/1002
{
    "id": 1002,
    "age": 21,
    "username": "刘某鹏",
    "nickname": "鹏鹏",
    "comefrom": "河北省邯郸市",
    "sex": 1,
    "birthday": "1998-10-21",
    "class": "计算机科学以技术1班"
}
#POST     http://192.168.123.64:9200/jacuesh/_doc/1003
{
    "id": 1003,
    "age": 20,
    "username": "刘某东",
    "nickname": "狗东",
    "comefrom": "河北省邢台市",
    "sex": 1,
    "birthday": "1993-10-21",
    "class": "计算机科学以技术1班"
}

DSL搜索 - 入门语法

请求参数的查询(QueryString)

查询[字段]包含[内容]的文档

# GET     /{索引}/_doc/_search?q={filds}:{value}
# GET   http://192.168.123.64:9200/jacuesh/_doc/_search?q=comefrom:河北
查询结果
{
    "took": 93,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 2,
            "relation": "eq"
        },
        "max_score": 0.2876821,
        "hits": [
            {
                "_index": "jacuesh",
                "_type": "_doc",
                "_id": "1002\n",
                "_score": 0.2876821,
                "_source": {
                    "id": 1002,
                    "age": 21,
                    "username": "刘某鹏",
                    "nickname": "鹏鹏",
                    "comefrom": "河北省邯郸市",
                    "sex": 1,
                    "birthday": "1998-10-21",
                    "class": "计算机科学以技术1班"
                }
            },
            {
                "_index": "jacuesh",
                "_type": "_doc",
                "_id": "1003\n",
                "_score": 0.2876821,
                "_source": {
                    "id": 1003,
                    "age": 20,
                    "username": "刘某东",
                    "nickname": "狗东",
                    "comefrom": "河北省邢台市",
                    "sex": 1,
                    "birthday": "1993-10-21",
                    "class": "计算机科学以技术1班"
                }
            }
        ]
    }
}
多个参数
#GET    /{索引}/_doc/_search?q={filds}:{value}&q={filds}:{value}
# GET  http://192.168.123.64:9200/jacuesh/_doc/_search?q=comefrom:河北&q=age:20

查询结果

{
    "took": 17,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 1,
            "relation": "eq"
        },
        "max_score": 1.0,
        "hits": [
            {
                "_index": "jacuesh",
                "_type": "_doc",
                "_id": "1003\n",
                "_score": 1.0,
                "_source": {
                    "id": 1003,
                    "age": 20,
                    "username": "刘某东",
                    "nickname": "狗东",
                    "comefrom": "河北省邢台市",
                    "sex": 1,
                    "birthday": "1993-10-21",
                    "class": "计算机科学以技术1班"
                }
            }
        ]
    }
}

text与keyword搜索对比测试(keyword不会被倒排索引,不会被分词)

#GET  http://192.168.123.64:9200/jacuesh/_doc/_search?q=username:黄
#由于username的type为keyword所以不会被分词
{
    "took": 28,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 0,
            "relation": "eq"
        },
        "max_score": null,
        "hits": []
    }
}
#GET  http://192.168.123.64:9200/jacuesh/_doc/_search?q=username:黄某人
##使用了全量匹配就能被匹配出来
{
    "took": 12,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 1,
            "relation": "eq"
        },
        "max_score": 0.2876821,
        "hits": [
            {
                "_index": "jacuesh",
                "_type": "_doc",
                "_id": "1001\n",
                "_score": 0.2876821,
                "_source": {
                    "id": 1001,
                    "age": 31,
                    "username": "黄某人",
                    "nickname": "老黄",
                    "comefrom": "广东省汕头市",
                    "sex": 1,
                    "birthday": "1995-10-29",
                    "class": "计算机科学以技术1班"
                }
            }
        ]
    }
}

这种方式称之为QueryString查询方式,参数都是放在url中作为请求参数的。