You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by ry...@apache.org on 2015/04/15 08:14:24 UTC

svn commit: r1673654 - in /lucene/dev/branches/branch_5x/solr/core/src/java/org/apache/solr/response: WriteableValue.java transform/RawValueTransformerFactory.java

Author: ryan
Date: Wed Apr 15 06:14:23 2015
New Revision: 1673654

URL: http://svn.apache.org/r1673654
Log:
SOLR-7376: adding missing fields

Added:
    lucene/dev/branches/branch_5x/solr/core/src/java/org/apache/solr/response/WriteableValue.java
    lucene/dev/branches/branch_5x/solr/core/src/java/org/apache/solr/response/transform/RawValueTransformerFactory.java   (with props)

Added: lucene/dev/branches/branch_5x/solr/core/src/java/org/apache/solr/response/WriteableValue.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_5x/solr/core/src/java/org/apache/solr/response/WriteableValue.java?rev=1673654&view=auto
==============================================================================
--- lucene/dev/branches/branch_5x/solr/core/src/java/org/apache/solr/response/WriteableValue.java (added)
+++ lucene/dev/branches/branch_5x/solr/core/src/java/org/apache/solr/response/WriteableValue.java Wed Apr 15 06:14:23 2015
@@ -0,0 +1,25 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.solr.response;
+
+import java.io.IOException;
+
+import org.apache.solr.common.util.JavaBinCodec.ObjectResolver;
+
+public abstract class WriteableValue implements ObjectResolver {
+  public abstract void write(String name, TextResponseWriter writer) throws IOException;
+}
\ No newline at end of file

Added: lucene/dev/branches/branch_5x/solr/core/src/java/org/apache/solr/response/transform/RawValueTransformerFactory.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_5x/solr/core/src/java/org/apache/solr/response/transform/RawValueTransformerFactory.java?rev=1673654&view=auto
==============================================================================
--- lucene/dev/branches/branch_5x/solr/core/src/java/org/apache/solr/response/transform/RawValueTransformerFactory.java (added)
+++ lucene/dev/branches/branch_5x/solr/core/src/java/org/apache/solr/response/transform/RawValueTransformerFactory.java Wed Apr 15 06:14:23 2015
@@ -0,0 +1,160 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.solr.response.transform;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collection;
+
+import org.apache.lucene.index.IndexableField;
+import org.apache.solr.common.SolrDocument;
+import org.apache.solr.common.params.CommonParams;
+import org.apache.solr.common.params.SolrParams;
+import org.apache.solr.common.util.JavaBinCodec;
+import org.apache.solr.common.util.JavaBinCodec.ObjectResolver;
+import org.apache.solr.common.util.NamedList;
+import org.apache.solr.request.SolrQueryRequest;
+import org.apache.solr.response.QueryResponseWriter;
+import org.apache.solr.response.TextResponseWriter;
+import org.apache.solr.response.WriteableValue;
+
+import com.google.common.base.Strings;
+
+/**
+ * @since solr 5.2
+ */
+public class RawValueTransformerFactory extends TransformerFactory
+{
+  String applyToWT = null;
+  
+  public RawValueTransformerFactory() {
+    
+  }
+
+  public RawValueTransformerFactory(String wt) {
+    this.applyToWT = wt;
+  }
+  
+  @Override
+  public void init(NamedList args) {
+    super.init(args);
+    if(defaultUserArgs!=null&&defaultUserArgs.startsWith("wt=")) {
+      applyToWT = defaultUserArgs.substring(3);
+    }
+  }
+  
+  @Override
+  public DocTransformer create(String display, SolrParams params, SolrQueryRequest req) {
+    String field = params.get("f");
+    if(Strings.isNullOrEmpty(field)) {
+      field = display;
+    }
+    // When a 'wt' is specified in the transformer, only apply it to the same wt
+    boolean apply = true;
+    if(applyToWT!=null) {
+      String qwt = req.getParams().get(CommonParams.WT);
+      if(qwt==null) {
+        QueryResponseWriter qw = req.getCore().getQueryResponseWriter(req);
+        QueryResponseWriter dw = req.getCore().getQueryResponseWriter(applyToWT);
+        if(qw!=dw) {
+          apply = false;
+        }
+      }
+      else {
+        apply = applyToWT.equals(qwt);
+      }
+    }
+
+    if(apply) {
+      return new RawTransformer( field, display );
+    }
+    
+    if(field.equals(display)) {
+      return null; // nothing
+    }
+    return new RenameFieldTransformer( field, display, false );
+  }
+
+  static class RawTransformer extends DocTransformer
+  {
+    final String field;
+    final String display;
+
+    public RawTransformer( String field, String display )
+    {
+      this.field = field;
+      this.display = display;
+    }
+
+    @Override
+    public String getName()
+    {
+      return display;
+    }
+
+    @Override
+    public void transform(SolrDocument doc, int docid) {
+      Object val = doc.remove(field);
+      if(val==null) {
+        return;
+      }
+      if(val instanceof Collection) {
+        Collection current = (Collection)val;
+        ArrayList<WriteableStringValue> vals = new ArrayList<RawValueTransformerFactory.WriteableStringValue>();
+        for(Object v : current) {
+          vals.add(new WriteableStringValue(v));
+        }
+        doc.setField(display, vals);
+      }
+      else {
+        doc.setField(display, new WriteableStringValue(val));
+      }
+    }
+  }
+  
+  public static class WriteableStringValue extends WriteableValue {
+    public final Object val;
+    
+    public WriteableStringValue(Object val) {
+      this.val = val;
+    }
+    
+    @Override
+    public void write(String name, TextResponseWriter writer) throws IOException {
+      String str = null;
+      if(val instanceof IndexableField) { // delays holding it in memory
+        str = ((IndexableField)val).stringValue();
+      }
+      else {
+        str = val.toString();
+      }
+      writer.getWriter().write(str);
+    }
+
+    @Override
+    public Object resolve(Object o, JavaBinCodec codec) throws IOException {
+      ObjectResolver orig = codec.getResolver();
+      if(orig != null) {
+        codec.writeVal(orig.resolve(val, codec));
+        return null;
+      }
+      return val.toString();
+    }
+  }
+}
+
+