【问题标题】:Elastic search search query with Filter使用过滤器的弹性搜索搜索查询
【发布时间】:2014-03-13 08:50:15
【问题描述】:

我是弹性搜索的新手,这是我在我的集​​合中查找完全匹配的查询。

{
   "query": {
      "filtered": {
         "query": {
            "match": {
               "_id": {
                  "query": "1"
               }
            }
         },      
       "filter": {
                "term" : {
                    "team_id":"2",
                    "created_user":"099"
                }
            }     
   }
   }
}

通过运行此查询,我得到了一条记录,但我的问题是它与“team_id”字段不匹配。当我将 team_id 更改为其他值时,例如:4,我仍然得到 team_id = 2 的记录,请帮助我编写一个包含三个字段的弹性搜索查询。谢谢

【问题讨论】:

  • 如果您的 _id 字段是唯一的,您的查询将始终返回一个查询。你是说Give me the result that has _id=1。可能,这将返回一个结果,然后过滤该结果没有意义。如果您解释一下您要做什么,那就更好了
  • @HüseyinBABAL 感谢您的快速回复,我是弹性搜索的新手,我在 mysql 方式中的要求是 select * from message where id = 1 and team_id = 2 and created_user = 099
  • 如果您在现实世界中解释您的请求(而不是弹性搜索方式),我们可以更好地帮助您

标签: node.js elasticsearch amazon-elastic-beanstalk


【解决方案1】:

如果您想要完全匹配,请确保您要进行搜索操作的字段必须是not_analyzed。看来您在过滤器中使用了多个案例。您可以使用and filter like 重构您的查询;

{
   "query": {
      "filtered": {
         "query": {
            "match": {
               "_id": {
                  "query": "1"  // remember that, this will always return one result. Update here according to your needs. For example, use tag instead of _id like tag=responsive in order to get results that matches tag field with responsive
               }
            }
         },      
       "filter": {
            "and": [
                {
                  "term": {"team_id":"2"}
                },
                {
                  "term": {"created_user":"099"}
                }
            ]
        }     
   }
}

【讨论】:

    猜你喜欢
    • 2015-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-28
    • 2019-12-17
    • 1970-01-01
    相关资源
    最近更新 更多