Java Code Examples for org.apache.solr.client.solrj.SolrQuery#add()

The following examples show how to use org.apache.solr.client.solrj.SolrQuery#add() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestFieldLengthFeature.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Test
public void testIfFieldIsMissingInDocumentLengthIsZero() throws Exception {
  // add a document without the field 'description'
  assertU(adoc("id", "42", "title", "w10"));
  assertU(commit());

  loadFeature("description-length2", FieldLengthFeature.class.getName(),
          "{\"field\":\"description\"}");

  loadModel("description-model2", LinearModel.class.getName(),
          new String[] {"description-length2"}, "{\"weights\":{\"description-length2\":1.0}}");

  final SolrQuery query = new SolrQuery();
  query.setQuery("title:w10");
  query.add("fl", "*, score");
  query.add("rows", "4");
  query.add("rq", "{!ltr model=description-model2 reRankDocs=8}");
  assertJQ("/query" + query.toQueryString(), "/response/docs/[0]/score==0.0");
}
 
Example 2
Source File: TestExternalFeatures.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Test
public void featureExtraction_valueFeatureExplicitlyNotRequired_shouldNotScoreFeature() throws Exception {
  final SolrQuery query = new SolrQuery();
  query.setQuery("*:*");
  query.add("rows", "1");

  final String docs0fvalias_dense_csv = FeatureLoggerTestUtils.toFeatureVector(
      "occurrences","0.0",
      "originalScore","1.0");
  final String docs0fvalias_sparse_csv = FeatureLoggerTestUtils.toFeatureVector(
      "originalScore","1.0");

  final String docs0fvalias_default_csv = chooseDefaultFeatureVector(docs0fvalias_dense_csv, docs0fvalias_sparse_csv);

  // Efi is explicitly not required, so we do not score the feature
  query.remove("fl");
  query.add("fl", "fvalias:[fv store=fstore3]");
  assertJQ("/query" + query.toQueryString(), "/response/docs/[0]/fvalias=='"+docs0fvalias_default_csv+"'");
}
 
Example 3
Source File: TestFieldLengthFeature.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Test
public void testIfFieldIsEmptyLengthIsZero() throws Exception {
  // add a document without the field 'description'
  assertU(adoc("id", "43", "title", "w11", "description", ""));
  assertU(commit());

  loadFeature("description-length3", FieldLengthFeature.class.getName(),
          "{\"field\":\"description\"}");

  loadModel("description-model3", LinearModel.class.getName(),
          new String[] {"description-length3"}, "{\"weights\":{\"description-length3\":1.0}}");

  final SolrQuery query = new SolrQuery();
  query.setQuery("title:w11");
  query.add("fl", "*, score");
  query.add("rows", "4");
  query.add("rq", "{!ltr model=description-model3 reRankDocs=8}");
  assertJQ("/query" + query.toQueryString(), "/response/docs/[0]/score==0.0");
}
 
Example 4
Source File: TestExternalFeatures.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Test
public void testEfiFeatureExtraction() throws Exception {
  final SolrQuery query = new SolrQuery();
  query.setQuery("*:*");
  query.add("rows", "1");

  final String docs0fv_csv = FeatureLoggerTestUtils.toFeatureVector(
      "occurrences","2.3", "originalScore","1.0");

  // Features we're extracting depend on external feature info not passed in
  query.add("fl", "[fv]");
  assertJQ("/query" + query.toQueryString(), "/error/msg=='Exception from createWeight for SolrFeature [name=matchedTitle, params={q={!terms f=title}${user_query}}] SolrFeatureWeight requires efi parameter that was not passed in request.'");

  // Adding efi in features section should make it work
  query.remove("fl");
  query.add("fl", "score,fvalias:[fv store=fstore3 efi.myOcc=2.3]");
  assertJQ("/query" + query.toQueryString(), "/response/docs/[0]/fvalias=='"+docs0fv_csv+"'");

  // Adding efi in transformer + rq should still returns features
  query.remove("fl");
  query.add("fl", "score,fvalias:[fv store=fstore3 efi.myOcc=2.3]");
  query.add("rq", "{!ltr reRankDocs=10 model=externalmodel efi.user_query=w3}");
  assertJQ("/query" + query.toQueryString(), "/response/docs/[0]/fvalias=='"+docs0fv_csv+"'");
}
 
Example 5
Source File: TestNoMatchSolrFeature.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Test
public void tesOnlyNoMatchFeaturesInStoreAndModelReturnsZeroScore() throws Exception {
  // Tests model with all no matching features
  final SolrQuery query = new SolrQuery();
  query.setQuery("*:*");
  query.add("fl", "*, score,fv:[fv]");
  query.add("rows", "4");
  query.add("fv", "true");
  query.add("rq", "{!ltr model=nomatchmodel3 reRankDocs=4}");

  final String docs0fv_dense_csv = FeatureLoggerTestUtils.toFeatureVector(
      "nomatchfeature4","0.0");
  final String docs0fv_sparse_csv = FeatureLoggerTestUtils.toFeatureVector();

  final String docs0fv_default_csv = chooseDefaultFeatureVector(docs0fv_dense_csv, docs0fv_sparse_csv);

  assertJQ("/query" + query.toQueryString(), "/response/docs/[0]/score==0.0");
  assertJQ("/query" + query.toQueryString(), "/response/docs/[0]/fv=='"+docs0fv_default_csv+"'");
}
 
Example 6
Source File: TestDefaultWrapperModel.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Test
public void testLoadNestedWrapperModel() throws Exception {
  String otherWrapperModelJson = getDefaultWrapperModelInJson("otherNestedWrapper",
      new String[0],
      "{\"resource\":\"" + baseModelFile.getName() + "\"}");
  File otherWrapperModelFile = new File(tmpConfDir, "nestedWrapperModel.json");
  try (BufferedWriter writer = new BufferedWriter(
      new OutputStreamWriter(new FileOutputStream(otherWrapperModelFile), StandardCharsets.UTF_8))) {
    writer.write(otherWrapperModelJson);
  }

  String wrapperModelJson = getDefaultWrapperModelInJson("nestedWrapper",
      new String[0],
      "{\"resource\":\"" + otherWrapperModelFile.getName() + "\"}");
  assertJPut(ManagedModelStore.REST_END_POINT, wrapperModelJson, "/responseHeader/status==0");
  final SolrQuery query = new SolrQuery();
  query.setQuery("{!func}pow(popularity,2)");
  query.add("rows", "3");
  query.add("fl", "*,score");
  query.add("rq", "{!ltr reRankDocs=3 model=nestedWrapper}");
  assertJQ("/query" + query.toQueryString(),
           "/response/docs/[0]/id==\"3\"", "/response/docs/[0]/score==2.0",
           "/response/docs/[1]/id==\"4\"", "/response/docs/[1]/score==1.0",
           "/response/docs/[2]/id==\"5\"", "/response/docs/[2]/score==0.0");
}
 
Example 7
Source File: TestRemoteStreaming.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/** Do a select query with the stream.url. Solr should fail */
@Test
public void testNoUrlAccess() throws Exception {
  SolrQuery query = new SolrQuery();
  query.setQuery( "*:*" );//for anything
  query.add("stream.url",makeDeleteAllUrl());
  SolrException se = expectThrows(SolrException.class, () -> getSolrClient().query(query));
  assertSame(ErrorCode.BAD_REQUEST, ErrorCode.getErrorCode(se.code()));
}
 
Example 8
Source File: TestLTRQParserPlugin.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Test
public void ltrMoreResultsThanReRankedTest() throws Exception {
  final String solrQuery = "_query_:{!edismax qf='title' mm=100% v='bloomberg' tie=0.1}";
  final SolrQuery query = new SolrQuery();
  query.setQuery(solrQuery);
  query.add("fl", "*, score");
  query.add("rows", "4");
  query.add("fv", "true");

  // FIXME: design better way to test this, we cannot check an absolute score
  // String nonRerankedScore = "0.09271725";

  // Normal solr order
  assertJQ("/query" + query.toQueryString(),
      "/response/docs/[0]/id=='9'",
      "/response/docs/[1]/id=='8'",
      "/response/docs/[2]/id=='7'",
      "/response/docs/[3]/id=='6'"
  //  "/response/docs/[3]/score=="+nonRerankedScore
  );

  query.add("rq", "{!ltr model=6029760550880411648 reRankDocs=3}");

  // Different order for top 3 reranked, but last one is the same top nonreranked doc
  assertJQ("/query" + query.toQueryString(),
      "/response/docs/[0]/id=='7'",
      "/response/docs/[1]/id=='8'",
      "/response/docs/[2]/id=='9'",
      "/response/docs/[3]/id=='6'"
  //  "/response/docs/[3]/score=="+nonRerankedScore
  );
}
 
Example 9
Source File: TestParallelWeightCreation.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Test
public void testLTRScoringQueryParallelWeightCreationResultOrder() throws Exception {
  setuptest("solrconfig-ltr_Th10_10.xml", "schema.xml");

  assertU(adoc("id", "1", "title", "w1 w3", "description", "w1", "popularity", "1"));
  assertU(adoc("id", "2", "title", "w2",    "description", "w2", "popularity", "2"));
  assertU(adoc("id", "3", "title", "w3",    "description", "w3", "popularity", "3"));
  assertU(adoc("id", "4", "title", "w3 w3", "description", "w4", "popularity", "4"));
  assertU(adoc("id", "5", "title", "w5",    "description", "w5", "popularity", "5"));
  assertU(commit());

  loadFeatures("external_features.json");
  loadModels("external_model.json");

  // check to make sure that the order of results will be the same when using parallel weight creation
  final SolrQuery query = new SolrQuery();
  query.setQuery("*:*");
  query.add("fl", "*,score");
  query.add("rows", "4");

  query.add("rq", "{!ltr reRankDocs=10 model=externalmodel efi.user_query=w3}");
  // SOLR-10710, feature based on query with term w3 now scores higher on doc 4, updated
  assertJQ("/query" + query.toQueryString(), "/response/docs/[0]/id=='4'");
  assertJQ("/query" + query.toQueryString(), "/response/docs/[1]/id=='3'");
  assertJQ("/query" + query.toQueryString(), "/response/docs/[2]/id=='1'");
  aftertest();
}
 
Example 10
Source File: SolrController.java    From Spring-Boot-Book with Apache License 2.0 5 votes vote down vote up
@RequestMapping("/queryAllOne")
   public String queryAllOne() throws IOException, SolrServerException {
/*  //第一种方式
       Map<String, String> queryParamMap = new HashMap<String, String>();
       queryParamMap.put("q", "*:*");
       //queryParamMap.put("f1", "id,name");
       queryParamMap.put("f1", "id:88");
       queryParamMap.put("sort", "id asc");
       MapSolrParams mapSolrParams = new MapSolrParams(queryParamMap);
       solrClient.query(mapSolrParams);

       for (Map.Entry<String, String[]> mapSolrParam : mapSolrParams) {

           System.out.println("solrDocument==============" + mapSolrParam);
       }

       return mapSolrParams;*/

       SolrQuery query = new SolrQuery();

       // 给query设置一个主查询条件:关键词
       query.setQuery("*:*");
       query.add("q", "name:然");

       QueryResponse response = solrClient.query(query);

       SolrDocumentList docs = response.getResults();

       long numFound = docs.getNumFound();

       System.out.println("总共查询到的文档数量: " + numFound);

       return docs.toString();

   }
 
Example 11
Source File: TestExternalFeatures.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Test
public void featureExtraction_valueFeatureRequiredInFq_shouldThrowException() throws Exception {
  final String userTitlePhrase1 = "userTitlePhrase1";
  final String userTitlePhrase2 = "userTitlePhrase2";
  final String userTitlePhrasePresent = (random().nextBoolean() ? userTitlePhrase1 : userTitlePhrase2);

  final SolrQuery query = new SolrQuery();
  query.setQuery("*:*");
  query.add("rows", "1");
  query.add("fl", "score,features:[fv efi.user_query=uq "+userTitlePhrasePresent+"=utpp]");
  assertJQ("/query" + query.toQueryString(), "/error/msg=='Exception from createWeight for "
      + "SolrFeature [name=titlePhrasesMatch, params={fq=[{!field f=title}${"+userTitlePhrase1+"}, {!field f=title}${"+userTitlePhrase2+"}]}] "
      + "SolrFeatureWeight requires efi parameter that was not passed in request.'");
}
 
Example 12
Source File: TestLTRQParserPlugin.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Test
public void ltrNoResultsTest() throws Exception {
  final SolrQuery query = new SolrQuery();
  query.setQuery("title:bloomberg23");
  query.add("fl", "*,[fv]");
  query.add("rows", "3");
  query.add("debugQuery", "on");
  query.add("rq", "{!ltr reRankDocs=3 model=6029760550880411648}");
  assertJQ("/query" + query.toQueryString(), "/response/numFound/==0");
}
 
Example 13
Source File: TestMultipleAdditiveTreesModel.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private void doTestMultipleAdditiveTreesScoringWithAndWithoutEfiFeatureMatches() throws Exception {

    final SolrQuery query = new SolrQuery();
    query.setQuery("*:*");
    query.add("rows", "3");
    query.add("fl", "*,score");

    // Regular scores
    assertJQ("/query" + query.toQueryString(), "/response/docs/[0]/score==1.0");
    assertJQ("/query" + query.toQueryString(), "/response/docs/[1]/score==1.0");
    assertJQ("/query" + query.toQueryString(), "/response/docs/[2]/score==1.0");

    // No match scores since user_query not passed in to external feature info
    // and feature depended on it.
    query.add("rq", "{!ltr reRankDocs=3 model=multipleadditivetreesmodel efi.user_query=dsjkafljjk}");

    assertJQ("/query" + query.toQueryString(), "/response/docs/[0]/score==-120.0");
    assertJQ("/query" + query.toQueryString(), "/response/docs/[1]/score==-120.0");
    assertJQ("/query" + query.toQueryString(), "/response/docs/[2]/score==-120.0");

    // Matched user query since it was passed in
    query.remove("rq");
    query.add("rq", "{!ltr reRankDocs=3 model=multipleadditivetreesmodel efi.user_query=w3}");

    assertJQ("/query" + query.toQueryString(), "/response/docs/[0]/id=='3'");
    assertJQ("/query" + query.toQueryString(), "/response/docs/[0]/score==30.0");
    assertJQ("/query" + query.toQueryString(), "/response/docs/[1]/score==-120.0");
    assertJQ("/query" + query.toQueryString(), "/response/docs/[2]/score==-120.0");
  }
 
Example 14
Source File: TestFeatureLogging.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Test
public void testGeneratedGroup() throws Exception {
  loadFeature("c1", ValueFeature.class.getName(), "testgroup",
      "{\"value\":1.0}");
  loadFeature("c2", ValueFeature.class.getName(), "testgroup",
      "{\"value\":2.0}");
  loadFeature("c3", ValueFeature.class.getName(), "testgroup",
      "{\"value\":3.0}");
  loadFeature("pop", FieldValueFeature.class.getName(), "testgroup",
      "{\"field\":\"popularity\"}");

  loadModel("sumgroup", LinearModel.class.getName(), new String[] {
      "c1", "c2", "c3"}, "testgroup",
      "{\"weights\":{\"c1\":1.0,\"c2\":1.0,\"c3\":1.0}}");

  final SolrQuery query = new SolrQuery();
  query.setQuery("title:bloomberg");
  query.add("fl", "*,[fv]");
  query.add("debugQuery", "on");

  query.remove("fl");
  query.add("fl", "fv:[fv]");
  query.add("rows", "3");
  query.add("group", "true");
  query.add("group.field", "title");

  query.add("rq", "{!ltr reRankDocs=3 model=sumgroup}");

  final String docs0fv_csv = FeatureLoggerTestUtils.toFeatureVector(
      "c1","1.0",
      "c2","2.0",
      "c3","3.0",
      "pop","5.0");

  restTestHarness.query("/query" + query.toQueryString());
  assertJQ(
      "/query" + query.toQueryString(),
      "/grouped/title/groups/[0]/doclist/docs/[0]/=={'fv':'"+docs0fv_csv+"'}");
}
 
Example 15
Source File: CustomTermsComponentTest.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
private static String findAndCheckTerms(final MiniSolrCloudCluster cluster, final String collection,
  String requestHandlerName, String in_shards,
  String field1, String value1,
  String field2, String value2,
  String solrServerExceptionMessagePrefix) throws IOException {

  // compose the query ...
  final SolrQuery solrQuery =  new SolrQuery("*:*");
  solrQuery.setRequestHandler(requestHandlerName);
  solrQuery.add("shards.qt", requestHandlerName);
  // ... asking for terms ...
  solrQuery.setTerms(true);
  if (field1 != null) {
    solrQuery.addTermsField(field1);
  }
  if (field2 != null) {
    solrQuery.addTermsField(field2);
  }
  // ... and shards info ...
  solrQuery.add("shards.info", "true");
  // ... passing shards to use (if we have a preference)
  if (in_shards != null) {
    solrQuery.add("shards", in_shards);
  }

  // make the query
  final QueryResponse queryResponse;
  try {
    queryResponse = new QueryRequest(solrQuery)
        .process(cluster.getSolrClient(), collection);
    assertNull("expected exception ("+solrServerExceptionMessagePrefix+") not encountered", solrServerExceptionMessagePrefix);
  } catch (SolrServerException sse) {
    assertNotNull("unexpectedly caught exception "+sse, solrServerExceptionMessagePrefix);
    assertTrue(sse.getMessage().startsWith(solrServerExceptionMessagePrefix));
    assertThat(sse.getCause().getMessage(), containsString("not on the shards whitelist"));
    return null;
  }

  // analyse the response ...
  final TermsResponse termsResponse = queryResponse.getTermsResponse();
  // ... checking the terms returned ...
  checkTermsResponse(termsResponse, field1, value1);
  checkTermsResponse(termsResponse, field2, value2);
  // ... and assemble info about the shards ...
  final String out_shards = extractShardAddresses(queryResponse, ",");
  // ... to return to the caller
  return out_shards;
}
 
Example 16
Source File: CloudHttp2SolrClientTest.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("deprecation")
private void queryWithShardsPreferenceRules(CloudHttp2SolrClient cloudClient,
                                        boolean useShardsPreference,
                                        String collectionName)
    throws Exception
{
  SolrQuery qRequest = new SolrQuery("*:*");

  ModifiableSolrParams qParams = new ModifiableSolrParams();
  if (useShardsPreference) {
    qParams.add(ShardParams.SHARDS_PREFERENCE, ShardParams.SHARDS_PREFERENCE_REPLICA_LOCATION + ":" + ShardParams.REPLICA_LOCAL);
  } else {
    qParams.add(CommonParams.PREFER_LOCAL_SHARDS, "true");
  }
  qParams.add(ShardParams.SHARDS_INFO, "true");
  qRequest.add(qParams);

  // CloudSolrClient sends the request to some node.
  // And since all the nodes are hosting cores from all shards, the
  // distributed query formed by this node will select cores from the
  // local shards only
  QueryResponse qResponse = cloudClient.query(collectionName, qRequest);

  Object shardsInfo = qResponse.getResponse().get(ShardParams.SHARDS_INFO);
  assertNotNull("Unable to obtain "+ShardParams.SHARDS_INFO, shardsInfo);

  // Iterate over shards-info and check what cores responded
  SimpleOrderedMap<?> shardsInfoMap = (SimpleOrderedMap<?>)shardsInfo;
  @SuppressWarnings({"unchecked"})
  Iterator<Map.Entry<String, ?>> itr = shardsInfoMap.asMap(100).entrySet().iterator();
  List<String> shardAddresses = new ArrayList<String>();
  while (itr.hasNext()) {
    Map.Entry<String, ?> e = itr.next();
    assertTrue("Did not find map-type value in "+ShardParams.SHARDS_INFO, e.getValue() instanceof Map);
    String shardAddress = (String)((Map)e.getValue()).get("shardAddress");
    assertNotNull(ShardParams.SHARDS_INFO+" did not return 'shardAddress' parameter", shardAddress);
    shardAddresses.add(shardAddress);
  }
  if (log.isInfoEnabled()) {
    log.info("Shards giving the response: {}", Arrays.toString(shardAddresses.toArray()));
  }

  // Make sure the distributed queries were directed to a single node only
  Set<Integer> ports = new HashSet<Integer>();
  for (String shardAddr: shardAddresses) {
    URL url = new URL (shardAddr);
    ports.add(url.getPort());
  }

  // This assertion would hold true as long as every shard has a core on each node
  assertTrue ("Response was not received from shards on a single node",
      shardAddresses.size() > 1 && ports.size()==1);
}
 
Example 17
Source File: CloudSolrClientTest.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
private void queryWithPreferReplicaTypes(CloudSolrClient cloudClient,
                                         String preferReplicaTypes,
                                         boolean preferLocalShards,
                                         String collectionName)
    throws Exception
{
  SolrQuery qRequest = new SolrQuery("*:*");
  ModifiableSolrParams qParams = new ModifiableSolrParams();

  final List<String> preferredTypes = Arrays.asList(preferReplicaTypes.split("\\|"));
  StringBuilder rule = new StringBuilder();
  preferredTypes.forEach(type -> {
    if (rule.length() != 0) {
      rule.append(',');
    }
    rule.append(ShardParams.SHARDS_PREFERENCE_REPLICA_TYPE);
    rule.append(':');
    rule.append(type);
  });
  if (preferLocalShards) {
    if (rule.length() != 0) {
      rule.append(',');
    }
    rule.append(ShardParams.SHARDS_PREFERENCE_REPLICA_LOCATION);
    rule.append(":local");
  }
  qParams.add(ShardParams.SHARDS_PREFERENCE, rule.toString());  
  qParams.add(ShardParams.SHARDS_INFO, "true");
  qRequest.add(qParams);

  // CloudSolrClient sends the request to some node.
  // And since all the nodes are hosting cores from all shards, the
  // distributed query formed by this node will select cores from the
  // local shards only
  QueryResponse qResponse = cloudClient.query(collectionName, qRequest);

  Object shardsInfo = qResponse.getResponse().get(ShardParams.SHARDS_INFO);
  assertNotNull("Unable to obtain "+ShardParams.SHARDS_INFO, shardsInfo);

  Map<String, String> replicaTypeMap = new HashMap<String, String>();
  DocCollection collection = getCollectionState(collectionName);
  for (Slice slice : collection.getSlices()) {
    for (Replica replica : slice.getReplicas()) {
      String coreUrl = replica.getCoreUrl();
      // It seems replica reports its core URL with a trailing slash while shard
      // info returned from the query doesn't. Oh well.
      if (coreUrl.endsWith("/")) {
        coreUrl = coreUrl.substring(0, coreUrl.length() - 1);
      }
      replicaTypeMap.put(coreUrl, replica.getType().toString());
    }
  }

  // Iterate over shards-info and check that replicas of correct type responded
  SimpleOrderedMap<?> shardsInfoMap = (SimpleOrderedMap<?>)shardsInfo;
  @SuppressWarnings({"unchecked"})
  Iterator<Map.Entry<String, ?>> itr = shardsInfoMap.asMap(100).entrySet().iterator();
  List<String> shardAddresses = new ArrayList<String>();
  while (itr.hasNext()) {
    Map.Entry<String, ?> e = itr.next();
    assertTrue("Did not find map-type value in "+ShardParams.SHARDS_INFO, e.getValue() instanceof Map);
    String shardAddress = (String)((Map)e.getValue()).get("shardAddress");
    assertNotNull(ShardParams.SHARDS_INFO+" did not return 'shardAddress' parameter", shardAddress);
    assertTrue(replicaTypeMap.containsKey(shardAddress));
    assertTrue(preferredTypes.indexOf(replicaTypeMap.get(shardAddress)) == 0);
    shardAddresses.add(shardAddress);
  }
  assertTrue("No responses", shardAddresses.size() > 0);
  if (log.isInfoEnabled()) {
    log.info("Shards giving the response: {}", Arrays.toString(shardAddresses.toArray()));
  }
}
 
Example 18
Source File: TestFeatureLogging.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Test
public void testGeneratedFeatures() throws Exception {
  loadFeature("c1", ValueFeature.class.getName(), "test1",
      "{\"value\":1.0}");
  loadFeature("c2", ValueFeature.class.getName(), "test1",
      "{\"value\":2.0}");
  loadFeature("c3", ValueFeature.class.getName(), "test1",
      "{\"value\":3.0}");
  loadFeature("pop", FieldValueFeature.class.getName(), "test1",
      "{\"field\":\"popularity\"}");
  loadFeature("nomatch", SolrFeature.class.getName(), "test1",
      "{\"q\":\"{!terms f=title}foobarbat\"}");
  loadFeature("yesmatch", SolrFeature.class.getName(), "test1",
      "{\"q\":\"{!terms f=popularity}2\"}");

  loadModel("sum1", LinearModel.class.getName(), new String[] {
      "c1", "c2", "c3"}, "test1",
      "{\"weights\":{\"c1\":1.0,\"c2\":1.0,\"c3\":1.0}}");

  final String docs0fv_dense_csv = FeatureLoggerTestUtils.toFeatureVector(
      "c1","1.0",
      "c2","2.0",
      "c3","3.0",
      "pop","2.0",
      "nomatch","0.0",
      "yesmatch","1.0");
  final String docs0fv_sparse_csv = FeatureLoggerTestUtils.toFeatureVector(
      "c1","1.0",
      "c2","2.0",
      "c3","3.0",
      "pop","2.0",
      "yesmatch","1.0");

  final String docs0fv_default_csv  = chooseDefaultFeatureVector(docs0fv_dense_csv, docs0fv_sparse_csv);

  final SolrQuery query = new SolrQuery();
  query.setQuery("title:bloomberg");
  query.add("fl", "title,description,id,popularity,[fv]");
  query.add("rows", "3");
  query.add("debugQuery", "on");
  query.add("rq", "{!ltr reRankDocs=3 model=sum1}");

  restTestHarness.query("/query" + query.toQueryString());
  assertJQ(
      "/query" + query.toQueryString(),
      "/response/docs/[0]/=={'title':'bloomberg bloomberg ', 'description':'bloomberg','id':'7', 'popularity':2,  '[fv]':'"+docs0fv_default_csv+"'}");

  query.remove("fl");
  query.add("fl", "[fv]");
  query.add("rows", "3");
  query.add("rq", "{!ltr reRankDocs=3 model=sum1}");

  restTestHarness.query("/query" + query.toQueryString());
  assertJQ("/query" + query.toQueryString(),
      "/response/docs/[0]/=={'[fv]':'"+docs0fv_default_csv+"'}");
}
 
Example 19
Source File: TestLTRWithSort.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Test
public void testRankingSolrSort() throws Exception {
  // before();
  loadFeature("powpularityS", SolrFeature.class.getName(),
      "{\"q\":\"{!func}pow(popularity,2)\"}");

  loadModel("powpularityS-model", LinearModel.class.getName(),
      new String[] {"powpularityS"}, "{\"weights\":{\"powpularityS\":1.0}}");

  final SolrQuery query = new SolrQuery();
  query.setQuery("title:a1");
  query.add("fl", "*, score");
  query.add("rows", "4");

  // Normal term match
  assertJQ("/query" + query.toQueryString(), "/response/numFound/==8");
  assertJQ("/query" + query.toQueryString(), "/response/docs/[0]/id=='1'");
  assertJQ("/query" + query.toQueryString(), "/response/docs/[1]/id=='2'");
  assertJQ("/query" + query.toQueryString(), "/response/docs/[2]/id=='3'");
  assertJQ("/query" + query.toQueryString(), "/response/docs/[3]/id=='4'");

  //Add sort
  query.add("sort", "description desc");
  assertJQ("/query" + query.toQueryString(), "/response/numFound/==8");
  assertJQ("/query" + query.toQueryString(), "/response/docs/[0]/id=='1'");
  assertJQ("/query" + query.toQueryString(), "/response/docs/[1]/id=='5'");
  assertJQ("/query" + query.toQueryString(), "/response/docs/[2]/id=='8'");
  assertJQ("/query" + query.toQueryString(), "/response/docs/[3]/id=='7'");

  query.add("rq", "{!ltr model=powpularityS-model reRankDocs=4}");
  query.set("debugQuery", "on");

  assertJQ("/query" + query.toQueryString(), "/response/numFound/==8");
  assertJQ("/query" + query.toQueryString(), "/response/docs/[0]/id=='8'");
  assertJQ("/query" + query.toQueryString(), "/response/docs/[0]/score==64.0");
  assertJQ("/query" + query.toQueryString(), "/response/docs/[1]/id=='7'");
  assertJQ("/query" + query.toQueryString(), "/response/docs/[1]/score==49.0");
  assertJQ("/query" + query.toQueryString(), "/response/docs/[2]/id=='5'");
  assertJQ("/query" + query.toQueryString(), "/response/docs/[2]/score==25.0");
  assertJQ("/query" + query.toQueryString(), "/response/docs/[3]/id=='1'");
  assertJQ("/query" + query.toQueryString(), "/response/docs/[3]/score==1.0");

}
 
Example 20
Source File: TestNoMatchSolrFeature.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Test
public void test1NoMatchFeatureReturnsFvWith1MatchingFeatureFromStoreAndDocWith0Score() throws Exception {
  // Tests model with all no matching features, but 1 feature store feature matching for extraction
  final SolrQuery query = new SolrQuery();
  query.setQuery("*:*");
  query.add("fl", "*, score,fv:[fv]");
  query.add("rows", "4");
  query.add("rq", "{!ltr model=nomatchmodel2 reRankDocs=4}");

  final SolrQuery yesMatchFeatureQuery = new SolrQuery();
  yesMatchFeatureQuery.setQuery("title:w1");
  yesMatchFeatureQuery.add("fl", "score");
  yesMatchFeatureQuery.add("rows", "4");
  String res = restTestHarness.query("/query"
      + yesMatchFeatureQuery.toQueryString());

  @SuppressWarnings({"unchecked"})
  final Map<String,Object> jsonParse = (Map<String,Object>) Utils
      .fromJSONString(res);
  @SuppressWarnings({"unchecked"})
  final Double doc0Score = (Double) ((Map<String,Object>) ((ArrayList<Object>) ((Map<String,Object>) jsonParse
      .get("response")).get("docs")).get(0)).get("score");

  final String docs0fv_dense_csv = FeatureLoggerTestUtils.toFeatureVector(
      "nomatchfeature","0.0",
      "yesmatchfeature",doc0Score.toString(),
      "nomatchfeature2","0.0",
      "nomatchfeature3","0.0");
  final String docs1fv_dense_csv = FeatureLoggerTestUtils.toFeatureVector(
      "nomatchfeature","0.0",
      "yesmatchfeature","0.0",
      "nomatchfeature2","0.0",
      "nomatchfeature3","0.0");
  final String docs2fv_dense_csv = FeatureLoggerTestUtils.toFeatureVector(
      "nomatchfeature","0.0",
      "yesmatchfeature","0.0",
      "nomatchfeature2","0.0",
      "nomatchfeature3","0.0");
  final String docs3fv_dense_csv = FeatureLoggerTestUtils.toFeatureVector(
      "nomatchfeature","0.0",
      "yesmatchfeature","0.0",
      "nomatchfeature2","0.0",
      "nomatchfeature3","0.0");

  final String docs0fv_sparse_csv = FeatureLoggerTestUtils.toFeatureVector(
      "yesmatchfeature",doc0Score.toString());
  final String docs1fv_sparse_csv = FeatureLoggerTestUtils.toFeatureVector();
  final String docs2fv_sparse_csv = FeatureLoggerTestUtils.toFeatureVector();
  final String docs3fv_sparse_csv = FeatureLoggerTestUtils.toFeatureVector();

  final String docs0fv_default_csv = chooseDefaultFeatureVector(docs0fv_dense_csv, docs0fv_sparse_csv);
  final String docs1fv_default_csv = chooseDefaultFeatureVector(docs1fv_dense_csv, docs1fv_sparse_csv);
  final String docs2fv_default_csv = chooseDefaultFeatureVector(docs2fv_dense_csv, docs2fv_sparse_csv);
  final String docs3fv_default_csv = chooseDefaultFeatureVector(docs3fv_dense_csv, docs3fv_sparse_csv);

  assertJQ("/query" + query.toQueryString(), "/response/docs/[0]/score==0.0");
  assertJQ("/query" + query.toQueryString(),
      "/response/docs/[0]/fv=='"+docs0fv_default_csv+"'");
  assertJQ("/query" + query.toQueryString(), "/response/docs/[1]/score==0.0");
  assertJQ("/query" + query.toQueryString(), "/response/docs/[1]/fv=='"+docs1fv_default_csv+"'");
  assertJQ("/query" + query.toQueryString(), "/response/docs/[2]/score==0.0");
  assertJQ("/query" + query.toQueryString(), "/response/docs/[2]/fv=='"+docs2fv_default_csv+"'");
  assertJQ("/query" + query.toQueryString(), "/response/docs/[3]/score==0.0");
  assertJQ("/query" + query.toQueryString(), "/response/docs/[3]/fv=='"+docs3fv_default_csv+"'");
}