我有以下查询可以正常工作:
GET <index_name>/_search
{
"sort": [
{
"irFileCreateTime": {
"order": "desc"
}
}
],
"query": {
"bool": {
"should": [
{
"match": {
"fileId": 46704
}
},
{
"match": {
"fileId": 46706
}
},
{
"match": {
"fileId": 46719
}
}
]
}
}
}
问题是我需要进一步过滤数据,但是我需要过滤的字段是文本字段。我尝试了多种不同的方式来将必须匹配项放入查询中,但是当我知道它只过滤掉一半时,所有内容要么格式错误,要么过滤掉所有匹配。如何向此查询添加必须匹配的“irStatus”:“COMPLETE”?提前致谢。
最佳答案
您所追求的是查询term
,最好是查询keyword
的irStatus
。也就是说:
GET index/_search
{
"sort": [
{
"irFileCreateTime": {
"order": "desc"
}
}
],
"query": {
"bool": {
"must": [
{
"term": {
"irStatus.keyword": {
"value": "COMPLETE"
}
}
}
],
"should": [
{
"match": {
"fileId": 46704
}
},
{
"match": {
"fileId": 46706
}
},
{
"match": {
"fileId": 46719
}
}
]
}
}
}
假设您的映射如下所示:
{
"mappings": {
"properties": {
"irFileCreateTime": {
"type": "date"
},
"fileId": {
"type": "integer"
},
"irStatus": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword"
}
}
}
}
}
}
它显然在您端上失败的原因是由于
standard
分析器,“COMPLETE”已小写。或者,您可以执行以下操作:
{
"must":[
{
"query_string":{
"query":"irStatus:COMPLETE AND (fileId:(46704 OR 46706 OR 46719))"
}
}
]
}
关于elasticsearch - elasticsearch需要在 bool 中添加一个必须的查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62312011/