千家信息网

ElasticSearch中如何使用多种搜索方式检索数据

发表于:2024-11-18 作者:千家信息网编辑
千家信息网最后更新 2024年11月18日,这篇文章给大家介绍ElasticSearch中如何使用多种搜索方式检索数据,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。1、query string search查询(1)、全量
千家信息网最后更新 2024年11月18日ElasticSearch中如何使用多种搜索方式检索数据

这篇文章给大家介绍ElasticSearch中如何使用多种搜索方式检索数据,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

1、query string search查询

(1)、全量查询
GET http://{{es-host}}/ecommerce/produce/_search

结果:

{        "took": 38,        "timed_out": false,        "_shards": {                "total": 5,                "successful": 5,                "skipped": 0,                "failed": 0        },        "hits": {                "total": 3,                "max_score": 1.0,                "hits": [                        {                                "_index": "ecommerce",                                "_type": "produce",                                "_id": "2",                                "_score": 1.0,                                "_source": {                                        "name": "jiajieshi yagao",                                        "desc": "youxiao fangzhu",                                        "price": 25,                                        "producer": "jiajieshi producer",                                        "tags": [                                                "fangzhu"                                        ]                                }                        },                        {                                "_index": "ecommerce",                                "_type": "produce",                                "_id": "1",                                "_score": 1.0,                                "_source": {                                        "name": "gaolujie yagao",                                        "desc": "gaoxiao meibai",                                        "price": 30,                                        "producer": "gaolujie producer",                                        "tags": [                                                "meibai",                                                "fangzhu"                                        ]                                }                        },                        {                                "_index": "ecommerce",                                "_type": "produce",                                "_id": "3",                                "_score": 1.0,                                "_source": {                                        "name": "zhonghua yagao",                                        "desc": "caoben zhiwu",                                        "price": 40,                                        "producer": "zhonghua producer",                                        "tags": [                                                "qingxin"                                        ]                                }                        }                ]        }}

解释:

    took 标识检索花费的时间,单位毫秒     time_out 是否超时     _shards 数据拆分成了5个分片,所以对于搜索请求,会打到所有的primary shard(或者是它的某个replica shard也可以)    total 检索5个分片     successful 成功5个     hits.total 检索到数据的总记录数     hits.max_score 相似度分数 最高为1     hits.hits 数据集合     hits.hits._index 索引名称     hits.hits._type 类型     hits.hits._score 数据评分     hits.hits._source 数据项内容
(2)、根据商品名称检索,按价格倒叙排列
GET http://{{es-host}}/ecommerce/produce/_search?q=name:yagao&sort=price:desc

结果:

{        "took": 71,        "timed_out": false,        "_shards": {                "total": 5,                "successful": 5,                "skipped": 0,                "failed": 0        },        "hits": {                "total": 3,                "max_score": null,                "hits": [                        {                                "_index": "ecommerce",                                "_type": "produce",                                "_id": "3",                                "_score": null,                                "_source": {                                        "name": "zhonghua yagao",                                        "desc": "caoben zhiwu",                                        "price": 40,                                        "producer": "zhonghua producer",                                        "tags": [                                                "qingxin"                                        ]                                },                                "sort": [                                        40                                ]                        },                        {                                "_index": "ecommerce",                                "_type": "produce",                                "_id": "1",                                "_score": null,                                "_source": {                                        "name": "gaolujie yagao",                                        "desc": "gaoxiao meibai",                                        "price": 30,                                        "producer": "gaolujie producer",                                        "tags": [                                                "meibai",                                                "fangzhu"                                        ]                                },                                "sort": [                                        30                                ]                        },                        {                                "_index": "ecommerce",                                "_type": "produce",                                "_id": "2",                                "_score": null,                                "_source": {                                        "name": "jiajieshi yagao",                                        "desc": "youxiao fangzhu",                                        "price": 25,                                        "producer": "jiajieshi producer",                                        "tags": [                                                "fangzhu"                                        ]                                },                                "sort": [                                        25                                ]                        }                ]        }}

2、query DSL查询

DSL:Domain Specified Language.特定领域的语言

http request body :请求体,可以用json的格式来构建查询语法,比较方便,可以构建各种复杂的语法,比query string search肯定强大多了。

(1)、全量查询
GET http://{{es-host}}/ecommerce/produce/_search{        "query":{                "match_all":{}        }}

结果:

{        "took": 23,        "timed_out": false,        "_shards": {                "total": 5,                "successful": 5,                "skipped": 0,                "failed": 0        },        "hits": {                "total": 3,                "max_score": 1.0,                "hits": [                        {                                "_index": "ecommerce",                                "_type": "produce",                                "_id": "2",                                "_score": 1.0,                                "_source": {                                        "name": "jiajieshi yagao",                                        "desc": "youxiao fangzhu",                                        "price": 25,                                        "producer": "jiajieshi producer",                                        "tags": [                                                "fangzhu"                                        ]                                }                        },                        {                                "_index": "ecommerce",                                "_type": "produce",                                "_id": "1",                                "_score": 1.0,                                "_source": {                                        "name": "gaolujie yagao",                                        "desc": "gaoxiao meibai",                                        "price": 30,                                        "producer": "gaolujie producer",                                        "tags": [                                                "meibai",                                                "fangzhu"                                        ]                                }                        },                        {                                "_index": "ecommerce",                                "_type": "produce",                                "_id": "3",                                "_score": 1.0,                                "_source": {                                        "name": "zhonghua yagao",                                        "desc": "caoben zhiwu",                                        "price": 40,                                        "producer": "zhonghua producer",                                        "tags": [                                                "qingxin"                                        ]                                }                        }                ]        }}
(2) 、全量查询返回指定属性值
GET http://{{es-host}}/ecommerce/produce/_search{        "query":{                "match_all":{}        },        "_source":["name","price"]}

返回结果:

{    "took": 57,    "timed_out": false,    "_shards": {        "total": 5,        "successful": 5,        "skipped": 0,        "failed": 0    },    "hits": {        "total": 3,        "max_score": 1,        "hits": [            {                "_index": "ecommerce",                "_type": "produce",                "_id": "2",                "_score": 1,                "_source": {                    "price": 25,                    "name": "jiajieshi yagao"                }            },            {                "_index": "ecommerce",                "_type": "produce",                "_id": "1",                "_score": 1,                "_source": {                    "price": 30,                    "name": "gaolujie yagao"                }            },            {                "_index": "ecommerce",                "_type": "produce",                "_id": "3",                "_score": 1,                "_source": {                    "price": 40,                    "name": "zhonghua yagao"                }            }        ]    }}
(3)、按商品名称搜索并按价格倒叙排列
GET http://{{es-host}}/ecommerce/produce/_search{        "query":{                "match":{                        "name":"yagao"                }        },        "sort":{                "price":"desc"        }}

结果:

{        "took": 384,        "timed_out": false,        "_shards": {                "total": 5,                "successful": 5,                "skipped": 0,                "failed": 0        },        "hits": {                "total": 3,                "max_score": null,                "hits": [                        {                                "_index": "ecommerce",                                "_type": "produce",                                "_id": "3",                                "_score": null,                                "_source": {                                        "name": "zhonghua yagao",                                        "desc": "caoben zhiwu",                                        "price": 40,                                        "producer": "zhonghua producer",                                        "tags": [                                                "qingxin"                                        ]                                },                                "sort": [                                        40                                ]                        },                        {                                "_index": "ecommerce",                                "_type": "produce",                                "_id": "1",                                "_score": null,                                "_source": {                                        "name": "gaolujie yagao",                                        "desc": "gaoxiao meibai",                                        "price": 30,                                        "producer": "gaolujie producer",                                        "tags": [                                                "meibai",                                                "fangzhu"                                        ]                                },                                "sort": [                                        30                                ]                        },                        {                                "_index": "ecommerce",                                "_type": "produce",                                "_id": "2",                                "_score": null,                                "_source": {                                        "name": "jiajieshi yagao",                                        "desc": "youxiao fangzhu",                                        "price": 25,                                        "producer": "jiajieshi producer",                                        "tags": [                                                "fangzhu"                                        ]                                },                                "sort": [                                        25                                ]                        }                ]        }}

3、query filter查询

搜索商品名称包含yagao,而且售价大于30元的商品

GET http://{{es-host}}/ecommerce/produce/_search{        "query":{                "bool":{                        "must":{                                "match":{                                        "name":"yagao"                                }                        },                        "filter":{                                "range":{                                        "price":{                                                "gt":"30"                                        }                                }                        }                }        }}

返回结果:

{    "took": 88,    "timed_out": false,    "_shards": {        "total": 5,        "successful": 5,        "skipped": 0,        "failed": 0    },    "hits": {        "total": 1,        "max_score": 0.25811607,        "hits": [            {                "_index": "ecommerce",                "_type": "produce",                "_id": "3",                "_score": 0.25811607,                "_source": {                    "name": "zhonghua yagao",                    "desc": "caoben zhiwu",                    "price": 40,                    "producer": "zhonghua producer",                    "tags": [                        "qingxin"                    ]                }            }        ]    }}

4、full-text search查询

准备数据:

PUT http://{{es-host}}/ecommerce/produce/4{        "name":"special yagao",        "desc":"special meibai",        "price":"50",        "producer":"special yagao producer",        "tags":["meibai"]}
http://{{es-host}}/ecommerce/produce/_search{        "query":{                "match":{                        "producer":"yagao producer"                }        }}

检索时候会把"yagao producer" 分割成"yagao" 和"producer" 只要匹配上任何一个就是符合的数据

结果

{    "took": 20,    "timed_out": false,    "_shards": {        "total": 5,        "successful": 5,        "skipped": 0,        "failed": 0    },    "hits": {        "total": 4,        "max_score": 0.70293105,        "hits": [            {                "_index": "ecommerce",                "_type": "produce",                "_id": "4",                "_score": 0.70293105,                "_source": {                    "name": "special yagao",                    "desc": "special meibai",                    "price": "50",                    "producer": "special yagao producer",                    "tags": [                        "meibai"                    ]                }            },            {                "_index": "ecommerce",                "_type": "produce",                "_id": "1",                "_score": 0.25811607,                "_source": {                    "name": "gaolujie yagao",                    "desc": "gaolujie meibai",                    "price": 30,                    "producer": "gaolujie producer",                    "tags": [                        "meibai",                        "fangzhu"                    ]                }            },            {                "_index": "ecommerce",                "_type": "produce",                "_id": "3",                "_score": 0.25811607,                "_source": {                    "name": "zhonghua yagao",                    "desc": "caoben zhiwu",                    "price": 40,                    "producer": "zhonghua producer",                    "tags": [                        "qingxin"                    ]                }            },            {                "_index": "ecommerce",                "_type": "produce",                "_id": "2",                "_score": 0.1805489,                "_source": {                    "name": "jiajieshi yagao",                    "desc": "youxiao fangzhu",                    "price": 25,                    "producer": "jiajieshi producer",                    "tags": [                        "fangzhu"                    ]                }            }        ]    }}

最匹配的相关度评分高,会排在最前面。

5、phrase search查询

对要搜索的词不再分词

GET http://{{es-host}}/ecommerce/produce/_search{        "query":{                "match_phrase":{                        "producer":"yagao producer"                }        }}

结果:

{    "took": 33,    "timed_out": false,    "_shards": {        "total": 5,        "successful": 5,        "skipped": 0,        "failed": 0    },    "hits": {        "total": 1,        "max_score": 0.70293105,        "hits": [            {                "_index": "ecommerce",                "_type": "produce",                "_id": "4",                "_score": 0.70293105,                "_source": {                    "name": "special yagao",                    "desc": "special meibai",                    "price": "50",                    "producer": "special yagao producer",                    "tags": [                        "meibai"                    ]                }            }        ]    }}

6、highlight search查询

GET http://{{es-host}}/ecommerce/produce/_search{        "query":{                "match":{                        "name":"yagao"                }        },        "highlight":{                "fields":{                        "name":{}                }        }}

结果:

{    "took": 16,    "timed_out": false,    "_shards": {        "total": 5,        "successful": 5,        "skipped": 0,        "failed": 0    },    "hits": {        "total": 4,        "max_score": 0.25811607,        "hits": [            {                "_index": "ecommerce",                "_type": "produce",                "_id": "1",                "_score": 0.25811607,                "_source": {                    "name": "gaolujie yagao",                    "desc": "gaolujie meibai",                    "price": 30,                    "producer": "gaolujie producer",                    "tags": [                        "meibai",                        "fangzhu"                    ]                },                "highlight": {                    "name": [                        "gaolujie yagao"                    ]                }            },            {                "_index": "ecommerce",                "_type": "produce",                "_id": "3",                "_score": 0.25811607,                "_source": {                    "name": "zhonghua yagao",                    "desc": "caoben zhiwu",                    "price": 40,                    "producer": "zhonghua producer",                    "tags": [                        "qingxin"                    ]                },                "highlight": {                    "name": [                        "zhonghua yagao"                    ]                }            },            {                "_index": "ecommerce",                "_type": "produce",                "_id": "2",                "_score": 0.16358379,                "_source": {                    "name": "jiajieshi yagao",                    "desc": "youxiao fangzhu",                    "price": 25,                    "producer": "jiajieshi producer",                    "tags": [                        "fangzhu"                    ]                },                "highlight": {                    "name": [                        "jiajieshi yagao"                    ]                }            },            {                "_index": "ecommerce",                "_type": "produce",                "_id": "4",                "_score": 0.16358379,                "_source": {                    "name": "special yagao",                    "desc": "special meibai",                    "price": "50",                    "producer": "special yagao producer",                    "tags": [                        "meibai"                    ]                },                "highlight": {                    "name": [                        "special yagao"                    ]                }            }        ]    }}

关于ElasticSearch中如何使用多种搜索方式检索数据就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

0