【问题标题】:filter '_index' same way as '_type' in search across multiple index query elastic search在跨多个索引查询弹性搜索的搜索中过滤“_index”的方式与“_type”相同
【发布时间】:2014-10-01 02:52:54
【问题描述】:

我有两个索引 index1 和 index2 并且都有两个类型 type1 和 type2 同名弹性搜索。(请假设我们有正当的商业理由)

我想搜索 index1 - type1 和 index2 -type2

这是我的查询

POST _search
{
 "query": {
    "indices": {
      "indices": ["index1","index2"],      
      "query": {
        "filtered":{  
         "query":{
       "multi_match": {
           "query": "test",
           "type": "cross_fields",
           "fields": ["_all"]         
       }

        },
         "filter":{  
            "or":{  
               "filters":[  
                  {  
                     "terms":{ 
                                "_index":["index1"], // how can i make this work?
                               "_type": ["type1"]
                     }                      
                  },
                  {  
                     "terms":{ 
                               "_index":["index2"], // how can i make this work?
                               "_type": ["type2"]
                     }                      
                  }
               ]
            }
         }
      }
      },
      "no_match_query":"none"
    }
  }
 }

【问题讨论】:

  • 你能解释一下你面临的问题是什么。您的查询对我来说很好,我有相同的场景多个索引具有相同的类型。
  • 嗨 Roopendra,当我执行上面的查询时,它会返回我不想要的 index1->type2 和 index2->type1 的结果。我只想要 index1->type1 和 index2->type2。我相信这是因为它没有过滤“_index”字段上的术语。希望这是有道理的。非常感谢

标签: json elasticsearch


【解决方案1】:

您可以在bool 过滤器中使用indices、type 来过滤类型和索引 查询将在以下几行中显示:

POST  index1,index2/_search
{
  "query": {
    "filtered": {
      "query": {
        "multi_match": {
          "query": "test",
          "type": "cross_fields",
          "fields": [
            "_all"
          ]
        }
      },
      "filter": {
        "bool": {
          "should": [
            {
              "indices": {
                "index": "index1",
                "filter": {
                  "type": {
                    "value": "type1"
                  }
                },
                "no_match_filter": "none"
              }
            },
            {
              "indices": {
                "index": "index2",
                "filter": {
                  "type": {
                    "value": "type2"
                  }
                },
                "no_match_filter": "none"
              }
            }
          ]
        }
      }
    }
  }
}

在 url 示例中传递索引名称:index1,index2/_search 是一种很好的做法,否则您可能会冒跨集群中所有索引执行查询的风险。

【讨论】:

  • 非常感谢。在索引过滤器中再做一项改进,我会使用术语,这样我就可以在其中包含多种类型。 “过滤器”:{“术语”:{“_type”:[“type1”,“type3”,“type5”]}}
猜你喜欢
  • 2020-10-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-25
  • 1970-01-01
  • 2014-06-22
  • 1970-01-01
  • 2017-11-25
相关资源
最近更新 更多