tomglk commented on a change in pull request #166: URL: https://github.com/apache/solr/pull/166#discussion_r648660579
########## File path: solr/contrib/ltr/src/test/org/apache/solr/ltr/TestCacheInteractionOfPrefetchingFieldValueFeature.java ########## @@ -0,0 +1,530 @@ +/* * 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.solr.ltr; + +import org.apache.lucene.document.Document; +import org.apache.lucene.document.Field; +import org.apache.lucene.document.StoredField; +import org.apache.lucene.index.LeafReaderContext; +import org.apache.lucene.misc.document.LazyDocument; +import org.apache.lucene.search.DocIdSetIterator; +import org.apache.lucene.search.IndexSearcher; +import org.apache.lucene.search.Query; +import org.apache.solr.client.solrj.SolrQuery; +import org.apache.solr.client.solrj.response.QueryResponse; +import org.apache.solr.common.SolrDocument; +import org.apache.solr.common.SolrInputDocument; +import org.apache.solr.ltr.feature.FeatureException; +import org.apache.solr.ltr.feature.PrefetchingFieldValueFeature; +import org.apache.solr.ltr.model.LinearModel; +import org.apache.solr.request.SolrQueryRequest; +import org.apache.solr.search.SolrDocumentFetcher; +import org.junit.Test; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Locale; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.stream.Collectors; +import java.util.stream.IntStream; + +import static java.util.stream.Collectors.toList; +import static org.apache.solr.ltr.feature.PrefetchingFieldValueFeature.DISABLE_PREFETCHING_FIELD_VALUE_FEATURE; + +public class TestCacheInteractionOfPrefetchingFieldValueFeature extends TestLTROnSolrCloudBase { + private final String LAZY_FIELD_LOADING_CONFIG_KEY = "solr.query.enableLazyFieldLoading"; + + private static final String FEATURE_STORE_NAME = "test"; + private static final int NUM_FEATURES = 6; + private static final String[] FIELD_NAMES = new String[]{"storedIntField", "storedLongField", + "storedFloatField", "storedDoubleField", "storedStrNumField", "storedStrBoolField"}; + private static final String[] FEATURE_NAMES = new String[]{"storedIntFieldFeature", "storedLongFieldFeature", + "storedFloatFieldFeature", "storedDoubleFieldFeature", "storedStrNumFieldFeature", "storedStrBoolFieldFeature"}; + private static final String MODEL_WEIGHTS = "{\"weights\":{\"storedIntFieldFeature\":0.1,\"storedLongFieldFeature\":0.1," + + "\"storedFloatFieldFeature\":0.1,\"storedDoubleFieldFeature\":0.1," + + "\"storedStrNumFieldFeature\":0.1,\"storedStrBoolFieldFeature\":0.1}}"; + + @Override + void setupSolrCluster(int numShards, int numReplicas) throws Exception { + // we do not want to test the scoring / ranking but the interaction with the cache + // because the scoring itself behaves just like the FieldValueFeature + // so just one shard, replica and node serve the purpose + setupSolrCluster(1, 1, 1); + } + + @Test + public void testSimpleQuery() throws Exception { + ObservingPrefetchingFieldValueFeature.setBreakPrefetching(false); + ObservingPrefetchingFieldValueFeature.loadedFields = new HashMap<>(); + System.setProperty(LAZY_FIELD_LOADING_CONFIG_KEY, "false"); + // needed to clear cache because we make assertions on its content + reloadCollection(COLLECTION); + + SolrQuery query = new SolrQuery("{!func}sub(8,field(popularity))"); + query.setRequestHandler("/query"); + query.setParam("rows", "8"); + query.setFields("id,features:[fv]"); + query.add("rq", "{!ltr model=powpularityS-model reRankDocs=8}"); + + QueryResponse queryResponse = solrCluster.getSolrClient().query(COLLECTION,query); + + Map<String, List<List<String>>> loadedFields = ObservingPrefetchingFieldValueFeature.loadedFields; + + assertEquals(loadedFields.size(), queryResponse.getResults().size()); + for (SolrDocument doc : queryResponse.getResults()) { + String docId = (String) doc.getFirstValue("id"); + if (docId.equals("1")) { + assertEquals(NUM_FEATURES, loadedFields.get(docId).stream() + // doc with id 1 has no values set for 3 of the 6 feature fields + .filter(fieldLoadedList -> fieldLoadedList.size() == NUM_FEATURES - 3) + .count()); + } else { + // all the fields were loaded at once + assertEquals(NUM_FEATURES, loadedFields.get(docId).stream() + .filter(fieldLoadedList -> fieldLoadedList.size() == NUM_FEATURES) + .count()); + } + } + assertTheResponse(queryResponse); + } + + @Test + public void testSimpleQueryLazy() throws Exception { + ObservingPrefetchingFieldValueFeature.setBreakPrefetching(false); + ObservingPrefetchingFieldValueFeature.loadedFields = new HashMap<>(); + System.setProperty(LAZY_FIELD_LOADING_CONFIG_KEY, "true"); + // needed to clear cache because we make assertions on its content + reloadCollection(COLLECTION); + + SolrQuery query = new SolrQuery("{!func}sub(8,field(popularity))"); + query.setRequestHandler("/query"); + query.setParam("rows", "8"); + query.setFields("id,features:[fv]"); + query.add("rq", "{!ltr model=powpularityS-model reRankDocs=8}"); + + QueryResponse queryResponse = solrCluster.getSolrClient().query(COLLECTION,query); + + Map<String, List<List<String>>> loadedFields = ObservingPrefetchingFieldValueFeature.loadedFields; + + for (SolrDocument doc : queryResponse.getResults()) { + String docId = (String) doc.getFirstValue("id"); + if (docId.equals("1")) { + assertEquals(NUM_FEATURES, loadedFields.get(docId).stream() + // doc with id 1 has no values set for 3 of the 6 feature fields + .filter(fieldLoadedList -> fieldLoadedList.size() == NUM_FEATURES - 3) + .count()); + } else { + // all the fields were loaded at once + assertEquals(NUM_FEATURES, loadedFields.get(docId).stream() + .filter(fieldLoadedList -> fieldLoadedList.size() == NUM_FEATURES) + .count()); + } + } + assertTheResponse(queryResponse); + } + + @Test + public void testSimpleQueryBreakPrefetching() throws Exception { + ObservingPrefetchingFieldValueFeature.setBreakPrefetching(true); + ObservingPrefetchingFieldValueFeature.loadedFields = new HashMap<>(); + System.setProperty(LAZY_FIELD_LOADING_CONFIG_KEY, "false"); + // needed to clear cache because we make assertions on its content + reloadCollection(COLLECTION); + + SolrQuery query = new SolrQuery("{!func}sub(8,field(popularity))"); + query.setRequestHandler("/query"); + query.setParam("rows", "8"); + query.setFields("id,features:[fv]"); + query.add("rq", "{!ltr model=powpularityS-model reRankDocs=8}"); + + QueryResponse queryResponse = + solrCluster.getSolrClient().query(COLLECTION,query); + + Map<String, List<List<String>>> loadedFields = ObservingPrefetchingFieldValueFeature.loadedFields; + + assertEquals(loadedFields.size(), queryResponse.getResults().size()); + for (SolrDocument doc : queryResponse.getResults()) { + String docId = (String) doc.getFirstValue("id"); + if (docId.equals("1")) { + // doc with id 1 has no values set for 3 of the 6 feature fields + assertEquals(NUM_FEATURES - 3, loadedFields.get(docId).stream() + .filter(fieldLoadedList -> fieldLoadedList.size() == 1) + .count()); + } else { + // each single field used for a feature gets loaded separately + assertEquals(NUM_FEATURES, loadedFields.get(docId).stream() + .filter(fieldLoadedList -> fieldLoadedList.size() == 1) + .count()); + } + } + assertTheResponse(queryResponse); + } + + @Test + public void testSimpleQueryLazyBreakPrefetching() throws Exception { Review comment: I hope that the commit to make the tests more comprehensible ( 2d355c7 ) helps with identifying overlap. The `BreakPrefetching` and `DisablePrefetching` tests are still very similar. They are not intended to test different behavior but rather that different configurations lead to the same behavior. -- 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. For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
