use of org.apache.solr.client.solrj.SolrQuery in project lucene-solr by apache.
the class TestFieldValueFeature method testIfADocumentDoesntHaveAFieldASetDefaultValueIsReturned.
@Test
public void testIfADocumentDoesntHaveAFieldASetDefaultValueIsReturned() throws Exception {
final String fstore = "testIfADocumentDoesntHaveAFieldASetDefaultValueIsReturned";
loadFeature("popularity42", FieldValueFeature.class.getCanonicalName(), fstore, "{\"field\":\"popularity\",\"defaultValue\":\"42.0\"}");
SolrQuery query = new SolrQuery();
query.setQuery("id:42");
query.add("fl", "*, score");
query.add("rows", "4");
loadModel("popularity-model42", LinearModel.class.getCanonicalName(), new String[] { "popularity42" }, fstore, "{\"weights\":{\"popularity42\":1.0}}");
assertJQ("/query" + query.toQueryString(), "/response/numFound/==1");
assertJQ("/query" + query.toQueryString(), "/response/docs/[0]/id=='42'");
query = new SolrQuery();
query.setQuery("id:42");
query.add("rq", "{!ltr model=popularity-model42 reRankDocs=4}");
query.add("fl", "[fv]");
assertJQ("/query" + query.toQueryString(), "/response/numFound/==1");
assertJQ("/query" + query.toQueryString(), "/response/docs/[0]/=={'[fv]':'" + FeatureLoggerTestUtils.toFeatureVector("popularity42", "42.0") + "'}");
}
use of org.apache.solr.client.solrj.SolrQuery in project lucene-solr by apache.
the class TestFieldValueFeature method testThatIfaFieldDoesNotExistDefaultValueIsReturned.
@Test
public void testThatIfaFieldDoesNotExistDefaultValueIsReturned() throws Exception {
// using a different fstore to avoid a clash with the other tests
final String fstore = "testThatIfaFieldDoesNotExistDefaultValueIsReturned";
loadFeature("not-existing-field", FieldValueFeature.class.getCanonicalName(), fstore, "{\"field\":\"cowabunga\"}");
loadModel("not-existing-field-model", LinearModel.class.getCanonicalName(), new String[] { "not-existing-field" }, fstore, "{\"weights\":{\"not-existing-field\":1.0}}");
final SolrQuery query = new SolrQuery();
query.setQuery("id:42");
query.add("rq", "{!ltr model=not-existing-field-model reRankDocs=4}");
query.add("fl", "[fv]");
assertJQ("/query" + query.toQueryString(), "/response/numFound/==1");
assertJQ("/query" + query.toQueryString(), "/response/docs/[0]/=={'[fv]':'" + FeatureLoggerTestUtils.toFeatureVector("not-existing-field", Float.toString(FIELD_VALUE_FEATURE_DEFAULT_VAL)) + "'}");
}
use of org.apache.solr.client.solrj.SolrQuery in project lucene-solr by apache.
the class TestFieldValueFeature method testBooleanValue.
@Test
public void testBooleanValue() throws Exception {
final String fstore = "test_boolean_store";
loadFeature("trendy", FieldValueFeature.class.getCanonicalName(), fstore, "{\"field\":\"isTrendy\"}");
loadModel("trendy-model", LinearModel.class.getCanonicalName(), new String[] { "trendy" }, fstore, "{\"weights\":{\"trendy\":1.0}}");
SolrQuery query = new SolrQuery();
query.setQuery("id:4");
query.add("rq", "{!ltr model=trendy-model reRankDocs=4}");
query.add("fl", "[fv]");
assertJQ("/query" + query.toQueryString(), "/response/docs/[0]/=={'[fv]':'" + FeatureLoggerTestUtils.toFeatureVector("trendy", "0.0") + "'}");
query = new SolrQuery();
query.setQuery("id:5");
query.add("rq", "{!ltr model=trendy-model reRankDocs=4}");
query.add("fl", "[fv]");
assertJQ("/query" + query.toQueryString(), "/response/docs/[0]/=={'[fv]':'" + FeatureLoggerTestUtils.toFeatureVector("trendy", "1.0") + "'}");
// check default value is false
query = new SolrQuery();
query.setQuery("id:2");
query.add("rq", "{!ltr model=trendy-model reRankDocs=4}");
query.add("fl", "[fv]");
assertJQ("/query" + query.toQueryString(), "/response/docs/[0]/=={'[fv]':'" + FeatureLoggerTestUtils.toFeatureVector("trendy", "0.0") + "'}");
}
use of org.apache.solr.client.solrj.SolrQuery in project lucene-solr by apache.
the class TestFieldValueFeature method testIfADocumentDoesntHaveAFieldDefaultValueIsReturned.
@Test
public void testIfADocumentDoesntHaveAFieldDefaultValueIsReturned() throws Exception {
SolrQuery query = new SolrQuery();
query.setQuery("id:42");
query.add("fl", "*, score");
query.add("rows", "4");
assertJQ("/query" + query.toQueryString(), "/response/numFound/==1");
assertJQ("/query" + query.toQueryString(), "/response/docs/[0]/id=='42'");
query = new SolrQuery();
query.setQuery("id:42");
query.add("rq", "{!ltr model=popularity-model reRankDocs=4}");
query.add("fl", "[fv]");
assertJQ("/query" + query.toQueryString(), "/response/numFound/==1");
assertJQ("/query" + query.toQueryString(), "/response/docs/[0]/=={'[fv]':'" + FeatureLoggerTestUtils.toFeatureVector("popularity", Float.toString(FIELD_VALUE_FEATURE_DEFAULT_VAL)) + "'}");
}
use of org.apache.solr.client.solrj.SolrQuery in project lucene-solr by apache.
the class TestNoMatchSolrFeature method test2NoMatch1YesMatchFeatureReturnsFvWith1FeatureAndDocScoreScaledByModel.
@Test
public void test2NoMatch1YesMatchFeatureReturnsFvWith1FeatureAndDocScoreScaledByModel() throws Exception {
// Tests model with all no matching features but 1
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=nomatchmodel 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());
final Map<String, Object> jsonParse = (Map<String, Object>) ObjectBuilder.fromJSON(res);
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");
final String docs1fv_dense_csv = FeatureLoggerTestUtils.toFeatureVector("nomatchfeature", "0.0", "yesmatchfeature", "0.0", "nomatchfeature2", "0.0");
final String docs2fv_dense_csv = FeatureLoggerTestUtils.toFeatureVector("nomatchfeature", "0.0", "yesmatchfeature", "0.0", "nomatchfeature2", "0.0");
final String docs3fv_dense_csv = FeatureLoggerTestUtils.toFeatureVector("nomatchfeature", "0.0", "yesmatchfeature", "0.0", "nomatchfeature2", "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]/id=='1'");
assertJQ("/query" + query.toQueryString(), "/response/docs/[0]/score==" + (doc0Score * 1.1));
assertJQ("/query" + query.toQueryString(), "/response/docs/[0]/fv=='" + docs0fv_default_csv + "'");
assertJQ("/query" + query.toQueryString(), "/response/docs/[1]/id=='2'");
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]/id=='3'");
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]/id=='4'");
assertJQ("/query" + query.toQueryString(), "/response/docs/[3]/score==0.0");
assertJQ("/query" + query.toQueryString(), "/response/docs/[3]/fv=='" + docs3fv_default_csv + "'");
}
Aggregations