mirror of https://github.com/apache/lucene.git
SOLR-807 -- BinaryResponseWriter writes fieldType.toExternal if it is not a supported type, otherwise it writes fieldType.toObject. This fixes the bug with encoding/decoding UUIDField.
git-svn-id: https://svn.apache.org/repos/asf/lucene/solr/trunk@726502 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
25c1d17448
commit
de702ae19a
|
@ -164,6 +164,10 @@ Bug Fixes
|
|||
|
||||
16. SOLR-901: FastOutputStream ignores write(byte[]) call. (Noble Paul via shalin)
|
||||
|
||||
17. SOLR-807: BinaryResponseWriter writes fieldType.toExternal if it is not a supported type,
|
||||
otherwise it writes fieldType.toObject. This fixes the bug with encoding/decoding UUIDField.
|
||||
(koji, Noble Paul, shalin)
|
||||
|
||||
|
||||
Other Changes
|
||||
----------------------
|
||||
|
|
|
@ -16,38 +16,34 @@
|
|||
*/
|
||||
package org.apache.solr.request;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.io.Writer;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import org.apache.lucene.document.Document;
|
||||
import org.apache.lucene.document.Fieldable;
|
||||
import org.apache.solr.common.SolrDocument;
|
||||
import org.apache.solr.common.params.CommonParams;
|
||||
import org.apache.solr.common.util.NamedList;
|
||||
import org.apache.solr.common.util.NamedListCodec;
|
||||
import org.apache.solr.schema.FieldType;
|
||||
import org.apache.solr.schema.IndexSchema;
|
||||
import org.apache.solr.schema.*;
|
||||
import org.apache.solr.search.DocIterator;
|
||||
import org.apache.solr.search.DocList;
|
||||
import org.apache.solr.search.SolrIndexSearcher;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
|
||||
public class BinaryResponseWriter implements BinaryQueryResponseWriter {
|
||||
private static final Logger LOG = LoggerFactory.getLogger(BinaryResponseWriter.class);
|
||||
private static final Set<Class> KNOWN_TYPES = new HashSet<Class>();
|
||||
|
||||
public void write(OutputStream out, SolrQueryRequest req, SolrQueryResponse response) throws IOException {
|
||||
Resolver resolver = new Resolver(req, response.getReturnFields());
|
||||
Boolean omitHeader = req.getParams().getBool(CommonParams.OMIT_HEADER);
|
||||
if(omitHeader != null && omitHeader) response.getValues().remove("responseHeader");
|
||||
if (omitHeader != null && omitHeader) response.getValues().remove("responseHeader");
|
||||
NamedListCodec codec = new NamedListCodec(resolver);
|
||||
codec.marshal(response.getValues(), out);
|
||||
}
|
||||
|
@ -77,10 +73,10 @@ public class BinaryResponseWriter implements BinaryQueryResponseWriter {
|
|||
public Resolver(SolrQueryRequest req, Set<String> returnFields) {
|
||||
this.schema = req.getSchema();
|
||||
this.searcher = req.getSearcher();
|
||||
this.includeScore = returnFields!=null && returnFields.contains("score");
|
||||
this.includeScore = returnFields != null && returnFields.contains("score");
|
||||
|
||||
if (returnFields != null) {
|
||||
if (returnFields.size() == 0 || (returnFields.size() == 1 && includeScore) || returnFields.contains("*")) {
|
||||
if (returnFields.size() == 0 || (returnFields.size() == 1 && includeScore) || returnFields.contains("*")) {
|
||||
returnFields = null; // null means return all stored fields
|
||||
}
|
||||
}
|
||||
|
@ -94,7 +90,7 @@ public class BinaryResponseWriter implements BinaryQueryResponseWriter {
|
|||
}
|
||||
if (o instanceof SolrDocument) {
|
||||
SolrDocument solrDocument = (SolrDocument) o;
|
||||
codec.writeSolrDocument(solrDocument,returnFields);
|
||||
codec.writeSolrDocument(solrDocument, returnFields);
|
||||
return null;
|
||||
}
|
||||
if (o instanceof Document) {
|
||||
|
@ -107,8 +103,8 @@ public class BinaryResponseWriter implements BinaryQueryResponseWriter {
|
|||
public void writeDocList(DocList ids, NamedListCodec codec) throws IOException {
|
||||
codec.writeTag(NamedListCodec.SOLRDOCLST);
|
||||
List l = new ArrayList(3);
|
||||
l.add((long)ids.matches());
|
||||
l.add((long)ids.offset());
|
||||
l.add((long) ids.matches());
|
||||
l.add((long) ids.offset());
|
||||
Float maxScore = null;
|
||||
if (includeScore && ids.hasScores()) {
|
||||
maxScore = ids.maxScore();
|
||||
|
@ -137,21 +133,25 @@ public class BinaryResponseWriter implements BinaryQueryResponseWriter {
|
|||
|
||||
public SolrDocument getDoc(Document doc) {
|
||||
SolrDocument solrDoc = new SolrDocument();
|
||||
for (Fieldable f : (List<Fieldable>)doc.getFields()) {
|
||||
for (Fieldable f : (List<Fieldable>) doc.getFields()) {
|
||||
String fieldName = f.name();
|
||||
if (returnFields!=null && !returnFields.contains(fieldName)) continue;
|
||||
if (returnFields != null && !returnFields.contains(fieldName)) continue;
|
||||
FieldType ft = schema.getFieldTypeNoEx(fieldName);
|
||||
Object val;
|
||||
if (ft==null) { // handle fields not in the schema
|
||||
if (ft == null) { // handle fields not in the schema
|
||||
if (f.isBinary()) val = f.binaryValue();
|
||||
else val = f.stringValue();
|
||||
} else {
|
||||
try {
|
||||
val = useFieldObjects ? ft.toObject(f) : ft.toExternal(f);
|
||||
if (useFieldObjects && KNOWN_TYPES.contains(ft.getClass())) {
|
||||
val = ft.toObject(f);
|
||||
} else {
|
||||
val = ft.toExternal(f);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
// There is a chance of the underlying field not really matching the
|
||||
// actual field type . So ,it can throw exception
|
||||
LOG.warn("Error reading a field from document : "+solrDoc, e);
|
||||
LOG.warn("Error reading a field from document : " + solrDoc, e);
|
||||
//if it happens log it and continue
|
||||
continue;
|
||||
}
|
||||
|
@ -169,14 +169,14 @@ public class BinaryResponseWriter implements BinaryQueryResponseWriter {
|
|||
*
|
||||
* @param req
|
||||
* @param rsp
|
||||
* @return a response object equivalent to what you get from the XML/JSON/javabin parser. Documents
|
||||
* become SolrDocuments, DocList becomes SolrDocumentList etc.
|
||||
*
|
||||
* @return a response object equivalent to what you get from the XML/JSON/javabin parser. Documents become
|
||||
* SolrDocuments, DocList becomes SolrDocumentList etc.
|
||||
*
|
||||
* @since solr 1.4
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public static NamedList<Object> getParsedResponse( SolrQueryRequest req, SolrQueryResponse rsp )
|
||||
{
|
||||
public static NamedList<Object> getParsedResponse(SolrQueryRequest req, SolrQueryResponse rsp) {
|
||||
try {
|
||||
Resolver resolver = new Resolver(req, rsp.getReturnFields());
|
||||
|
||||
|
@ -184,11 +184,33 @@ public class BinaryResponseWriter implements BinaryQueryResponseWriter {
|
|||
NamedListCodec codec = new NamedListCodec(resolver);
|
||||
codec.marshal(rsp.getValues(), out);
|
||||
|
||||
InputStream in = new ByteArrayInputStream( out.toByteArray() );
|
||||
return codec.unmarshal( in );
|
||||
InputStream in = new ByteArrayInputStream(out.toByteArray());
|
||||
return codec.unmarshal(in);
|
||||
}
|
||||
catch( Exception ex ) {
|
||||
throw new RuntimeException( ex );
|
||||
catch (Exception ex) {
|
||||
throw new RuntimeException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
static {
|
||||
KNOWN_TYPES.add(BoolField.class);
|
||||
KNOWN_TYPES.add(BCDIntField.class);
|
||||
KNOWN_TYPES.add(BCDLongField.class);
|
||||
KNOWN_TYPES.add(BCDStrField.class);
|
||||
KNOWN_TYPES.add(ByteField.class);
|
||||
KNOWN_TYPES.add(DateField.class);
|
||||
KNOWN_TYPES.add(DoubleField.class);
|
||||
KNOWN_TYPES.add(FloatField.class);
|
||||
KNOWN_TYPES.add(ShortField.class);
|
||||
KNOWN_TYPES.add(IntField.class);
|
||||
KNOWN_TYPES.add(LongField.class);
|
||||
KNOWN_TYPES.add(SortableLongField.class);
|
||||
KNOWN_TYPES.add(SortableIntField.class);
|
||||
KNOWN_TYPES.add(SortableFloatField.class);
|
||||
KNOWN_TYPES.add(SortableDoubleField.class);
|
||||
KNOWN_TYPES.add(StrField.class);
|
||||
KNOWN_TYPES.add(TextField.class);
|
||||
// We do not add UUIDField because UUID object is not a supported type in NamedListCodec
|
||||
// and if we write UUIDField.toObject, we wouldn't know how to handle it in the client side
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,66 @@
|
|||
/**
|
||||
* 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.request;
|
||||
|
||||
import org.apache.solr.common.SolrDocument;
|
||||
import org.apache.solr.common.SolrDocumentList;
|
||||
import org.apache.solr.common.params.CommonParams;
|
||||
import org.apache.solr.common.util.NamedList;
|
||||
import org.apache.solr.common.util.NamedListCodec;
|
||||
import org.apache.solr.util.AbstractSolrTestCase;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* Test for BinaryResponseWriter
|
||||
*
|
||||
* @version $Id$
|
||||
* @since solr 1.4
|
||||
*/
|
||||
public class TestBinaryResponseWriter extends AbstractSolrTestCase {
|
||||
|
||||
public String getSchemaFile() {
|
||||
return "schema.xml";
|
||||
}
|
||||
|
||||
public String getSolrConfigFile() {
|
||||
return "solrconfig.xml";
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests known types implementation by asserting correct encoding/decoding of UUIDField
|
||||
*/
|
||||
public void testUUID() throws Exception {
|
||||
String s = UUID.randomUUID().toString().toLowerCase();
|
||||
assertU(adoc("id", "101", "uuid", s));
|
||||
assertU(commit());
|
||||
LocalSolrQueryRequest req = lrf.makeRequest("q", "*:*");
|
||||
SolrQueryResponse rsp = h.queryAndResponse(req.getParams().get(CommonParams.QT), req);
|
||||
BinaryQueryResponseWriter writer = (BinaryQueryResponseWriter) h.getCore().getQueryResponseWriter("javabin");
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
writer.write(baos, req, rsp);
|
||||
NamedList res = new NamedListCodec().unmarshal(new ByteArrayInputStream(baos.toByteArray()));
|
||||
SolrDocumentList docs = (SolrDocumentList) res.get("response");
|
||||
for (Object doc : docs) {
|
||||
SolrDocument document = (SolrDocument) doc;
|
||||
assertEquals("Returned object must be a string", "java.lang.String", document.getFieldValue("uuid").getClass().getName());
|
||||
assertEquals("Wrong UUID string returned", s, document.getFieldValue("uuid"));
|
||||
}
|
||||
}
|
||||
}
|
|
@ -311,11 +311,14 @@
|
|||
</analyzer>
|
||||
</fieldtype>
|
||||
|
||||
<fieldType name="uuid" class="solr.UUIDField" />
|
||||
|
||||
</types>
|
||||
|
||||
|
||||
<fields>
|
||||
<field name="id" type="integer" indexed="true" stored="true" multiValued="false" required="false"/>
|
||||
<field name="uuid" type="uuid" stored="true" />
|
||||
<field name="name" type="nametext" indexed="true" stored="true"/>
|
||||
<field name="text" type="text" indexed="true" stored="false"/>
|
||||
<field name="subject" type="text" indexed="true" stored="true"/>
|
||||
|
|
Loading…
Reference in New Issue