【问题标题】:ElasticSearch: How to search for a value in any field, across all types, in one or more indices?ElasticSearch:如何在一个或多个索引中搜索所有类型的任何字段中的值?
【发布时间】:2016-03-12 21:01:50
【问题描述】:

我有两个索引 my_index_1my_index_2。在这些索引中,我有以下文档类型:

my_index_1:

  • 组织
  • 角色
  • 技能

my_index_2:

  • 产品
  • 服务
  • 专利
  • 商标
  • 服务标记

每种类型都有不同的字段。

我的问题: 在任何类型的任何字段、任何一个甚至两个索引中查询字符串“abc”的最佳方式是什么? p>

我在文档中没有看到任何有助于此类搜索的内容。有没有可能看起来像的东西:

$ curl -XPOST 'localhost:9200/_search?pretty' -d '
{
  "query": { "match": { *: "abc" } }
}'

在此先感谢您提供的任何帮助。

【问题讨论】:

    标签: elasticsearch match


    【解决方案1】:

    query_string querymatch query 将是您要查找的内容。

    如果default_field 中未指定任何_all fieldquery_string 将使用特殊的_all field,这样效果会很好。

    curl -XPOST 'localhost:9200/_search?pretty' -d '{
      "query": { "query_string": { "query": "abc" } }
    }'
    

    对于match,您也可以指定_all

    curl -XPOST 'localhost:9200/_search?pretty' -d '{
      "query": { "match": { "_all": "abc" } }
    }'
    

    请注意,query_string 可以使用通配符,match 不能使用通配符


    更新:

    由于 _all 字段在 6.0 中已弃用,因此现在的解决方案是实现 custom all field

    【讨论】:

    • 嗨,瓦尔。感谢您提供两种选择。我现在可以将 match 与 _all 一起使用,但我肯定需要更深入地研究 query_string,因为将来我将需要使用通配符。所以,很高兴知道。
    • 请注意 _all 在 6.0.0 中已弃用 See here
    • @FaisalMq 是的,现在解决方案是实现custom all field
    【解决方案2】:

    现在已弃用的 _all 字段的替代方法是 multi match query

    GET /_search
    {
      "query": {
        "multi_match" : {
          "query":    "Will Smith",
          "fields": [ "important_field^5", "less_important^2", "title", "*_name" ] 
        }
      }
    }
    

    支持字段名称中的注意通配符,并使用 ^# 语法提高字段的重要性

    从 7.3 版开始: 如果未提供任何字段,则 multi_match 查询默认为 index.query.default_field 索引设置,而后者又默认为 *。 * 提取映射中符合术语查询条件的所有字段并过滤元数据字段。然后组合所有提取的字段以构建查询。

    另一种选择是Query string query

    GET /_search
    {
        "query": {
            "query_string" : {
                "query" : "(new york city) OR (big apple)",
                "default_field" : "content"
            }
        }
    }
    

    default_field (可选,字符串)如果查询字符串中没有提供字段,您希望搜索的默认字段。

    默认为 index.query.default_field 索引设置,默认值为 *。

    【讨论】:

    • 谢谢,这可能是目前最相关的答案。帮了我很多忙!
    • 虽然 Val 的回答也是正确的,但这个回答对我来说是信息最丰富/最相关的。
    【解决方案3】:

    我认为您可以为此目的使用_all

    试试这个

    $ curl -XPOST 'localhost:9200/_search?pretty' -d '
    {
      "query": { "match": { "_all": "abc" } }
    }'
    

    您也可以使用query_string,因为它默认搜索_all

    【讨论】:

      【解决方案4】:

      试试这个得到所有索引和所有字段的结果:

      GET _all/_search
      {
      "query" : {
         "multi_match": {
            "query": "abc",
            "fields": []
             }
          }
       }
      

      【讨论】:

        猜你喜欢
        • 2015-04-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-09-12
        • 1970-01-01
        • 2018-08-01
        • 1970-01-01
        相关资源
        最近更新 更多