Search in sources :

Example 31 with SolrQuery

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") + "'}");
}
Also used : LinearModel(org.apache.solr.ltr.model.LinearModel) SolrQuery(org.apache.solr.client.solrj.SolrQuery) Test(org.junit.Test)

Example 32 with SolrQuery

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)) + "'}");
}
Also used : LinearModel(org.apache.solr.ltr.model.LinearModel) SolrQuery(org.apache.solr.client.solrj.SolrQuery) Test(org.junit.Test)

Example 33 with SolrQuery

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") + "'}");
}
Also used : LinearModel(org.apache.solr.ltr.model.LinearModel) SolrQuery(org.apache.solr.client.solrj.SolrQuery) Test(org.junit.Test)

Example 34 with SolrQuery

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)) + "'}");
}
Also used : SolrQuery(org.apache.solr.client.solrj.SolrQuery) Test(org.junit.Test)

Example 35 with SolrQuery

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 + "'");
}
Also used : ArrayList(java.util.ArrayList) Map(java.util.Map) SolrQuery(org.apache.solr.client.solrj.SolrQuery) Test(org.junit.Test)

Aggregations

SolrQuery (org.apache.solr.client.solrj.SolrQuery)327 Test (org.junit.Test)174 QueryResponse (org.apache.solr.client.solrj.response.QueryResponse)129 SolrServerException (org.apache.solr.client.solrj.SolrServerException)53 SolrInputDocument (org.apache.solr.common.SolrInputDocument)52 SolrDocument (org.apache.solr.common.SolrDocument)47 ArrayList (java.util.ArrayList)46 HttpSolrClient (org.apache.solr.client.solrj.impl.HttpSolrClient)45 IOException (java.io.IOException)36 CloudSolrClient (org.apache.solr.client.solrj.impl.CloudSolrClient)33 SolrDocumentList (org.apache.solr.common.SolrDocumentList)33 SolrClient (org.apache.solr.client.solrj.SolrClient)27 LinearModel (org.apache.solr.ltr.model.LinearModel)24 Map (java.util.Map)23 UpdateRequest (org.apache.solr.client.solrj.request.UpdateRequest)23 Replica (org.apache.solr.common.cloud.Replica)23 Slice (org.apache.solr.common.cloud.Slice)23 ModifiableSolrParams (org.apache.solr.common.params.ModifiableSolrParams)21 JettySolrRunner (org.apache.solr.client.solrj.embedded.JettySolrRunner)19 SolrException (org.apache.solr.common.SolrException)18