org.apache.solr.search.SolrReturnFields Java Examples

The following examples show how to use org.apache.solr.search.SolrReturnFields. 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: UnifiedSolrHighlighter.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
/**
 * Retrieves the unique keys for the topdocs to key the results
 */
protected String[] getUniqueKeys(SolrIndexSearcher searcher, int[] docIDs) throws IOException {
  IndexSchema schema = searcher.getSchema();
  SchemaField keyField = schema.getUniqueKeyField();
  if (keyField != null) {
    SolrReturnFields returnFields = new SolrReturnFields(keyField.getName(), null);
    String[] uniqueKeys = new String[docIDs.length];
    for (int i = 0; i < docIDs.length; i++) {
      int docid = docIDs[i];
      SolrDocument solrDoc = searcher.getDocFetcher().solrDoc(docid, returnFields);
      uniqueKeys[i] = schema.printableUniqueKey(solrDoc);
    }
    return uniqueKeys;
  } else {
    return new String[docIDs.length];
  }
}
 
Example #2
Source File: AlfrescoFieldMapperTransformer.java    From SearchServices with GNU Lesser General Public License v3.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public void transform(SolrDocument doc, int docid, float score)
{
    Collection<String> fieldNames = new ArrayList<>(doc.getFieldNames());
    solrReturnFields = new SolrReturnFields(context.getRequest().getParams().get("originalFl"), context.getRequest());

    for (String fieldName : fieldNames)
    {
       SchemaField schemaField = context.getSearcher().getSchema().getFieldOrNull(fieldName);
       if(schemaField != null)
       {
           String alfrescoFieldName = AlfrescoSolrDataModel.getInstance().getAlfrescoPropertyFromSchemaField(fieldName);
           if (isRequestedField(alfrescoFieldName) || alfrescoFieldName.equals("id"))
           {
               Object value = doc.getFieldValue(fieldName);
               doc.removeFields(fieldName);
               if (schemaField.multiValued())
               {
                   Object collectionValue =
                           ((Collection<Object>) value).stream()
                                .map(elem -> getFieldValue(schemaField, elem))
                                .collect(Collectors.toSet());
                   doc.setField(alfrescoFieldName, collectionValue);
               }
               else
               {
                   doc.setField(transformToUnderscoreNotation(alfrescoFieldName), getFieldValue(schemaField, value));
               }
           }
           else
           {
               doc.removeFields(alfrescoFieldName);
               doc.removeFields(fieldName);
           }
       }
    }
}
 
Example #3
Source File: DocsStreamer.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public DocsStreamer(ResultContext rctx) {
  this.rctx = rctx;
  this.docs = rctx.getDocList();
  transformer = rctx.getReturnFields().getTransformer();
  docIterator = this.docs.iterator();
  docFetcher = rctx.getSearcher().getDocFetcher();
  solrReturnFields = (SolrReturnFields)rctx.getReturnFields();

  if (transformer != null) transformer.setContext(rctx);
}
 
Example #4
Source File: ChildDocTransformer.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
ChildDocTransformer(String name, BitSetProducer parentsFilter, DocSet childDocSet,
                    SolrReturnFields returnFields, boolean isNestedSchema, int limit) {
  this.name = name;
  this.parentsFilter = parentsFilter;
  this.childDocSet = childDocSet;
  this.limit = limit;
  this.isNestedSchema = isNestedSchema;
  this.childReturnFields = returnFields!=null? returnFields: new SolrReturnFields();
}
 
Example #5
Source File: XMLWriter.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/**
 * The SolrDocument should already have multivalued fields implemented as
 * Collections -- this will not rewrite to &lt;arr&gt;
 */
@Override
public void writeSolrDocument(String name, SolrDocument doc, ReturnFields returnFields, int idx ) throws IOException {
  startTag("doc", name, false);
  incLevel();

  for (String fname : doc.getFieldNames()) {
    if (returnFields!= null && !returnFields.wantsField(fname)) {
      continue;
    }

    Object val = doc.getFieldValue(fname);
    if( "_explain_".equals( fname ) ) {
      if (log.isDebugEnabled()) {
        log.debug(String.valueOf(val));
      }
    }
    writeVal(fname, val);
  }

  if(doc.hasChildDocuments()) {
    for(SolrDocument childDoc : doc.getChildDocuments()) {
      writeSolrDocument(null, childDoc, new SolrReturnFields(), idx);
    }
  }

  decLevel();
  writer.write("</doc>");
}
 
Example #6
Source File: LegacyFacet.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private void addStat(String val) {
  StrParser sp = new StrParser(val);
  int start = 0;
  sp.eatws();
  if (sp.pos >= sp.end) addStat(val, val);

  // try key:func() format
  String key = null;
  String funcStr = val;

  if (key == null) {
    key = SolrReturnFields.getFieldName(sp);
    if (key != null && sp.opt(':')) {
      // OK, we got the key
      funcStr = val.substring(sp.pos);
    } else {
      // an invalid key... it must not be present.
      sp.pos = start;
      key = null;
    }
  }

  if (key == null) {
    key = funcStr;  // not really ideal
  }

  addStat(key, funcStr);
}
 
Example #7
Source File: TestSolrQueryResponse.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Test
public void testReturnFields() throws Exception {
  final SolrQueryResponse response = new SolrQueryResponse();
  final ReturnFields defaultReturnFields = new SolrReturnFields();
  assertEquals("returnFields initial value", defaultReturnFields.toString(), response.getReturnFields().toString());
  final SolrReturnFields newValue = new SolrReturnFields((random().nextBoolean()
      ? SolrReturnFields.SCORE : "value"), null);
  response.setReturnFields(newValue);
  assertEquals("returnFields new value", newValue.toString(), response.getReturnFields().toString());
}
 
Example #8
Source File: TestBinaryResponseWriter.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
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();

}
 
Example #9
Source File: SecureRealTimeGetComponent.java    From incubator-sentry with Apache License 2.0 5 votes vote down vote up
@Override
public void prepare(ResponseBuilder rb) throws IOException {
  QueryDocAuthorizationComponent docComponent =
      (QueryDocAuthorizationComponent)rb.req.getCore().getSearchComponent("queryDocAuthorization");
  if (docComponent != null) {
    String userName = sentryInstance.getUserName(rb.req);
    String superUser = (System.getProperty("solr.authorization.superuser", "solr"));
    // security is never applied to the super user; for example, if solr internally is using
    // real time get for replica synchronization, we need to return all the documents.
    if (docComponent.getEnabled() && !superUser.equals(userName)) {
      Set<String> roles = sentryInstance.getRoles(userName);
      if (roles != null && roles.size() > 0) {
        SolrReturnFields savedReturnFields = (SolrReturnFields)rb.rsp.getReturnFields();
        if (savedReturnFields == null) {
          throw new SolrException(SolrException.ErrorCode.SERVER_ERROR,
              "Not able to authorize request because ReturnFields is invalid: " + savedReturnFields);
        }
        DocTransformer savedTransformer = savedReturnFields.getTransformer();
        Query filterQuery = docComponent.getFilterQuery(roles);
        if (filterQuery != null) {
          SolrReturnFields solrReturnFields = new AddDocIdReturnFields(rb.req, savedTransformer, filterQuery);
          rb.rsp.setReturnFields(solrReturnFields);
        } else {
          throw new SolrException(SolrException.ErrorCode.UNAUTHORIZED,
            "Request from user: " + userName +
            "rejected because filter query was unable to be generated");
        }
      } else {
        throw new SolrException(SolrException.ErrorCode.UNAUTHORIZED,
          "Request from user: " + userName +
          " rejected because user is not associated with any roles");
      }
    }
  } else {
    throw new SolrException(SolrException.ErrorCode.UNAUTHORIZED,
      "RealTimeGetRequest request " +
      " rejected because \"queryDocAuthorization\" component not defined");
  }
}
 
Example #10
Source File: RewriteFieldListComponent.java    From SearchServices with GNU Lesser General Public License v3.0 4 votes vote down vote up
private void transformFieldList(SolrQueryRequest req)
{
    Set<String> fieldListSet = new HashSet<>();

    Set<String> defaultNonCachedFields = Set.of("id","DBID", "_version_");
    Set<String> allowedNonCachedFields = new HashSet<>(defaultNonCachedFields);
    allowedNonCachedFields.add("score");

    SolrReturnFields solrReturnFields = new SolrReturnFields(req);
    String originalFieldList = req.getParams().get("fl");

    boolean cacheTransformer = ofNullable(solrReturnFields.getTransformer())
            .map(DocTransformer::getName)
            .map(name -> name.contains("fmap"))
            .orElse(false);

    ModifiableSolrParams params = new ModifiableSolrParams(req.getParams());


    // In case cache transformer is no set, we need to modify the field list in order return
    // only id, DBID and _version_ fields
    if (!cacheTransformer){
        if (!solrReturnFields.wantsAllFields())
        {
            fieldListSet.addAll(solrReturnFields.getLuceneFieldNames()
                    .stream()
                    .filter(allowedNonCachedFields::contains)
                    .collect(Collectors.toSet()));
        }

        if (fieldListSet.isEmpty())
        {
            fieldListSet.addAll(defaultNonCachedFields);
        }

        params.set("fl", String.join(",", fieldListSet));
    }
    else
    {
        if (solrReturnFields.wantsAllFields() || solrReturnFields.hasPatternMatching())
        {
            fieldListSet.add("*");
        }
        else
        {
            fieldListSet.addAll(solrReturnFields.getLuceneFieldNames().stream()
                    .map( field -> AlfrescoSolrDataModel.getInstance()
                                            .mapStoredProperty(field, req))
                    .filter(Objects::nonNull)
                    .map(schemaFieldName -> schemaFieldName.chars()
                            .mapToObj(c -> (char) c)
                            .map(c -> Character.isJavaIdentifierPart(c)? c : '?')
                            .map(Object::toString)
                            .collect(Collectors.joining()))
                    .collect(Collectors.toSet()));
        }

        params.add("fl", String.join(",", fieldListSet));
    }

    // This is added for filtering the fields in the cached transformer.
    params.set("originalFl", originalFieldList);
    req.setParams(params);
}
 
Example #11
Source File: RealTimeGetComponent.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Override
public void prepare(ResponseBuilder rb) throws IOException {
  // Set field flags
  ReturnFields returnFields = new SolrReturnFields( rb.req );
  rb.rsp.setReturnFields( returnFields );
}
 
Example #12
Source File: JSONWriterTest.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Test
public void testJSONSolrDocument() throws Exception {
  SolrQueryRequest req = req(CommonParams.WT,"json",
                             CommonParams.FL,"id,score,_children_,path");
  SolrQueryResponse rsp = new SolrQueryResponse();
  JSONResponseWriter w = new JSONResponseWriter();

  ReturnFields returnFields = new SolrReturnFields(req);
  rsp.setReturnFields(returnFields);

  StringWriter buf = new StringWriter();

  SolrDocument childDoc = new SolrDocument();
  childDoc.addField("id", "2");
  childDoc.addField("score", "0.4");
  childDoc.addField("path", Arrays.asList("a>b", "a>b>c"));

  SolrDocumentList childList = new SolrDocumentList();
  childList.setNumFound(1);
  childList.setStart(0);
  childList.add(childDoc);

  SolrDocument solrDoc = new SolrDocument();
  solrDoc.addField("id", "1");
  solrDoc.addField("subject", "hello2");
  solrDoc.addField("title", "hello3");
  solrDoc.addField("score", "0.7");
  solrDoc.setField("_children_", childList);

  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\"") && result.contains("_children_"));

  String expectedResult = "{'response':{'numFound':1,'start':0,'maxScore':0.7, 'numFoundExact':true,'docs':[{'id':'1', 'score':'0.7'," +
      " '_children_':{'numFound':1,'start':0,'numFoundExact':true,'docs':[{'id':'2', 'score':'0.4', 'path':['a>b', 'a>b>c']}] }}] }}";
  String error = JSONTestUtil.match(result, "=="+expectedResult);
  assertNull("response validation failed with error: " + error, error);

  req.close();
}
 
Example #13
Source File: DocsStreamer.java    From lucene-solr with Apache License 2.0 2 votes vote down vote up
/**
 * This method is less efficient then the 3 arg version because it may convert some fields that 
 * are not needed
 *
 * @deprecated use the 3 arg version for better performance
 * @see #convertLuceneDocToSolrDoc(Document,IndexSchema,ReturnFields)
 */
@Deprecated
public static SolrDocument convertLuceneDocToSolrDoc(Document doc, final IndexSchema schema) {
  return convertLuceneDocToSolrDoc(doc,schema, new SolrReturnFields());
}