2017-09-14 105 views
1

有2个索引:categories,postsElasticsearch仅在索引有字段时使用索引过滤器

类别

帖子

  • 身体
  • publish_at
  • publish_until

我想为posts指数做一个过滤器两个索引的查询上publish_atpublish_until

http://localhost:9200/categories,posts/_search

{ 
     "query": { 
      "bool": { 
       "must": { 
        "multi_match": { 
         "query": "keyword", 
         "fields": [ 
          "name^3", 
          "body" 
         ] 
        } 
      }, 
      "filter": [{ 
       "bool": { 
        "must": [ 
         { 
          "range": { 
           "publish_at": { 
            "lte" : "now" 
           } 
          } 
         }, 
         { 
          "range": { 
           "publish_until": { 
            "gt" : "now" 
           } 
          } 
         } 
        ] 
       } 
      }] 
     } 
    } 
} 

此查询仅给了我posts的结果。我也想在我的结果中使用categories

如何将日期范围过滤器仅应用于publish_atpublish_until字段的索引,并跳过其他索引的日期范围过滤器?

回答

1

bool摆弄了一天之后,好吧,我得到它的工作:

{ 
    "query": { 
     "bool" : { 
      "must" : [ 
       { 
        "multi_match": { 
         "query": "keyword", 
         "fields": [ 
          "name^3", 
          "body" 
         ] 
        } 
       }, 
       { 
        "bool": { 
         "should": [ 
          { 
           "bool": { 
            "must": [ 
             { 
              "range": { 
               "publish_at": { 
                "lte" : "now" 
               } 
              } 
             }, 
             { 
              "range": { 
               "publish_until": { 
                "gt" : "now" 
               } 
              } 
             } 
            ] 
           } 
          }, 
          { 
           "bool": { 
            "must_not": [ 
             { 
              "exists": { 
               "field": "publish_at" 
              } 
             }, 
             { 
              "exists": { 
               "field": "publish_until" 
              } 
             } 
            ] 
           } 
          } 
         ] 
        } 
       } 
      ] 
     } 
    } 
}