/*
 * Copyright (c) 2016-2021 Flux Capacitor.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *     http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.fluxcapacitor.common.api.search;

import com.fasterxml.jackson.annotation.JsonProperty;
import io.fluxcapacitor.common.api.search.constraints.AllConstraint;
import io.fluxcapacitor.common.search.Document;
import lombok.Builder;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Singular;
import lombok.ToString;
import lombok.Value;
import lombok.experimental.Accessors;

import java.time.Instant;
import java.util.List;
import java.util.stream.Collectors;

@Value
@Builder(toBuilder = true, builderClassName = "Builder")
public class SearchQuery {
    @JsonProperty("collection")
    @Singular List<String> collections;
    Instant since, before;
    boolean requireTimestamp;
    @Singular List<Constraint> constraints;

    @ToString.Exclude
    @EqualsAndHashCode.Exclude
    @Getter(lazy = true) @Accessors(fluent = true)
    Constraint decomposeConstraints = AllConstraint.all(constraints.stream().map(Constraint::decompose).collect(
            Collectors.toList()));

    @SuppressWarnings("RedundantIfStatement")
    public boolean matches(Document d) {
        if (!decomposeConstraints().matches(d)) {
            return false;
        }
        if (d.getTimestamp() == null) {
            return !requireTimestamp;
        }
        if (since != null && d.getTimestamp().compareTo(since) < 0) {
            return false;
        }
        if (before != null && d.getEnd().compareTo(before) >= 0) {
            return false;
        }
        if (!collections.isEmpty() && !collections.contains(d.getCollection())) {
            return false;
        }
        return true;
    }
}

@@ -1787,16 +1787,36 @@
     if (
+requireTimestamp && 
 d.getTim
@@ -1858,25 +1858,13 @@
 urn 
-!requireTimestamp
+false
 ;%0A  
@@ -1895,24 +1895,52 @@
 e != null &&
+ d.getTimestamp() != null &&
  d.getTimest
@@ -2030,24 +2030,46 @@
 e != null &&
+ d.getEnd() != null &&
  d.getEnd().

Fix SearchQuery match for documents without timestamp