2014-03-13 134 views
0

刚刚进行弹性搜索,这是我的查询,以查找我的集合中的完全匹配。弹性搜索搜索查询与过滤器

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

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

+0

如果你的'_id'字段是唯一的,你的查询总是返回一个查询。你在说'给我带来_id = 1'的结果。大概,这会返回一个结果,然后过滤这个结果是没有意义的。这将是更好的,如果你解释你想做什么 –

+0

@HüseyinBABAL感谢您的快速回复,新的弹性搜索 我的需求在mysql的方式是 select * from message where id = 1 and team_id = 2 and created_user = 099 – Dibish

+0

如果你在真实世界中解释你的请求(不是以elasticsearch的方式),我们可以帮助你更好 –

回答

1

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

{ 
    "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"} 
       } 
      ] 
     }  
    } 
} 
+0

非常感谢你HüseyinBABAL ...它真的帮助我.. – Dibish