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

The following examples show how to use org.apache.solr.client.solrj.SolrQuery#remove() . 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: AbstractDateRangeFacetQueryConverter.java    From ambari-logsearch with Apache License 2.0 6 votes vote down vote up
@Override
public SolrQuery convert(SOURCE request) {
  SolrQuery solrQuery = new SolrQuery();
  String unit = StringUtils.defaultIfEmpty(request.getUnit(), "+1HOUR");
  solrQuery.setQuery("*:*");
  solrQuery.setFacet(true);
  solrQuery.addFacetPivotField("{!range=r1}" + getTypeFieldName());
  solrQuery.setFacetMinCount(1);
  solrQuery.setFacetLimit(-1);
  solrQuery.setFacetSort(LogSearchConstants.FACET_INDEX);
  solrQuery.add("facet.range", "{!tag=r1}" + getDateFieldName());
  solrQuery.add(String.format(Locale.ROOT, "f.%s.%s", getDateFieldName(), "facet.range.start"), request.getFrom());
  solrQuery.add(String.format(Locale.ROOT, "f.%s.%s", getDateFieldName(), "facet.range.end"), request.getTo());
  solrQuery.add(String.format(Locale.ROOT, "f.%s.%s", getDateFieldName(), "facet.range.gap"), unit);
  solrQuery.remove("sort");
  solrQuery.setRows(0);
  solrQuery.setStart(0);
  return solrQuery;
}
 
Example 2
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 3
Source File: TestExternalFeatures.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Test
public void featureExtraction_valueFeatureImplicitlyNotRequired_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 4
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 5
Source File: ServiceLogsManager.java    From ambari-logsearch with Apache License 2.0 5 votes vote down vote up
private Date getDocDateFromNextOrLastPage(ServiceLogRequest request, String keyword, boolean isNext, int currentPageNumber, int maxRows) {
  int lastOrFirstLogIndex;
  if (isNext) {
    lastOrFirstLogIndex = ((currentPageNumber + 1) * maxRows);
  } else {
    if (currentPageNumber == 0) {
      throw new NotFoundException("This is the first Page");
    }
    lastOrFirstLogIndex = (currentPageNumber * maxRows) - 1;
  }
  SimpleQuery sq = conversionService.convert(request, SimpleQuery.class);
  SolrQuery nextPageLogTimeQuery = new DefaultQueryParser().doConstructSolrQuery(sq);
  nextPageLogTimeQuery.remove("start");
  nextPageLogTimeQuery.remove("rows");
  nextPageLogTimeQuery.setStart(lastOrFirstLogIndex);
  nextPageLogTimeQuery.setRows(1);

  QueryResponse queryResponse = serviceLogsSolrDao.process(nextPageLogTimeQuery);
  if (queryResponse == null) {
    throw new MalformedInputException(String.format("Cannot process next page query for \"%s\" ", keyword));
  }
  SolrDocumentList docList = queryResponse.getResults();
  if (docList == null || docList.isEmpty()) {
    throw new MalformedInputException(String.format("Next page element for \"%s\" is not found", keyword));
  }

  SolrDocument solrDoc = docList.get(0);
  return (Date) solrDoc.get(LOGTIME);
}
 
Example 6
Source File: DistributedDebugComponentTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Test
// commented out on: 24-Dec-2018   @BadApple(bugUrl="https://issues.apache.org/jira/browse/SOLR-12028") // added 20-Sep-2018
public void testCompareWithNonDistributedRequest() throws SolrServerException, IOException {
  SolrQuery query = new SolrQuery();
  query.setQuery("id:1 OR id:2");
  query.setFilterQueries("id:[0 TO 10]", "id:[0 TO 5]");
  query.setRows(1);
  query.setSort("id", SolrQuery.ORDER.asc); // thus only return id:1 since rows 1
  query.set("debug",  "true");
  query.set("distrib", "true");
  query.setFields("id");
  if (random().nextBoolean()) { // can affect rb.onePassDistributedQuery
    query.addField("text");
  }
  query.set(ShardParams.DISTRIB_SINGLE_PASS, random().nextBoolean());
  query.set("shards", shard1 + "," + shard2);
  QueryResponse distribResponse = collection1.query(query);
  
  // same query but not distributed
  query.set("distrib", "false");
  query.remove("shards");
  QueryResponse nonDistribResponse = collection1.query(query);
  
  assertNotNull(distribResponse.getDebugMap().get("track"));
  assertNull(nonDistribResponse.getDebugMap().get("track"));
  assertEquals(distribResponse.getDebugMap().size() - 1, nonDistribResponse.getDebugMap().size());
  
  assertSectionEquals(distribResponse, nonDistribResponse, "explain");
  assertSectionEquals(distribResponse, nonDistribResponse, "rawquerystring");
  assertSectionEquals(distribResponse, nonDistribResponse, "querystring");
  assertSectionEquals(distribResponse, nonDistribResponse, "parsedquery");
  assertSectionEquals(distribResponse, nonDistribResponse, "parsedquery_toString");
  assertSectionEquals(distribResponse, nonDistribResponse, "QParser");
  assertSectionEquals(distribResponse, nonDistribResponse, "filter_queries");
  assertSectionEquals(distribResponse, nonDistribResponse, "parsed_filter_queries");
  
  // timing should have the same sections:
  assertSameKeys((NamedList<?>)nonDistribResponse.getDebugMap().get("timing"), (NamedList<?>)distribResponse.getDebugMap().get("timing"));
}
 
Example 7
Source File: SolrUtil.java    From ambari-logsearch with Apache License 2.0 5 votes vote down vote up
public static void setRowCount(SolrQuery solrQuery, int rows) {
  if (rows > 0) {
    solrQuery.setRows(rows);
  } else {
    solrQuery.setRows(0);
    solrQuery.remove(LogSearchConstants.SORT);
  }
}
 
Example 8
Source File: TestFieldValueFeature.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Test
public void testRanking() throws Exception {

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

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

  query.add("rq", "{!ltr model=popularity-model reRankDocs=4}");

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

  query.setQuery("*:*");
  query.remove("rows");
  query.add("rows", "8");
  query.remove("rq");
  query.add("rq", "{!ltr model=popularity-model reRankDocs=8}");

  assertJQ("/query" + query.toQueryString(), "/response/docs/[0]/id=='8'");
  assertJQ("/query" + query.toQueryString(), "/response/docs/[1]/id=='7'");
  assertJQ("/query" + query.toQueryString(), "/response/docs/[2]/id=='6'");
  assertJQ("/query" + query.toQueryString(), "/response/docs/[3]/id=='5'");
}
 
Example 9
Source File: TestLTRQParserExplain.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Test
public void LinearScoreExplainMissingEfiFeatureShouldReturnDefaultScore() throws Exception {
  loadFeatures("features-linear-efi.json");
  loadModels("linear-model-efi.json");

  SolrQuery query = new SolrQuery();
  query.setQuery("title:bloomberg");
  query.setParam("debugQuery", "on");
  query.add("rows", "4");
  query.add("rq", "{!ltr reRankDocs=4 model=linear-efi}");
  query.add("fl", "*,score");
  query.add("wt", "xml");

  final String linearModelEfiString = "LinearModel(name=linear-efi,featureWeights=["
    + "sampleConstant=1.0,"
    + "search_number_of_nights=2.0])";

  query.remove("wt");
  query.add("wt", "json");
  assertJQ(
      "/query" + query.toQueryString(),
      "/debug/explain/7=='\n5.0 = "+linearModelEfiString+" model applied to features, sum of:\n  5.0 = prod of:\n    1.0 = weight on feature\n    5.0 = ValueFeature [name=sampleConstant, params={value=5}]\n" +
          "  0.0 = prod of:\n" +
          "    2.0 = weight on feature\n" +
          "    0.0 = The feature has no value\n'}");
  assertJQ(
      "/query" + query.toQueryString(),
      "/debug/explain/9=='\n5.0 = "+linearModelEfiString+" model applied to features, sum of:\n  5.0 = prod of:\n    1.0 = weight on feature\n    5.0 = ValueFeature [name=sampleConstant, params={value=5}]\n" +
          "  0.0 = prod of:\n" +
          "    2.0 = weight on feature\n" +
          "    0.0 = The feature has no value\n'}");
}
 
Example 10
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 11
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 12
Source File: TestExternalFeatures.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Test
public void featureExtraction_valueFeatureRequired_shouldThrowException() throws Exception {
  final SolrQuery query = new SolrQuery();
  query.setQuery("*:*");
  query.add("rows", "1");

  // Using nondefault store should still result in error with no efi when it is required (myPop)
  query.remove("fl");
  query.add("fl", "fvalias:[fv store=fstore4]");
  assertJQ("/query" + query.toQueryString(), "/error/msg=='Exception from createWeight for ValueFeature [name=popularity, params={value=${myPop}, required=true}] ValueFeatureWeight requires efi parameter that was not passed in request.'");
}
 
Example 13
Source File: TestExternalFeatures.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Test
public void testEfiInTransformerShouldNotChangeOrderOfRerankedResults() throws Exception {
  final SolrQuery query = new SolrQuery();
  query.setQuery("*:*");
  query.add("fl", "*,score");
  query.add("rows", "3");

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

  query.remove("fl");
  query.add("fl", "*,score,[fv]");
  query.add("rq", "{!ltr reRankDocs=10 model=externalmodel efi.user_query=w3 efi.userTitlePhrase1=w4 efi.userTitlePhrase2=w5}");

  assertJQ("/query" + query.toQueryString(), "/response/docs/[0]/id=='3'");
  assertJQ("/query" + query.toQueryString(), "/response/docs/[0]/score==0.34972426");
  assertJQ("/query" + query.toQueryString(), "/response/docs/[1]/score==0.0");
  assertJQ("/query" + query.toQueryString(), "/response/docs/[2]/score==0.0");

  // Adding an efi in the transformer should not affect the rq ranking with a
  // different value for efi of the same parameter
  query.remove("fl");
  query.add("fl", "*,score,[fv efi.user_query=w2 efi.userTitlePhrase1=w4 efi.userTitlePhrase2=w5]");

  assertJQ("/query" + query.toQueryString(), "/response/docs/[0]/id=='3'");
  assertJQ("/query" + query.toQueryString(), "/response/docs/[0]/score==0.34972426");
  assertJQ("/query" + query.toQueryString(), "/response/docs/[1]/score==0.0");
  assertJQ("/query" + query.toQueryString(), "/response/docs/[2]/score==0.0");
}
 
Example 14
Source File: TestSelectiveWeightCreation.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Test
  public void testSelectiveWeightsRequestFeaturesFromDifferentStore() throws Exception {

//    final String docs0fv_sparse = FeatureLoggerTestUtils.toFeatureVector(
//        "matchedTitle","1.0", "titlePhraseMatch","0.6103343");
//    final String docs0fv_dense = FeatureLoggerTestUtils.toFeatureVector(
//        "matchedTitle","1.0", "titlePhraseMatch","0.6103343", "titlePhrasesMatch","0.0");
//    final String docs0fv_fstore4= FeatureLoggerTestUtils.toFeatureVector(
//        "popularity","3.0", "originalScore","1.0");
//
//    final String docs0fv = chooseDefaultFeatureVector(docs0fv_dense, docs0fv_sparse);

    // extract all features in externalmodel's store (default store)
    // rerank using externalmodel (default store)
    final SolrQuery query = new SolrQuery();
    query.setQuery("*:*");
    query.add("fl", "*,score,fv:[fv]");
    query.add("rows", "5");
    query.add("rq", "{!ltr reRankDocs=10 model=externalmodel efi.user_query=w3 efi.userTitlePhrase1=w2 efi.userTitlePhrase2=w1}");

    // 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'");
    // FIXME design better way to test this, we can't rely on absolute scores
    // assertJQ("/query" + query.toQueryString(), "/response/docs/[0]/fv=='"+docs0fv+"'");

    // extract all features from fstore4
    // rerank using externalmodel (default store)
    query.remove("fl");
    query.remove("rq");
    query.add("fl", "*,score,fv:[fv store=fstore4 efi.myPop=3]");
    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'");
    // FIXME design better way to test this, we can't rely on absolute scores
    // assertJQ("/query" + query.toQueryString(), "/response/docs/[0]/fv=='"+docs0fv_fstore4+"'");

    // extract all features from fstore4
    // rerank using externalmodel2 (fstore2)
    query.remove("fl");
    query.remove("rq");
    query.add("fl", "*,score,fv:[fv store=fstore4 efi.myPop=3]");
    query.add("rq", "{!ltr reRankDocs=10 model=externalmodel2 efi.user_query=w3}");
    
    assertJQ("/query" + query.toQueryString(), "/response/docs/[0]/id=='5'"); 
    assertJQ("/query" + query.toQueryString(), "/response/docs/[1]/id=='4'"); 
    assertJQ("/query" + query.toQueryString(), "/response/docs/[2]/id=='3'");
    // FIXME design better way to test this, we can't rely on absolute scores
    // assertJQ("/query" + query.toQueryString(), "/response/docs/[0]/fv=='"+docs0fv_fstore4+"'");
  }
 
Example 15
Source File: TestFieldLengthFeature.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Test
public void testRanking() throws Exception {
  loadFeature("title-length", FieldLengthFeature.class.getName(),
      "{\"field\":\"title\"}");

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

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

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

  query.add("rq", "{!ltr model=title-model reRankDocs=4}");

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

  query.setQuery("*:*");
  query.remove("rows");
  query.add("rows", "8");
  query.remove("rq");
  query.add("rq", "{!ltr model=title-model reRankDocs=8}");

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

  loadFeature("description-length",
      FieldLengthFeature.class.getName(),
      "{\"field\":\"description\"}");
  loadModel("description-model", LinearModel.class.getName(),
      new String[] {"description-length"},
      "{\"weights\":{\"description-length\":1.0}}");
  query.setQuery("title:w1");
  query.remove("rq");
  query.remove("rows");
  query.add("rows", "4");
  query.add("rq", "{!ltr model=description-model reRankDocs=4}");

  assertJQ("/query" + query.toQueryString(), "/response/numFound/==4");
  assertJQ("/query" + query.toQueryString(), "/response/docs/[0]/id=='7'");
  assertJQ("/query" + query.toQueryString(), "/response/docs/[1]/id=='8'");
  assertJQ("/query" + query.toQueryString(), "/response/docs/[2]/id=='6'");
  assertJQ("/query" + query.toQueryString(), "/response/docs/[3]/id=='1'");
}
 
Example 16
Source File: TestOriginalScoreFeature.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
public static void implTestOriginalScoreResponseDocsCheck(String modelName,
    String origScoreFeatureName,
    String nonScoringFeatureName, String nonScoringFeatureValue) throws Exception {

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

  final int doc0Id = 1;
  final int doc1Id = 8;
  final int doc2Id = 6;
  final int doc3Id = 7;

  assertJQ("/query" + query.toQueryString(), "/response/numFound/==4");
  assertJQ("/query" + query.toQueryString(), "/response/docs/[0]/id=='"+doc0Id+"'");
  assertJQ("/query" + query.toQueryString(), "/response/docs/[1]/id=='"+doc1Id+"'");
  assertJQ("/query" + query.toQueryString(), "/response/docs/[2]/id=='"+doc2Id+"'");
  assertJQ("/query" + query.toQueryString(), "/response/docs/[3]/id=='"+doc3Id+"'");

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

  @SuppressWarnings({"unchecked"})
  final String doc1Score = ((Double) ((Map<String,Object>) ((ArrayList<Object>) ((Map<String,Object>) jsonParse
      .get("response")).get("docs")).get(1)).get("score")).toString();
  @SuppressWarnings({"unchecked"})
  final String doc2Score = ((Double) ((Map<String,Object>) ((ArrayList<Object>) ((Map<String,Object>) jsonParse
      .get("response")).get("docs")).get(2)).get("score")).toString();
  @SuppressWarnings({"unchecked"})
  final String doc3Score = ((Double) ((Map<String,Object>) ((ArrayList<Object>) ((Map<String,Object>) jsonParse
      .get("response")).get("docs")).get(3)).get("score")).toString();

  final boolean debugQuery = random().nextBoolean();
  if (debugQuery) {
    query.add(CommonParams.DEBUG_QUERY, "true");
  }

  query.remove("fl");
  query.add("fl", "*, score, fv:[fv]");
  query.add("rq", "{!ltr model="+modelName+" reRankDocs=4}");

  assertJQ("/query" + query.toQueryString(), "/response/numFound/==4");
  assertJQ("/query" + query.toQueryString(), "/response/docs/[0]/id=='"+doc0Id+"'");
  assertJQ("/query" + query.toQueryString(), "/response/docs/[1]/id=='"+doc1Id+"'");
  assertJQ("/query" + query.toQueryString(), "/response/docs/[2]/id=='"+doc2Id+"'");
  assertJQ("/query" + query.toQueryString(), "/response/docs/[3]/id=='"+doc3Id+"'");

  implTestOriginalScoreResponseDocsCheck(modelName, query, 0, doc0Id, origScoreFeatureName, doc0Score,
      nonScoringFeatureName, nonScoringFeatureValue, debugQuery);
  implTestOriginalScoreResponseDocsCheck(modelName, query, 1, doc1Id, origScoreFeatureName, doc1Score,
      nonScoringFeatureName, nonScoringFeatureValue, debugQuery);
  implTestOriginalScoreResponseDocsCheck(modelName, query, 2, doc2Id, origScoreFeatureName, doc2Score,
      nonScoringFeatureName, nonScoringFeatureValue, debugQuery);
  implTestOriginalScoreResponseDocsCheck(modelName, query, 3, doc3Id, origScoreFeatureName, doc3Score,
      nonScoringFeatureName, nonScoringFeatureValue, debugQuery);
}
 
Example 17
Source File: DistributedDebugComponentTest.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
private void verifyDebugSections(SolrQuery query, SolrClient client) throws SolrServerException, IOException {
  query.set("debugQuery", "true");
  query.remove("debug");
  QueryResponse response = client.query(query);
  assertFalse(response.getDebugMap().isEmpty());
  assertInDebug(response, "track");
  assertInDebug(response, "rawquerystring");
  assertInDebug(response, "querystring");
  assertInDebug(response, "parsedquery");
  assertInDebug(response, "parsedquery_toString");
  assertInDebug(response, "QParser");
  assertInDebug(response, "explain");
  assertInDebug(response, "timing");
  
  query.set("debug", "true");
  query.remove("debugQuery");
  response = client.query(query);
  assertFalse(response.getDebugMap().isEmpty());
  assertInDebug(response, "track");
  assertInDebug(response, "rawquerystring");
  assertInDebug(response, "querystring");
  assertInDebug(response, "parsedquery");
  assertInDebug(response, "parsedquery_toString");
  assertInDebug(response, "QParser");
  assertInDebug(response, "explain");
  assertInDebug(response, "timing");
  
  query.set("debug", "track");
  response = client.query(query);
  assertFalse(response.getDebugMap().isEmpty());
  assertInDebug(response, "track");
  assertNotInDebug(response, "rawquerystring");
  assertNotInDebug(response, "querystring");
  assertNotInDebug(response, "parsedquery");
  assertNotInDebug(response, "parsedquery_toString");
  assertNotInDebug(response, "QParser");
  assertNotInDebug(response, "explain");
  assertNotInDebug(response, "timing");
  
  query.set("debug", "query");
  response = client.query(query);
  assertFalse(response.getDebugMap().isEmpty());
  assertNotInDebug(response, "track");
  assertInDebug(response, "rawquerystring");
  assertInDebug(response, "querystring");
  assertInDebug(response, "parsedquery");
  assertInDebug(response, "parsedquery_toString");
  assertInDebug(response, "QParser");
  assertNotInDebug(response, "explain");
  assertNotInDebug(response, "timing");
  
  query.set("debug", "results");
  response = client.query(query);
  assertFalse(response.getDebugMap().isEmpty());
  assertNotInDebug(response, "track");
  assertNotInDebug(response, "rawquerystring");
  assertNotInDebug(response, "querystring");
  assertNotInDebug(response, "parsedquery");
  assertNotInDebug(response, "parsedquery_toString");
  assertNotInDebug(response, "QParser");
  assertInDebug(response, "explain");
  assertNotInDebug(response, "timing");
  
  query.set("debug", "timing");
  response = client.query(query);
  assertFalse(response.getDebugMap().isEmpty());
  assertNotInDebug(response, "track");
  assertNotInDebug(response, "rawquerystring");
  assertNotInDebug(response, "querystring");
  assertNotInDebug(response, "parsedquery");
  assertNotInDebug(response, "parsedquery_toString");
  assertNotInDebug(response, "QParser");
  assertNotInDebug(response, "explain");
  assertInDebug(response, "timing");
  
  query.set("debug", "false");
  response = client.query(query);
  assertNull(response.getDebugMap());
}
 
Example 18
Source File: TestFeatureLogging.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Test
public void testSparseDenseFeatures() throws Exception {
  loadFeature("match", SolrFeature.class.getName(), "test4",
      "{\"q\":\"{!terms f=title}different\"}");
  loadFeature("c4", ValueFeature.class.getName(), "test4",
      "{\"value\":1.0}");

  loadModel("sum4", LinearModel.class.getName(), new String[] {
      "match"}, "test4",
      "{\"weights\":{\"match\":1.0}}");

  final String docs0fv_sparse_csv = FeatureLoggerTestUtils.toFeatureVector("match", "1.0", "c4", "1.0");
  final String docs1fv_sparse_csv = FeatureLoggerTestUtils.toFeatureVector(                "c4", "1.0");

  final String docs0fv_dense_csv  = FeatureLoggerTestUtils.toFeatureVector("match", "1.0", "c4", "1.0");
  final String docs1fv_dense_csv  = FeatureLoggerTestUtils.toFeatureVector("match", "0.0", "c4", "1.0");

  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 SolrQuery query = new SolrQuery();
  query.setQuery("title:bloomberg");
  query.add("rows", "10");
  query.add("rq", "{!ltr reRankDocs=10 model=sum4}");

  //csv - no feature format specified i.e. use default
  query.remove("fl");
  query.add("fl", "*,score,fv:[fv store=test4]");
  assertJQ(
      "/query" + query.toQueryString(),
      "/response/docs/[0]/fv/=='"+docs0fv_default_csv+"'");
  assertJQ(
      "/query" + query.toQueryString(),
      "/response/docs/[1]/fv/=='"+docs1fv_default_csv+"'");

  //csv - sparse feature format check
  query.remove("fl");
  query.add("fl", "*,score,fv:[fv store=test4 format=sparse]");
  assertJQ(
      "/query" + query.toQueryString(),
      "/response/docs/[0]/fv/=='"+docs0fv_sparse_csv+"'");
  assertJQ(
      "/query" + query.toQueryString(),
      "/response/docs/[1]/fv/=='"+docs1fv_sparse_csv+"'");

  //csv - dense feature format check
  query.remove("fl");
  query.add("fl", "*,score,fv:[fv store=test4 format=dense]");
  assertJQ(
      "/query" + query.toQueryString(),
      "/response/docs/[0]/fv/=='"+docs0fv_dense_csv+"'");
  assertJQ(
      "/query" + query.toQueryString(),
      "/response/docs/[1]/fv/=='"+docs1fv_dense_csv+"'");
}
 
Example 19
Source File: TestFeatureLogging.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Test
public void testDefaultStoreFeatureExtraction() throws Exception {
  loadFeature("defaultf1", ValueFeature.class.getName(),
      FeatureStore.DEFAULT_FEATURE_STORE_NAME,
      "{\"value\":1.0}");
  loadFeature("store8f1", ValueFeature.class.getName(),
      "store8",
      "{\"value\":2.0}");
  loadFeature("store9f1", ValueFeature.class.getName(),
      "store9",
      "{\"value\":3.0}");
  loadModel("store9m1", LinearModel.class.getName(),
    new String[] {"store9f1"},
    "store9",
    "{\"weights\":{\"store9f1\":1.0}}");

  final SolrQuery query = new SolrQuery();
  query.setQuery("id:7");
  query.add("rows", "1");

  // No store specified, use default store for extraction
  query.add("fl", "fv:[fv]");
  assertJQ("/query" + query.toQueryString(),
      "/response/docs/[0]/=={'fv':'"+FeatureLoggerTestUtils.toFeatureVector("defaultf1","1.0")+"'}");

  // Store specified, use store for extraction
  query.remove("fl");
  query.add("fl", "fv:[fv store=store8]");
  assertJQ("/query" + query.toQueryString(),
      "/response/docs/[0]/=={'fv':'"+FeatureLoggerTestUtils.toFeatureVector("store8f1","2.0")+"'}");

  // Store specified + model specified, use store for extraction
  query.add("rq", "{!ltr reRankDocs=3 model=store9m1}");
  assertJQ("/query" + query.toQueryString(),
      "/response/docs/[0]/=={'fv':'"+FeatureLoggerTestUtils.toFeatureVector("store8f1","2.0")+"'}");

  // No store specified + model specified, use model store for extraction
  query.remove("fl");
  query.add("fl", "fv:[fv]");
  assertJQ("/query" + query.toQueryString(),
      "/response/docs/[0]/=={'fv':'"+FeatureLoggerTestUtils.toFeatureVector("store9f1","3.0")+"'}");
}
 
Example 20
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+"'}");
}