All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.lucene.monitor.CandidateMatcher Maven / Gradle / Ivy

There is a newer version: 10.1.0
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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 org.apache.lucene.monitor;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;

/** Class used to match candidate queries selected by a Presearcher from a Monitor query index. */
public abstract class CandidateMatcher {

  /** The searcher to run candidate queries against */
  protected final IndexSearcher searcher;

  private final Map errors = new HashMap<>();
  private final List> matches;

  private long searchTime = System.nanoTime();

  private static class MatchHolder {
    Map matches = new HashMap<>();
  }

  /**
   * Creates a new CandidateMatcher for the supplied DocumentBatch
   *
   * @param searcher the IndexSearcher to run queries against
   */
  public CandidateMatcher(IndexSearcher searcher) {
    this.searcher = searcher;
    int docCount = searcher.getIndexReader().maxDoc();
    this.matches = new ArrayList<>(docCount);
    for (int i = 0; i < docCount; i++) {
      this.matches.add(new MatchHolder<>());
    }
  }

  /**
   * Runs the supplied query against this CandidateMatcher's set of documents, storing any resulting
   * match, and recording the query in the presearcher hits
   *
   * @param queryId the query id
   * @param matchQuery the query to run
   * @param metadata the query metadata
   * @throws IOException on IO errors
   */
  protected abstract void matchQuery(String queryId, Query matchQuery, Map metadata)
      throws IOException;

  /**
   * Record a match
   *
   * @param match a QueryMatch object
   */
  protected final void addMatch(T match, int doc) {
    MatchHolder docMatches = matches.get(doc);
    docMatches.matches.compute(
        match.getQueryId(),
        (key, oldValue) -> {
          if (oldValue != null) {
            return resolve(match, oldValue);
          }
          return match;
        });
  }

  /**
   * If two matches from the same query are found (for example, two branches of a disjunction),
   * combine them.
   *
   * @param match1 the first match found
   * @param match2 the second match found
   * @return a Match object that combines the two
   */
  public abstract T resolve(T match1, T match2);

  /** Called by the Monitor if running a query throws an Exception */
  void reportError(String queryId, Exception e) {
    this.errors.put(queryId, e);
  }

  /**
   * @return the matches from this matcher
   */
  final MultiMatchingQueries finish(long buildTime, int queryCount) {
    doFinish();
    this.searchTime =
        TimeUnit.MILLISECONDS.convert(System.nanoTime() - searchTime, TimeUnit.NANOSECONDS);
    List> results = new ArrayList<>();
    for (MatchHolder matchHolder : matches) {
      results.add(matchHolder.matches);
    }
    return new MultiMatchingQueries<>(
        results, errors, buildTime, searchTime, queryCount, matches.size());
  }

  /** Called when all monitoring of a batch of documents is complete */
  protected void doFinish() {}

  /** Copy all matches from another CandidateMatcher */
  protected void copyMatches(CandidateMatcher other) {
    this.matches.clear();
    this.matches.addAll(other.matches);
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy