elasticsearch - 如何在弹性搜索中使用必须和应该
问题描述
我在弹性搜索中有这样的数据:
{
a : "status",
b : "01"
},
{
a : "status b",
b : "02"
}
我想用must
and获取所有数据should
我试过这样的机智must
和should
查询
{
"query": {
"bool": {
"must":[
{
"match": {
"a": "status b"
}
}],
"should":[ {
"match": {
"b": "01"
}
}]
}
}
}
但查询should
不起作用,任何人都可以帮助我吗?
解决方案
由于您没有提到您的映射,我根据您的数据创建了自己的映射并索引了您的示例文档,它工作正常。
您还可以使用_analyze API检查您的数据是如何被索引的,这将帮助您有效地调试问题。另外,使用解释 API,它会告诉你为什么你的 should 子句不匹配任何文档。
索引定义
{
"mappings": {
"properties": {
"a": {
"type": "text"
},
"b": {
"type": "integer"
}
}
}
}
索引示例文档
{
a : "status",
b : "01"
},
{
a : "status b",
b : "02"
}
笔记搜索查询也和你的一样
{
"query": {
"bool": {
"must": [
{
"match": {
"a": "status b"
}
}
],
"should": [
{
"match": {
"b": "01"
}
}
]
}
}
}
它带来了两个示例文档
"hits": [
{
"_index": "so_must_should",
"_type": "_doc",
"_id": "1",
"_score": 1.2111092,
"_source": {
"a": "status",
"b": "01"
}
},
{
"_index": "so_must_should",
"_type": "_doc",
"_id": "2",
"_score": 0.77041256,
"_source": {
"a": "status b",
"b": "02"
}
}
]
请交叉检查您的映射并将其与此示例进行比较,如果您还有其他问题,请告诉我。
推荐阅读
- amazon-web-services - AWS CloudFront Lambda@Edge 部署
- php - 在 PHP 中删除字符串中的所有类型的制表符、空格、回车符
- node.js - AWS S3 对象删除,Nodejs
- c++ - 国际象棋骑士巡回赛遇到困难
- python - 如果我在释放网络摄像头后再次访问它,OpenCV Python 捕获将失败
- c# - 测试代理未并行执行。VSTS
- php - 当我尝试使用注释参数创建自定义消息时。我得到 [语法错误]
- python - 计算python中一个值相对于另一个值的重新出现次数
- node.js - 传递 get 参数来表达路由
- kubernetes - 以非管理权限在本地运行 Kubernetes,但缺少 kubernetes 配置文件