2017-09-14 68 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" 
              } 
             } 
            ] 
           } 
          } 
         ] 
        } 
       } 
      ] 
     } 
    } 
} 
相關問題