Search in sources :

Example 6 with SolrReturnFields

use of org.apache.solr.search.SolrReturnFields in project lucene-solr by apache.

the class TestBinaryResponseWriter method testResolverSolrDocumentPartialFields.

public void testResolverSolrDocumentPartialFields() throws Exception {
    LocalSolrQueryRequest req = lrf.makeRequest("q", "*:*", "fl", "id,xxx,ddd_s");
    SolrDocument in = new SolrDocument();
    in.addField("id", 345);
    in.addField("aaa_s", "aaa");
    in.addField("bbb_s", "bbb");
    in.addField("ccc_s", "ccc");
    in.addField("ddd_s", "ddd");
    in.addField("eee_s", "eee");
    Resolver r = new Resolver(req, new SolrReturnFields(req));
    Object o = r.resolve(in, new JavaBinCodec());
    assertNotNull("obj is null", o);
    assertTrue("obj is not doc", o instanceof SolrDocument);
    SolrDocument out = new SolrDocument();
    for (Map.Entry<String, Object> e : in) {
        if (r.isWritable(e.getKey()))
            out.put(e.getKey(), e.getValue());
    }
    assertTrue("id not found", out.getFieldNames().contains("id"));
    assertTrue("ddd_s not found", out.getFieldNames().contains("ddd_s"));
    assertEquals("Wrong number of fields found", 2, out.getFieldNames().size());
    req.close();
}
Also used : LocalSolrQueryRequest(org.apache.solr.request.LocalSolrQueryRequest) SolrDocument(org.apache.solr.common.SolrDocument) Resolver(org.apache.solr.response.BinaryResponseWriter.Resolver) Map(java.util.Map) SolrReturnFields(org.apache.solr.search.SolrReturnFields) JavaBinCodec(org.apache.solr.common.util.JavaBinCodec)

Example 7 with SolrReturnFields

use of org.apache.solr.search.SolrReturnFields in project lucene-solr by apache.

the class JSONWriterTest method testJSONSolrDocument.

@Test
public void testJSONSolrDocument() throws IOException {
    SolrQueryRequest req = req(CommonParams.WT, "json", CommonParams.FL, "id,score");
    SolrQueryResponse rsp = new SolrQueryResponse();
    JSONResponseWriter w = new JSONResponseWriter();
    ReturnFields returnFields = new SolrReturnFields(req);
    rsp.setReturnFields(returnFields);
    StringWriter buf = new StringWriter();
    SolrDocument solrDoc = new SolrDocument();
    solrDoc.addField("id", "1");
    solrDoc.addField("subject", "hello2");
    solrDoc.addField("title", "hello3");
    solrDoc.addField("score", "0.7");
    SolrDocumentList list = new SolrDocumentList();
    list.setNumFound(1);
    list.setStart(0);
    list.setMaxScore(0.7f);
    list.add(solrDoc);
    rsp.addResponse(list);
    w.write(buf, req, rsp);
    String result = buf.toString();
    assertFalse("response contains unexpected fields: " + result, result.contains("hello") || result.contains("\"subject\"") || result.contains("\"title\""));
    assertTrue("response doesn't contain expected fields: " + result, result.contains("\"id\"") && result.contains("\"score\""));
    req.close();
}
Also used : SolrQueryRequest(org.apache.solr.request.SolrQueryRequest) SolrDocument(org.apache.solr.common.SolrDocument) SolrReturnFields(org.apache.solr.search.SolrReturnFields) ReturnFields(org.apache.solr.search.ReturnFields) StringWriter(java.io.StringWriter) SolrDocumentList(org.apache.solr.common.SolrDocumentList) SolrReturnFields(org.apache.solr.search.SolrReturnFields) Test(org.junit.Test)

Example 8 with SolrReturnFields

use of org.apache.solr.search.SolrReturnFields in project lucene-solr by apache.

the class SmileWriterTest method testJSONSolrDocument.

@Test
public void testJSONSolrDocument() throws IOException {
    SolrQueryRequest req = req(CommonParams.WT, "json", CommonParams.FL, "id,score");
    SolrQueryResponse rsp = new SolrQueryResponse();
    SmileResponseWriter w = new SmileResponseWriter();
    ReturnFields returnFields = new SolrReturnFields(req);
    rsp.setReturnFields(returnFields);
    ByteArrayOutputStream buf = new ByteArrayOutputStream();
    SolrDocument solrDoc = new SolrDocument();
    solrDoc.addField("id", "1");
    solrDoc.addField("subject", "hello2");
    solrDoc.addField("title", "hello3");
    solrDoc.addField("score", "0.7");
    SolrDocumentList list = new SolrDocumentList();
    list.setNumFound(1);
    list.setStart(0);
    list.setMaxScore(0.7f);
    list.add(solrDoc);
    rsp.addResponse(list);
    w.write(buf, req, rsp);
    byte[] bytes = buf.toByteArray();
    Map m = (Map) decodeSmile(new ByteArrayInputStream(bytes));
    m = (Map) m.get("response");
    List l = (List) m.get("docs");
    Map doc = (Map) l.get(0);
    assertFalse(doc.containsKey("subject"));
    assertFalse(doc.containsKey("title"));
    assertTrue(doc.containsKey("id"));
    assertTrue(doc.containsKey("score"));
    req.close();
}
Also used : SolrQueryRequest(org.apache.solr.request.SolrQueryRequest) LocalSolrQueryRequest(org.apache.solr.request.LocalSolrQueryRequest) SolrDocument(org.apache.solr.common.SolrDocument) SolrReturnFields(org.apache.solr.search.SolrReturnFields) ReturnFields(org.apache.solr.search.ReturnFields) ByteArrayInputStream(java.io.ByteArrayInputStream) SolrDocumentList(org.apache.solr.common.SolrDocumentList) ArrayList(java.util.ArrayList) NamedList(org.apache.solr.common.util.NamedList) List(java.util.List) ByteArrayOutputStream(java.io.ByteArrayOutputStream) SolrDocumentList(org.apache.solr.common.SolrDocumentList) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) SolrReturnFields(org.apache.solr.search.SolrReturnFields) Test(org.junit.Test)

Example 9 with SolrReturnFields

use of org.apache.solr.search.SolrReturnFields in project lucene-solr by apache.

the class TestCSVResponseWriter method testCSVOutput.

@Test
public void testCSVOutput() throws Exception {
    // test our basic types,and that fields come back in the requested order
    assertEquals("id,foo_s,foo_i,foo_l,foo_b,foo_f,foo_d,foo_dt\n1,hi,-1,12345678987654321,false,1.414,-1.0E300,2000-01-02T03:04:05Z\n", h.query(req("q", "id:1", "wt", "csv", "fl", "id,foo_s,foo_i,foo_l,foo_b,foo_f,foo_d,foo_dt")));
    // test retrieving score, csv.header
    assertEquals("1,0.0,hi\n", h.query(req("q", "id:1^0", "wt", "csv", "csv.header", "false", "fl", "id,score,foo_s")));
    // test multivalued
    assertEquals("2,\"hi,there\"\n", h.query(req("q", "id:2", "wt", "csv", "csv.header", "false", "fl", "id,v_ss")));
    // test separator change
    assertEquals("2|\"hi|there\"\n", h.query(req("q", "id:2", "wt", "csv", "csv.header", "false", "csv.separator", "|", "fl", "id,v_ss")));
    // test mv separator change
    assertEquals("2,hi|there\n", h.query(req("q", "id:2", "wt", "csv", "csv.header", "false", "csv.mv.separator", "|", "fl", "id,v_ss")));
    // test mv separator change for a single field
    assertEquals("2,hi|there,nice:output\n", h.query(req("q", "id:2", "wt", "csv", "csv.header", "false", "csv.mv.separator", "|", "f.v2_ss.csv.separator", ":", "fl", "id,v_ss,v2_ss")));
    // test csv field for polyfield (currency) SOLR-3959
    assertEquals("4,\"1.50\\,EUR\"\n", h.query(req("q", "id:4", "wt", "csv", "csv.header", "false", "fl", "id,amount_c")));
    // test csv field for polyfield (latlon) SOLR-3959
    assertEquals("5,\"12.434\\,-134.1\"\n", h.query(req("q", "id:5", "wt", "csv", "csv.header", "false", "fl", "id,store")));
    // test retrieving fields from index
    String result = h.query(req("q", "*:*", "wt", "csv", "csv.header", "true", "fl", "*,score"));
    for (String field : "id,foo_s,foo_i,foo_l,foo_b,foo_f,foo_d,foo_dt,v_ss,v2_ss,score".split(",")) {
        assertTrue(result.indexOf(field) >= 0);
    }
    // test null values
    assertEquals("2,,hi|there\n", h.query(req("q", "id:2", "wt", "csv", "csv.header", "false", "csv.mv.separator", "|", "fl", "id,foo_s,v_ss")));
    // test alternate null value
    assertEquals("2,NULL,hi|there\n", h.query(req("q", "id:2", "wt", "csv", "csv.header", "false", "csv.mv.separator", "|", "csv.null", "NULL", "fl", "id,foo_s,v_ss")));
    // test alternate newline
    assertEquals("2,\"hi,there\"\r\n", h.query(req("q", "id:2", "wt", "csv", "csv.header", "false", "csv.newline", "\r\n", "fl", "id,v_ss")));
    // test alternate encapsulator
    assertEquals("2,'hi,there'\n", h.query(req("q", "id:2", "wt", "csv", "csv.header", "false", "csv.encapsulator", "'", "fl", "id,v_ss")));
    // test using escape instead of encapsulator
    assertEquals("2,hi\\,there\n", h.query(req("q", "id:2", "wt", "csv", "csv.header", "false", "csv.escape", "\\", "fl", "id,v_ss")));
    // test multiple lines
    assertEquals("1,,hi\n2,\"hi,there\",\n", h.query(req("q", "id:[1 TO 2]", "wt", "csv", "csv.header", "false", "fl", "id,v_ss,foo_s")));
    // test SOLR-2970 not returning non-stored fields by default. Compare sorted list
    assertEquals(sortHeader("amount_c,store,v_ss,foo_b,v2_ss,foo_f,foo_i,foo_d,foo_s,foo_dt,id,foo_l\n"), sortHeader(h.query(req("q", "id:3", "wt", "csv", "csv.header", "true", "fl", "*", "rows", "0"))));
    // now test SolrDocumentList
    SolrDocument d = new SolrDocument();
    SolrDocument d1 = d;
    d.addField("id", "1");
    d.addField("foo_i", -1);
    d.addField("foo_s", "hi");
    d.addField("foo_l", "12345678987654321L");
    d.addField("foo_b", false);
    d.addField("foo_f", 1.414f);
    d.addField("foo_d", -1.0E300);
    d.addField("foo_dt", new Date(Instant.parse("2000-01-02T03:04:05Z").toEpochMilli()));
    d.addField("score", "2.718");
    d = new SolrDocument();
    SolrDocument d2 = d;
    d.addField("id", "2");
    d.addField("v_ss", "hi");
    d.addField("v_ss", "there");
    d.addField("v2_ss", "nice");
    d.addField("v2_ss", "output");
    d.addField("score", "89.83");
    d.addField("shouldbeunstored", "foo");
    SolrDocumentList sdl = new SolrDocumentList();
    sdl.add(d1);
    sdl.add(d2);
    SolrQueryRequest req = req("q", "*:*");
    SolrQueryResponse rsp = new SolrQueryResponse();
    rsp.addResponse(sdl);
    QueryResponseWriter w = new CSVResponseWriter();
    rsp.setReturnFields(new SolrReturnFields("id,foo_s", req));
    StringWriter buf = new StringWriter();
    w.write(buf, req, rsp);
    assertEquals("id,foo_s\n1,hi\n2,\n", buf.toString());
    // try scores
    rsp.setReturnFields(new SolrReturnFields("id,score,foo_s", req));
    buf = new StringWriter();
    w.write(buf, req, rsp);
    assertEquals("id,score,foo_s\n1,2.718,hi\n2,89.83,\n", buf.toString());
    // get field values from docs... should be ordered and not include score unless requested
    rsp.setReturnFields(new SolrReturnFields("*", req));
    buf = new StringWriter();
    w.write(buf, req, rsp);
    assertEquals("id,foo_i,foo_s,foo_l,foo_b,foo_f,foo_d,foo_dt,v_ss,v2_ss\n" + "1,-1,hi,12345678987654321L,false,1.414,-1.0E300,2000-01-02T03:04:05Z,,\n" + "2,,,,,,,,\"hi,there\",\"nice,output\"\n", buf.toString());
    // get field values and scores - just check that the scores are there... we don't guarantee where
    rsp.setReturnFields(new SolrReturnFields("*,score", req));
    buf = new StringWriter();
    w.write(buf, req, rsp);
    String s = buf.toString();
    assertTrue(s.indexOf("score") >= 0 && s.indexOf("2.718") > 0 && s.indexOf("89.83") > 0);
    // Test field globs
    rsp.setReturnFields(new SolrReturnFields("id,foo*", req));
    buf = new StringWriter();
    w.write(buf, req, rsp);
    assertEquals("id,foo_i,foo_s,foo_l,foo_b,foo_f,foo_d,foo_dt\n" + "1,-1,hi,12345678987654321L,false,1.414,-1.0E300,2000-01-02T03:04:05Z\n" + "2,,,,,,,\n", buf.toString());
    rsp.setReturnFields(new SolrReturnFields("id,*_d*", req));
    buf = new StringWriter();
    w.write(buf, req, rsp);
    assertEquals("id,foo_d,foo_dt\n" + "1,-1.0E300,2000-01-02T03:04:05Z\n" + "2,,\n", buf.toString());
    // Test function queries
    rsp.setReturnFields(new SolrReturnFields("sum(1,1),id,exists(foo_i),div(9,1),foo_f", req));
    buf = new StringWriter();
    w.write(buf, req, rsp);
    assertEquals("\"sum(1,1)\",id,exists(foo_i),\"div(9,1)\",foo_f\n" + "\"\",1,,,1.414\n" + "\"\",2,,,\n", buf.toString());
    // Test transformers
    rsp.setReturnFields(new SolrReturnFields("mydocid:[docid],[explain]", req));
    buf = new StringWriter();
    w.write(buf, req, rsp);
    assertEquals("mydocid,[explain]\n" + "\"\",\n" + "\"\",\n", buf.toString());
    req.close();
}
Also used : SolrQueryRequest(org.apache.solr.request.SolrQueryRequest) SolrDocument(org.apache.solr.common.SolrDocument) StringWriter(java.io.StringWriter) SolrDocumentList(org.apache.solr.common.SolrDocumentList) Date(java.util.Date) SolrReturnFields(org.apache.solr.search.SolrReturnFields) Test(org.junit.Test)

Example 10 with SolrReturnFields

use of org.apache.solr.search.SolrReturnFields in project lucene-solr by apache.

the class FieldOptions method getFields.

/**
   * Helper method for determining the list of fields that we should 
   * try to find term vectors on.  
   * <p>
   * Does simple (non-glob-supporting) parsing on the 
   * {@link TermVectorParams#FIELDS} param if specified, otherwise it returns 
   * the concrete field values specified in {@link CommonParams#FL} -- 
   * ignoring functions, transformers, or literals.  
   * </p>
   * <p>
   * If "fl=*" is used, or neither param is specified, then <code>null</code> 
   * will be returned.  If the empty set is returned, it means the "fl" 
   * specified consisted entirely of things that are not real fields 
   * (ie: functions, transformers, partial-globs, score, etc...) and not 
   * supported by this component. 
   * </p>
   */
private Set<String> getFields(ResponseBuilder rb) {
    SolrParams params = rb.req.getParams();
    String[] fldLst = params.getParams(TermVectorParams.FIELDS);
    if (null == fldLst || 0 == fldLst.length || (1 == fldLst.length && 0 == fldLst[0].length())) {
        // no tv.fl, parse the main fl
        ReturnFields rf = new SolrReturnFields(params.getParams(CommonParams.FL), rb.req);
        if (rf.wantsAllFields()) {
            return null;
        }
        Set<String> fieldNames = rf.getLuceneFieldNames();
        return (null != fieldNames) ? fieldNames : // return empty set indicating no fields should be used
        Collections.<String>emptySet();
    }
    // otherwise us the raw fldList as is, no special parsing or globs
    Set<String> fieldNames = new LinkedHashSet<>();
    for (String fl : fldLst) {
        fieldNames.addAll(Arrays.asList(SolrPluginUtils.split(fl)));
    }
    return fieldNames;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) SolrReturnFields(org.apache.solr.search.SolrReturnFields) ReturnFields(org.apache.solr.search.ReturnFields) SolrParams(org.apache.solr.common.params.SolrParams) SolrReturnFields(org.apache.solr.search.SolrReturnFields)

Aggregations

SolrReturnFields (org.apache.solr.search.SolrReturnFields)13 ReturnFields (org.apache.solr.search.ReturnFields)8 SolrDocument (org.apache.solr.common.SolrDocument)7 SolrDocumentList (org.apache.solr.common.SolrDocumentList)5 SolrQueryRequest (org.apache.solr.request.SolrQueryRequest)5 Test (org.junit.Test)5 ArrayList (java.util.ArrayList)4 SolrParams (org.apache.solr.common.params.SolrParams)4 NamedList (org.apache.solr.common.util.NamedList)4 SolrException (org.apache.solr.common.SolrException)3 SolrQueryResponse (org.apache.solr.response.SolrQueryResponse)3 DocList (org.apache.solr.search.DocList)3 IOException (java.io.IOException)2 StringWriter (java.io.StringWriter)2 Date (java.util.Date)2 List (java.util.List)2 Map (java.util.Map)2 BooleanQuery (org.apache.lucene.search.BooleanQuery)2 Query (org.apache.lucene.search.Query)2 ModifiableSolrParams (org.apache.solr.common.params.ModifiableSolrParams)2