博客
关于我
ES DSL搜索 - multi_match、boost和布尔查询
阅读量:286 次
发布时间:2019-03-01

本文共 3676 字,大约阅读时间需要 12 分钟。

Elasticsearch搜索API入门介绍

搜索API基础

Elasticsearch的搜索API提供了强大的文档搜索功能,以下是常用的操作方法。请通过Postman等工具进行测试,接口请求的前缀地址统一为Elasticsearch部署IP地址+端口号(如http://192.168.51.4:9200)。

请求地址示例

POST /search_demo/_doc/_search

multi_match多字段匹配

multi_match用于在多个字段中进行全文匹配,可以灵活配置匹配字段。

请求示例

{    "query": {        "multi_match": {            "query": "组合",            "fields": ["desc", "nickname"]        }    },    "_source": ["id", "nickname", "desc"]}

返回结果示例

{    "took": 3,    "timed_out": false,    "hits": {        "total": {            "value": 1,            "relation": "eq"        },        "max_score": 2.2874916,        "hits": [            {                "_index": "search_demo",                "_type": "_doc",                "_id": "1007",                "_score": 2.2874916,                "_source": {                    "nickname": "老男孩",                    "id": 1007,                    "desc": "确实是个很好的组合,筷子 筷子"                }            }        ]    }}

boost字段权重设置

boost用于为特定字段设置权重,提升相关性得分。常用于搜索商品名称或关键词时。

请求示例

{    "query": {        "multi_match": {            "query": "好的",            "fields": ["desc", "nickname*10"]        }    },    "_source": ["id", "nickname", "desc"]}

返回结果示例

{    "took": 4,    "hits": {        "total": {            "value": 9,            "relation": "eq"        },        "max_score": 3.1980762,        "hits": [            {                "_index": "search_demo",                "_type": "_doc",                "_id": "1004",                "_score": 3.1980762,                "_source": {                    "nickname": "红帽子",                    "id": 1004,                    "desc": "好的系统必须拥有稳定的系统结构"                }            },            // 其他结果...        ]    }}

布尔查询

bool查询用于复杂的文本组合查询,支持mustshouldmust_not等布尔逻辑。

单个查询示例

{    "query": {        "bool": {            "must": [                {                    "multi_match": {                        "query": "好的",                        "fields": ["desc", "nickname"]                    }                },                {                    "term": {                        "sex": 0                    }                },                {                    "term": {                        "birthday": "1992-12-24"                    }                }            ]        }    },    "_source": ["id", "sex", "nickname", "desc", "birthday"]}

组合查询示例

{    "query": {        "bool": {            "must": [                {                    "match": {                        "desc": "好的"                    }                },                {                    "match": {                        "nickname": "好的"                    }                }            ],            "should": [                {                    "match": {                        "sex": 1                    }                }            ],            "must_not": [                {                    "term": {                        "birthday": "1993-01-24"                    }                }            ]        }    },    "_source": ["id", "sex", "nickname", "desc", "birthday"]}

加权字段查询

{    "query": {        "bool": {            "should": [                {                    "match": {                        "desc": {                            "query": "好的",                            "boost": 2                        }                    }                },                {                    "match": {                        "desc": {                            "query": "男孩",                            "boost": 20                        }                    }                }            ]        }    },    "_source": ["id", "sex", "nickname", "desc", "birthday"]}

相关信息

  • 博文不易,辛苦各位猿友点个关注和赞,感谢!

转载地址:http://sjhx.baihongyu.com/

你可能感兴趣的文章
NLP 基于kashgari和BERT实现中文命名实体识别(NER)
查看>>
No 'Access-Control-Allow-Origin' header is present on the requested resource.
查看>>
Node.js安装与配置指南:轻松启航您的JavaScript服务器之旅
查看>>
npm的问题:config global `--global`, `--local` are deprecated. Use `--location=global` instead 的解决办法
查看>>
NR,NF,FNR
查看>>
nrf开发笔记一开发软件
查看>>
NSDateFormatter的替代方法
查看>>
NSOperation基本操作
查看>>
NSSet集合 无序的 不能重复的
查看>>
ntko文件存取错误_苹果推送 macOS 10.15.4:iCloud 云盘文件夹共享终于来了
查看>>
nullnullHuge Pages
查看>>
numpy 用法
查看>>
Numpy如何使用np.umprod重写range函数中i的python
查看>>
oauth2-shiro 添加 redis 实现版本
查看>>
OAuth2.0_JWT令牌-生成令牌和校验令牌_Spring Security OAuth2.0认证授权---springcloud工作笔记148
查看>>
OAuth2.0_JWT令牌介绍_Spring Security OAuth2.0认证授权---springcloud工作笔记147
查看>>
OAuth2.0_介绍_Spring Security OAuth2.0认证授权---springcloud工作笔记137
查看>>
OAuth2.0_完善环境配置_把资源微服务客户端信息_授权码存入到数据库_Spring Security OAuth2.0认证授权---springcloud工作笔记149
查看>>
OAuth2.0_授权服务配置_Spring Security OAuth2.0认证授权---springcloud工作笔记140
查看>>
OAuth2.0_授权服务配置_令牌服务和令牌端点配置_Spring Security OAuth2.0认证授权---springcloud工作笔记143
查看>>