Elastic Search:(三)搜索的简单使用

目录

1.搜索简单使用(POST)

1.1 词条查询

1.1.1 term查询

1.1.2 terms查询

1.1.3 总结

1.2 全文查询

1.2.1 match_all

1.2.2 match

1.2.3 multi_match

1.2.4 match_phrase

1.2.5 match_phrase_prefix


1.搜索简单使用(POST)

1.1 词条查询

该查询方式为精准查询

1.1.1 term查询

term为某一限定字段下某一类别的精确查询,例如:请求性别为女的所有歌手

POST students/_search
{
    "query":{
        "term":{
            "sex":"女"
        }
    }
}

返回结果:

{
    "took": 1,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
		// 这里为查询到符合限制条件的所有信息条数
    "hits": {
        "total": {
            "value": 1,
            "relation": "eq"
        },
        "max_score": 1.540445,
        "hits": [
            {
                "_index": "students",
                "_type": "_doc",
                "_id": "ciRNn3sBrIzyq1AtxIS6",
                "_score": 1.540445,
                "_source": {
                    "name": "韩红",
                    "No": "2020131545",
                    "age": "50",
                    "sex": "女",
                    "address": "西藏藏族自治区"
                }
            }
        ]
    }
}

1.1.2 terms查询

terms为某一限定字段下多个类别的精确查询。例如:查询性别为男和为女的所有歌手。

POST students/_search
{
    "query":{
        "terms":{
            "sex":["男","女"]
        }
    }
}

返回结果:

{
    "took": 4,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 5,
            "relation": "eq"
        },
        "max_score": 1.0,
        "hits": [
            {
                "_index": "students",
                "_type": "_doc",
                "_id": "NtQ8lnsBlAiSLOzxFcXJ",
                "_score": 1.0,
                "_source": {
                    "name": "周杰伦",
                    "No": "2020131567",
                    "age": "40",
                    "sex": "男",
                    "address": "台湾省"
                }
            },
            {
                "_index": "students",
                "_type": "_doc",
                "_id": "1",
                "_score": 1.0,
                "_source": {
                    "name": "许嵩",
                    "No": "2020131566",
                    "age": "321",
                    "sex": "男",
                    "address": "安徽省"
                }
            },
            {
                "_index": "students",
                "_type": "_doc",
                "_id": "cSRNn3sBrIzyq1AtBITi",
                "_score": 1.0,
                "_source": {
                    "name": "汪苏泷",
                    "No": "2020131565",
                    "age": "30",
                    "sex": "男",
                    "address": "山东省"
                }
            },
            {
                "_index": "students",
                "_type": "_doc",
                "_id": "ciRNn3sBrIzyq1AtxIS6",
                "_score": 1.0,
                "_source": {
                    "name": "韩红",
                    "No": "2020131545",
                    "age": "50",
                    "sex": "女",
                    "address": "西藏藏族自治区"
                }
            },
            {
                "_index": "students",
                "_type": "_doc",
                "_id": "cyROn3sBrIzyq1AtcoQa",
                "_score": 1.0,
                "_source": {
                    "name": "徐良",
                    "No": "2020131345",
                    "age": "34",
                    "sex": "男",
                    "address": "山东省"
                }
            }
        ]
    }
}

1.1.3 总结

  1. term查询符合某一限定字段下的某一类别;
  2. terms查询符合某一限定字段下的多个类别;
  3. terms比term好用,建议直接使用terms查询,条件用[]括起来;
  4. 两者都不常用,且查询限定条件的类型结构化数据如number、date、keyword才可查询出来。

1.2 全文查询

该方式类似模糊查询,十分有用。

1.2.1 match_all

该方式为查询数据库中全部信息,默认显示十条,一般搭配from和size进行分页显示。

POST students/_search
{
    "query":{
        "match_all":{}
    },
		// 从0页开始显示
    "from":0,
		// 一共显示100页
    "size":100
}

1.2.2 match

单字段查询,且为模糊查询,会将输入的信息分割成单个字符与数据库中数据匹配,根据得分高低进行顺序显示。

POST students/_search
{
    "query":{
        "match":{
            "name":"周徐"
        }
    }
}

显示结果:

{
    "took": 3,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 2,
            "relation": "eq"
        },
				// 最高得分,按照得分高低顺序排序
        "max_score": 1.6360589,
        "hits": [
            {
                "_index": "students",
                "_type": "_doc",
                "_id": "cyROn3sBrIzyq1AtcoQa",
                "_score": 1.6360589,
                "_source": {
                    "name": "徐良",
                    "No": "2020131345",
                    "age": "34",
                    "sex": "男",
                    "address": "山东省"
                }
            },
            {
                "_index": "students",
                "_type": "_doc",
                "_id": "NtQ8lnsBlAiSLOzxFcXJ",
                "_score": 1.3792357,
                "_source": {
                    "name": "周杰伦",
                    "No": "2020131567",
                    "age": "40",
                    "sex": "男",
                    "address": "台湾省"
                }
            }
        ]
    }
}

1.2.3 multi_match

多字段查询,与单字段查询功能类似,支持多个字段的查询。

POST students/_search
{
    "query":{
        "multi_match":{
            "query":"许30",
            "fields":["name","age"]
            
        }
    }
}

返回结果:

{
    "took": 1,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 1,
            "relation": "eq"
        },
        "max_score": 1.093527,
        "hits": [
            {
                "_index": "students",
                "_type": "_doc",
                "_id": "1",
                "_score": 1.093527,
                "_source": {
                    "name": "许嵩",
                    "No": "2020131566",
                    "age": "321",
                    "sex": "男",
                    "address": "安徽省"
                }
            }
        ]
    }
}

1.2.4 match_phrase

类似词条查询,是精确查询方式。

POST students/_search
{
    "query":{
        "match_phrase":{
            "name":"嵩"
        }
    }
}

1.2.5 match_phrase_prefix

类似关系数据库中的like查询,且为后like查询,一般用在查询英文使用,给予前缀信息进行查询。

POST students/_search
{
    "query":{
        "match_phrase_prefix":{
            "neck":"V"
        }
    }
}

返回结果:

{
    "took": 486,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 1,
            "relation": "eq"
        },
        "max_score": 0.2876821,
        "hits": [
            {
                "_index": "students",
                "_type": "_doc",
                "_id": "dCRxn3sBrIzyq1At1oQ2",
                "_score": 0.2876821,
                "_source": {
                    "name": "许嵩",
                    "neck": "Vae",
                    "No": "2020131566",
                    "age": "321",
                    "sex": "男",
                    "address": "安徽省"
                }
            }
        ]
    }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Shaco、LYF

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值