简体   繁体   English

Elasticsearch:按任意字段过滤

[英]Elasticsearch: filter by any field

I am playing with filters in elasticsearch (we use old version 1.3.1), and I need to filter my search results by any field. 我在Elasticsearch中使用过滤器(我们使用旧版本1.3.1),并且需要按任何字段过滤搜索结果。 With query, this can be done like this: 使用查询,可以这样完成:

"query": {
  "query_string": {
    "query": "_all:test"
  }
}

But filters seems to not work with _all statement. 但是过滤器似乎不适用于_all语句。 What can I do? 我能做什么? Would newer elasticsearch version solve my problem? 较新的elasticsearch版本可以解决我的问题吗?

Thanks in advance! 提前致谢!

PS: I need to search exact values, so I cannot use queries. PS:我需要搜索确切的值,所以我不能使用查询。 There is difference between queries and filters - if you search for my brown , then you can expect results like: 查询和过滤器之间有区别-如果您搜索my brown ,那么您可以期望得到类似以下结果:

my brown

This is my brown dog.

someone stolen my brown wallet

But filter will return only my brown , and that is what I need. 但是filter只返回my brown ,这就是我所需要的。

You might want to read up a little on the distinction between queries and filters . 您可能需要稍微了解一下查询和过滤器之间的区别。 What you're doing there is a query string query . 您在执行的操作是查询字符串query

If you do actually want to filter against exact text tokens (read up on analysis if you don't know what I mean by "tokens"), AND you have your mapping set up such that the "_all" field behaves as you're expecting then try something like this: 如果您确实想过滤确切的文本标记(如果您不了解“标记”的含义,请"_all"阅读分析 ),然后设置映射,使"_all"字段的行为与您期待然后尝试这样的事情:

POST /test_index/_search
{
   "query": {
      "filtered": {
         "filter": {
            "term": {
               "_all": "test"
            }
         }
      }
   }
}

If, on the other hand, you want to allow some analysis (so that "Test" is tokenized to "test" , for example), you may want this instead: 另一方面,如果您希望进行某些分析(例如,将"Test"标记为"test" ),则可能需要这样做:

POST /test_index/_search
{
   "query": {
      "match": {
         "_all": "Test"
      }
   }
}

Here is some code I used to play around with it: 这是我以前玩过的一些代码:

http://sense.qbox.io/gist/44adf2c2ade8abd6758f0e08ed2e40434850fc1c http://sense.qbox.io/gist/44adf2c2ade8abd6758f0e08ed2e40434850fc1c

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

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