在 elasticsearch 中对嵌套术语执行文本搜索和过滤
Performing a text search and filtering on nested terms in elasticsearch
我正在尝试执行搜索,例如在描述中搜索郊狼一词,但它们是红色和绿色,并且属于卡通类别。现在我想我明白你不能在同一个查询中有匹配和术语(下面的查询因为这个原因不起作用),而且你也知道你不应该使用术语来搜索文本字段。谁能指出我正确的方向?
这是我的查询
GET /searchproducts/_search
{
"query": {
"match": {
"description": {
"query": "coyote"
}
},
"bool": {
"should": [{
"terms": {
"colours.name": ["red", "green"]
}
},
{
"terms": {
"categories.name": ["Cartoon"]
}
}
]
}
},
"aggs": {
"colours": {
"terms": {
"field": "colour.name.value",
"size": 100
}
},
"categories": {
"terms": {
"field": "categories.id",
"size": 100
}
}
}
}
您可以使用 bool query 组合多个查询。试试这个查询:
{
"query": {
"bool": {
"should": [
{
"match": {
"description": {
"query": "coyote"
}
}
},
{
"bool": {
"should": [
{
"terms": {
"colours.name": [
"red",
"green"
]
}
},
{
"terms": {
"categories.name": [
"Cartoon"
]
}
}
]
}
}
]
}
},
"aggs": {
"colours": {
"terms": {
"field": "colour.name.value",
"size": 100
}
},
"categories": {
"terms": {
"field": "categories.id",
"size": 100
}
}
}
}
我正在尝试执行搜索,例如在描述中搜索郊狼一词,但它们是红色和绿色,并且属于卡通类别。现在我想我明白你不能在同一个查询中有匹配和术语(下面的查询因为这个原因不起作用),而且你也知道你不应该使用术语来搜索文本字段。谁能指出我正确的方向?
这是我的查询
GET /searchproducts/_search
{
"query": {
"match": {
"description": {
"query": "coyote"
}
},
"bool": {
"should": [{
"terms": {
"colours.name": ["red", "green"]
}
},
{
"terms": {
"categories.name": ["Cartoon"]
}
}
]
}
},
"aggs": {
"colours": {
"terms": {
"field": "colour.name.value",
"size": 100
}
},
"categories": {
"terms": {
"field": "categories.id",
"size": 100
}
}
}
}
您可以使用 bool query 组合多个查询。试试这个查询:
{
"query": {
"bool": {
"should": [
{
"match": {
"description": {
"query": "coyote"
}
}
},
{
"bool": {
"should": [
{
"terms": {
"colours.name": [
"red",
"green"
]
}
},
{
"terms": {
"categories.name": [
"Cartoon"
]
}
}
]
}
}
]
}
},
"aggs": {
"colours": {
"terms": {
"field": "colour.name.value",
"size": 100
}
},
"categories": {
"terms": {
"field": "categories.id",
"size": 100
}
}
}
}