dengzhhu653 commented on code in PR #6595: URL: https://github.com/apache/hive/pull/6595#discussion_r4355825103
########## standalone-metastore/metastore-search/src/main/java/org/apache/hive/search/search/Searcher.java: ########## @@ -0,0 +1,293 @@ +/* + * 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.hive.search.search; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.Locale; +import java.util.Objects; + +import org.apache.commons.lang3.StringUtils; +import org.apache.hadoop.hive.common.TableName; +import org.apache.hadoop.hive.metastore.api.Table; +import org.apache.hive.search.config.SearchOptions; +import org.apache.hive.search.exception.InferenceException; +import org.apache.hive.search.exception.SearchException; +import org.apache.hive.search.mapping.FieldSchema; +import org.apache.hive.search.mapping.IndexMapping; +import org.apache.hive.search.mapping.TableDocument; +import org.apache.hive.search.mapping.TextFieldSchema; +import org.apache.hive.search.metastore.MetastoreTableMapper; +import org.apache.hive.search.metastore.TableBlobCodec; +import org.apache.hive.search.index.IndexManager; +import org.apache.hive.search.inference.Embedder; +import org.apache.hive.search.inference.EmbedderRegistry; +import org.apache.lucene.document.Document; +import org.apache.lucene.index.IndexableField; +import org.apache.lucene.index.Term; +import org.apache.lucene.queryparser.simple.SimpleQueryParser; +import org.apache.lucene.search.BayesianScoreEstimator; +import org.apache.lucene.search.BayesianScoreQuery; +import org.apache.lucene.search.BooleanClause; +import org.apache.lucene.search.BooleanQuery; +import org.apache.lucene.search.BoostQuery; +import org.apache.lucene.search.ConstantScoreQuery; +import org.apache.lucene.search.DisjunctionMaxQuery; +import org.apache.lucene.search.IndexSearcher; +import org.apache.lucene.search.KnnFloatVectorQuery; +import org.apache.lucene.search.LogOddsFusionQuery; +import org.apache.lucene.search.Query; +import org.apache.lucene.search.ScoreDoc; +import org.apache.lucene.search.SearcherManager; +import org.apache.lucene.search.TermQuery; +import org.apache.lucene.search.PrefixQuery; +import org.apache.lucene.search.TopDocs; + +public final class Searcher implements AutoCloseable { + private final EmbedderRegistry embedderRegistry; + private final IndexSearcher indexSearcher; + private final SearcherManager searcherManager; + private final IndexMapping mapping; + private final SearchOptions searchConfig; + private final BayesianScoreEstimator.Parameters parameters; + private final long processedEventId; + + public Searcher(SearcherManager manager, + IndexManager indexManager, + EmbedderRegistry registry, + SearchOptions searchConfig, + BayesianScoreEstimator.Parameters parameters) throws IOException { + this.searcherManager = manager; + this.processedEventId = indexManager.getProcessedEventId(); + this.indexSearcher = manager.acquire(); + this.mapping = indexManager.mapping(); + this.searchConfig = searchConfig; + this.parameters = parameters; + this.embedderRegistry = Objects.requireNonNull(registry, "Embedder registry"); + } + + public TableSearchResult search(SearchQuery request) + throws SearchException, IOException { + if (StringUtils.isBlank(request.body().queryText())) { + throw new SearchException("query text is required"); + } + Query filter = compileScopeFilter(request.catalogName(), request.databaseName()); + int size = request.limit() > 0 ? request.limit() : searchConfig.getDefaultLimit(); + Query query = + switch (request.body()) { + case MatchQuery match -> { + Query matchQuery = compileMatchQuery(match.resolve(mapping)); + if (filter == null) { + yield matchQuery; + } + yield new BooleanQuery.Builder() + .add(matchQuery, BooleanClause.Occur.MUST) + .add(filter, BooleanClause.Occur.FILTER) + .build(); + } + case SemanticQuery semantic -> compileSemanticQuery(semantic.resolve(mapping), size, filter); + case HybridQuery hybrid -> compileFusionQuery(hybrid.resolve(mapping), size, filter); + }; + TopDocs topDocs = indexSearcher.search(query, size); + List<TableSearchHit> hits = new ArrayList<>(); + for (ScoreDoc scoreDoc : topDocs.scoreDocs) { + hits.add(readHit(scoreDoc)); + } + return new TableSearchResult(hits, topDocs.totalHits.value(), processedEventId); + } + + public TableSearchResult loadTables(List<TableName> tableNames) throws SearchException, IOException { + if (tableNames == null || tableNames.isEmpty()) { + return new TableSearchResult(List.of(), 0, processedEventId); + } + List<String> ids = new ArrayList<>(tableNames.size()); + for (TableName table : tableNames) { + ids.add(MetastoreTableMapper.tableId(table)); + } + BooleanQuery.Builder builder = new BooleanQuery.Builder(); + for (String id : ids) { + builder.add( + new TermQuery(new Term("_id" + TableDocument.FILTER_SUFFIX, id)), + BooleanClause.Occur.SHOULD); + } + TopDocs hits = indexSearcher.search(builder.build(), ids.size()); + List<TableSearchHit> searchHits = new ArrayList<>(hits.scoreDocs.length); + for (ScoreDoc scoreDoc : hits.scoreDocs) { + searchHits.add(readHit(scoreDoc)); + } + return new TableSearchResult(searchHits, hits.totalHits.value(), processedEventId); + } + + private Query compileFusionQuery(HybridQuery query, int semanticK, Query filter) + throws SearchException { + Query matchQuery = new BayesianScoreQuery( Review Comment: Done -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
