简体   繁体   English

弹性搜索布尔查询

[英]Elastic search bool query

My objective is to find out most recent 10 documents which match message id as MSG-1013 and Severity field must be info. 我的目标是找出与消息ID匹配的最新10个文档,因为MSG-1013和“严重性”字段必须为info。 Both conditions should satisfied and match text should be exact. 这两个条件都应满足,并且匹配文本应准确。 I have tried with search query below but it does not give me expected results. 我已经尝试过使用下面的搜索查询,但是它没有给我预期的结果。 What am I doing wrong here ? 我在这里做错了什么?

{
    "size": 10,
    "query": {
        "bool": { 
            "must": [
                {
                    "match": { "messageId": "MSG-1013" }
                },
                {
                    "match": { "Severity": "Info" }
                }
            ] 
        }
    }
}

If I have understood you correctly, you want to find the top 10 (recent) documents having exactly fields "messageId" and "Severity". 如果我对您的理解正确,则希望找到前十个(最近)文档,它们的字段完全为“ messageId”和“ Severity”。 I assume, you don't need a score because your score seems to be the the document timestamp or something else like a date field. 我认为,您不需要分数,因为您的分数似乎是文档时间戳或其他类似日期字段的分数。 For this purpose, you could use the bool filter in combination with a sort query. 为此,可以将布尔过滤器与排序查询结合使用。

{ 
    "query": {
        "bool": {
            "filter": [
                { "term": { "messageId": "MSG-1013" } },
                { "term": { "Severity": "Info" } }
            ]
        }
    },
    "sort" : [
        { "documentTimestamp" : {"order" : "desc"}}
    ],
    "size": 10
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM