ES搜索探析

场景1

多个文档,字段拥有重复数据,如name中有相同的字.

数据结构

每个文档中的name都有这个字

{
    "took": 2,
    "timed_out": false,
    "_shards": {
        "total": 3,
        "successful": 3,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 3,
        "max_score": 1,
        "hits": [
            {
                "_index": "index-people",
                "_type": "people",
                "_id": "crLX8WwBnSECdNekMHZT",
                "_score": 1,
                "_source": {
                    "name": "小红",
                    "address": "深圳",
                    "age": 22,
                    "date": "2019-09-01"
                }
            },
            {
                "_index": "index-people",
                "_type": "people",
                "_id": "2",
                "_score": 1,
                "_source": {
                    "name": "小小",
                    "address": "深圳",
                    "age": "22",
                    "date": "2019-09-01"
                }
            },
            {
                "_index": "index-people",
                "_type": "people",
                "_id": "dLLY8WwBnSECdNek43az",
                "_score": 1,
                "_source": {
                    "name": "小红",
                    "address": "深圳",
                    "age": 22,
                    "date": "2019-09-01"
                }
            }
        ]
    }
}

搜索测试

  • postman
    字段内容重复.
POST http://localhost:9200/index-people/_search
  • body
{
	"query":{
		"match":{
			"name":"小小"
		}
	},
	"size":1
}
  • 结果
{
    "took": 6,
    "timed_out": false,
    "_shards": {
        "total": 3,
        "successful": 3,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 3,
        "max_score": 0.5753642,
        "hits": [
            {
                "_index": "index-people",
                "_type": "people",
                "_id": "dLLY8WwBnSECdNek43az",
                "_score": 0.5753642,
                "_source": {
                    "name": "小红",
                    "address": "深圳",
                    "age": 22,
                    "date": "2019-09-01"
                }
            }
        ]
    }
}

搜索小小,出现结果为小红,再搜索,返回两个结果,如下:

  • postman
    返回两组数据.
POST http://localhost:9200/index-people/_search
  • body
{
	"query":{
		"match":{
			"name":"小小"
		}
	},
	"size":2
}
  • 结果
{
    "took": 1,
    "timed_out": false,
    "_shards": {
        "total": 3,
        "successful": 3,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 3,
        "max_score": 0.5753642,
        "hits": [
            {
                "_index": "index-people",
                "_type": "people",
                "_id": "dLLY8WwBnSECdNek43az",
                "_score": 0.5753642,
                "_source": {
                    "name": "小红",
                    "address": "深圳",
                    "age": 22,
                    "date": "2019-09-01"
                }
            },
            {
                "_index": "index-people",
                "_type": "people",
                "_id": "2",
                "_score": 0.5013843,
                "_source": {
                    "name": "小小",
                    "address": "深圳",
                    "age": "22",
                    "date": "2019-09-01"
                }
            }
        ]
    }
}

搜索小小,结果得分不同,反而小红得分较高,因为有两个小红的字段,且每个name中都有,小红得分高于小小.

场景2

文档中不含重复字或词.

数据结构

{
    "took": 1,
    "timed_out": false,
    "_shards": {
        "total": 3,
        "successful": 3,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 3,
        "max_score": 1,
        "hits": [
            {
                "_index": "index-people",
                "_type": "people",
                "_id": "2",
                "_score": 1,
                "_source": {
                    "name": "小小",
                    "address": "深圳",
                    "age": "22",
                    "date": "2019-09-01"
                }
            },
            {
                "_index": "index-people",
                "_type": "people",
                "_id": "crLX8WwBnSECdNekMHZT",
                "_score": 1,
                "_source": {
                    "name": "黄强柱",
                    "address": "深圳",
                    "age": 22,
                    "date": "2019-09-01"
                }
            },
            {
                "_index": "index-people",
                "_type": "people",
                "_id": "dLLY8WwBnSECdNek43az",
                "_score": 1,
                "_source": {
                    "name": "大红",
                    "address": "深圳",
                    "age": 22,
                    "date": "2019-09-01"
                }
            }
        ]
    }
}

搜索测试

  • postman
POST http://localhost:9200/index-people/_search
  • body
{
	"query":{
		"match":{
			"name":"小小"
		}
	},
	"size":1
}
  • 结果
{
    "took": 1,
    "timed_out": false,
    "_shards": {
        "total": 3,
        "successful": 3,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 1,
        "max_score": 2.0197666,
        "hits": [
            {
                "_index": "index-people",
                "_type": "people",
                "_id": "2",
                "_score": 2.0197666,
                "_source": {
                    "name": "小小",
                    "address": "深圳",
                    "age": "22",
                    "date": "2019-09-01"
                }
            }
        ]
    }
}

分数高的离谱,不含同字的文档,搜索结果准确.

小结

(1) 文档不含相同字或词,搜索为精确搜索,1:1获取结果;
(2) 文档中含有相同字或词,则为模糊搜索,搜索结果进行综合计算,获取结果分数,输出最高分的结果;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

天然玩家

坚持才能做到极致

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

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

打赏作者

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

抵扣说明:

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

余额充值