Failed to load uid from the index in match_all query with parent/child and _source disabled, closes #1149.

This commit is contained in:
Shay Banon 2011-07-30 22:29:06 +03:00
parent 146a6d3730
commit ca7cd21519
10 changed files with 78 additions and 45 deletions

View File

@ -20,7 +20,6 @@
package org.elasticsearch.action.get;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.FieldSelector;
import org.apache.lucene.document.Fieldable;
import org.elasticsearch.ElasticSearchException;
import org.elasticsearch.action.ActionListener;
@ -34,6 +33,7 @@ import org.elasticsearch.cluster.routing.ShardIterator;
import org.elasticsearch.common.BytesHolder;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.logging.ESLogger;
import org.elasticsearch.common.lucene.document.ResetFieldSelector;
import org.elasticsearch.common.lucene.uid.UidField;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.index.engine.Engine;
@ -164,8 +164,9 @@ public class TransportGetAction extends TransportShardSingleOperationAction<GetR
Map<String, GetField> fields = null;
byte[] source = null;
UidField.DocIdAndVersion docIdAndVersion = get.docIdAndVersion();
FieldSelector fieldSelector = buildFieldSelectors(docMapper, gFields);
ResetFieldSelector fieldSelector = buildFieldSelectors(docMapper, gFields);
if (fieldSelector != null) {
fieldSelector.reset();
Document doc;
try {
doc = docIdAndVersion.reader.document(docIdAndVersion.docId, fieldSelector);
@ -319,7 +320,7 @@ public class TransportGetAction extends TransportShardSingleOperationAction<GetR
}
}
private static FieldSelector buildFieldSelectors(DocumentMapper docMapper, String... fields) {
private static ResetFieldSelector buildFieldSelectors(DocumentMapper docMapper, String... fields) {
if (fields == null) {
return docMapper.sourceMapper().fieldSelector();
}

View File

@ -0,0 +1,29 @@
/*
* Licensed to Elastic Search and Shay Banon under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. Elastic Search 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.elasticsearch.common.lucene.document;
import org.apache.lucene.document.FieldSelector;
/**
*/
public interface ResetFieldSelector extends FieldSelector {
void reset();
}

View File

@ -19,13 +19,12 @@
package org.elasticsearch.common.lucene.document;
import org.apache.lucene.document.FieldSelector;
import org.apache.lucene.document.FieldSelectorResult;
/**
* @author kimchy (shay.banon)
*/
public class SingleFieldSelector implements FieldSelector {
public class SingleFieldSelector implements ResetFieldSelector {
private String name;
@ -42,8 +41,11 @@ public class SingleFieldSelector implements FieldSelector {
@Override public FieldSelectorResult accept(String fieldName) {
if (name.equals(fieldName)) {
return FieldSelectorResult.LOAD;
return FieldSelectorResult.LOAD_AND_BREAK;
}
return FieldSelectorResult.NO_LOAD;
}
@Override public void reset() {
}
}

View File

@ -21,14 +21,13 @@ package org.elasticsearch.index.mapper.internal;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.FieldSelector;
import org.apache.lucene.document.FieldSelectorResult;
import org.apache.lucene.document.Fieldable;
import org.elasticsearch.ElasticSearchParseException;
import org.elasticsearch.common.compress.lzf.LZF;
import org.elasticsearch.common.compress.lzf.LZFDecoder;
import org.elasticsearch.common.compress.lzf.LZFEncoder;
import org.elasticsearch.common.lucene.Lucene;
import org.elasticsearch.common.lucene.document.ResetFieldSelector;
import org.elasticsearch.common.unit.ByteSizeValue;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.index.mapper.InternalMapper;
@ -97,8 +96,6 @@ public class SourceFieldMapper extends AbstractFieldMapper<byte[]> implements In
private long compressThreshold;
private final SourceFieldSelector fieldSelector;
public SourceFieldMapper() {
this(Defaults.NAME, Defaults.ENABLED, null, -1);
}
@ -109,15 +106,14 @@ public class SourceFieldMapper extends AbstractFieldMapper<byte[]> implements In
this.enabled = enabled;
this.compress = compress;
this.compressThreshold = compressThreshold;
this.fieldSelector = new SourceFieldSelector(names.indexName());
}
public boolean enabled() {
return this.enabled;
}
public FieldSelector fieldSelector() {
return this.fieldSelector;
public ResetFieldSelector fieldSelector() {
return SourceFieldSelector.INSTANCE;
}
@Override protected Field parseCreateField(ParseContext context) throws IOException {
@ -176,22 +172,6 @@ public class SourceFieldMapper extends AbstractFieldMapper<byte[]> implements In
return value;
}
private static class SourceFieldSelector implements FieldSelector {
private final String name;
private SourceFieldSelector(String name) {
this.name = name;
}
@Override public FieldSelectorResult accept(String fieldName) {
if (fieldName.equals(name)) {
return FieldSelectorResult.LOAD_AND_BREAK;
}
return FieldSelectorResult.NO_LOAD;
}
}
@Override protected String contentType() {
return CONTENT_TYPE;
}

View File

@ -19,15 +19,15 @@
package org.elasticsearch.index.mapper.internal;
import org.apache.lucene.document.FieldSelector;
import org.apache.lucene.document.FieldSelectorResult;
import org.elasticsearch.common.lucene.document.ResetFieldSelector;
/**
* An optimized field selector that loads just the uid.
*
* @author kimchy (shay.banon)
*/
public class SourceFieldSelector implements FieldSelector {
public class SourceFieldSelector implements ResetFieldSelector {
public static final SourceFieldSelector INSTANCE = new SourceFieldSelector();
@ -41,4 +41,7 @@ public class SourceFieldSelector implements FieldSelector {
}
return FieldSelectorResult.NO_LOAD;
}
@Override public void reset() {
}
}

View File

@ -19,8 +19,8 @@
package org.elasticsearch.index.mapper.selector;
import org.apache.lucene.document.FieldSelector;
import org.apache.lucene.document.FieldSelectorResult;
import org.elasticsearch.common.lucene.document.ResetFieldSelector;
import org.elasticsearch.index.mapper.internal.SourceFieldMapper;
/**
@ -28,7 +28,7 @@ import org.elasticsearch.index.mapper.internal.SourceFieldMapper;
*
* @author kimchy (shay.banon)
*/
public class AllButSourceFieldSelector implements FieldSelector {
public class AllButSourceFieldSelector implements ResetFieldSelector {
public static final AllButSourceFieldSelector INSTANCE = new AllButSourceFieldSelector();
@ -38,4 +38,7 @@ public class AllButSourceFieldSelector implements FieldSelector {
}
return FieldSelectorResult.LOAD;
}
@Override public void reset() {
}
}

View File

@ -19,8 +19,8 @@
package org.elasticsearch.index.mapper.selector;
import org.apache.lucene.document.FieldSelector;
import org.apache.lucene.document.FieldSelectorResult;
import org.elasticsearch.common.lucene.document.ResetFieldSelector;
import org.elasticsearch.index.mapper.FieldMapper;
import org.elasticsearch.index.mapper.FieldMappers;
@ -29,9 +29,11 @@ import java.util.HashSet;
/**
* @author kimchy (shay.banon)
*/
public class FieldMappersFieldSelector implements FieldSelector {
public class FieldMappersFieldSelector implements ResetFieldSelector {
private final HashSet<String> names = new HashSet<String>();
private int count;
public void add(String fieldName) {
names.add(fieldName);
@ -45,8 +47,15 @@ public class FieldMappersFieldSelector implements FieldSelector {
@Override public FieldSelectorResult accept(String fieldName) {
if (names.contains(fieldName)) {
if (++count == names.size()) {
return FieldSelectorResult.LOAD_AND_BREAK;
}
return FieldSelectorResult.LOAD;
}
return FieldSelectorResult.NO_LOAD;
}
@Override public void reset() {
count = 0;
}
}

View File

@ -19,8 +19,8 @@
package org.elasticsearch.index.mapper.selector;
import org.apache.lucene.document.FieldSelector;
import org.apache.lucene.document.FieldSelectorResult;
import org.elasticsearch.common.lucene.document.ResetFieldSelector;
import org.elasticsearch.index.mapper.internal.SourceFieldMapper;
import org.elasticsearch.index.mapper.internal.UidFieldMapper;
@ -29,25 +29,27 @@ import org.elasticsearch.index.mapper.internal.UidFieldMapper;
*
* @author kimchy (shay.banon)
*/
public class UidAndSourceFieldSelector implements FieldSelector {
public class UidAndSourceFieldSelector implements ResetFieldSelector {
private int match = 0;
@Override public FieldSelectorResult accept(String fieldName) {
if (UidFieldMapper.NAME.equals(fieldName)) {
if (++match == 2) {
match = 0;
return FieldSelectorResult.LOAD_AND_BREAK;
}
return FieldSelectorResult.LOAD;
}
if (SourceFieldMapper.NAME.equals(fieldName)) {
if (++match == 2) {
match = 0;
return FieldSelectorResult.LOAD_AND_BREAK;
}
return FieldSelectorResult.LOAD;
}
return FieldSelectorResult.NO_LOAD;
}
@Override public void reset() {
match = 0;
}
}

View File

@ -19,8 +19,8 @@
package org.elasticsearch.index.mapper.selector;
import org.apache.lucene.document.FieldSelector;
import org.apache.lucene.document.FieldSelectorResult;
import org.elasticsearch.common.lucene.document.ResetFieldSelector;
import org.elasticsearch.index.mapper.internal.UidFieldMapper;
/**
@ -28,7 +28,7 @@ import org.elasticsearch.index.mapper.internal.UidFieldMapper;
*
* @author kimchy (shay.banon)
*/
public class UidFieldSelector implements FieldSelector {
public class UidFieldSelector implements ResetFieldSelector {
public static final UidFieldSelector INSTANCE = new UidFieldSelector();
@ -42,4 +42,7 @@ public class UidFieldSelector implements FieldSelector {
}
return FieldSelectorResult.NO_LOAD;
}
@Override public void reset() {
}
}

View File

@ -20,11 +20,11 @@
package org.elasticsearch.search.fetch;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.FieldSelector;
import org.apache.lucene.document.Fieldable;
import org.apache.lucene.index.IndexReader;
import org.elasticsearch.common.collect.ImmutableMap;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.lucene.document.ResetFieldSelector;
import org.elasticsearch.index.Index;
import org.elasticsearch.index.mapper.DocumentMapper;
import org.elasticsearch.index.mapper.FieldMapper;
@ -81,7 +81,7 @@ public class FetchPhase implements SearchPhase {
}
public void execute(SearchContext context) {
FieldSelector fieldSelector = buildFieldSelectors(context);
ResetFieldSelector fieldSelector = buildFieldSelectors(context);
InternalSearchHit[] hits = new InternalSearchHit[context.docIdsToLoadSize()];
for (int index = 0; index < context.docIdsToLoadSize(); index++) {
@ -181,15 +181,16 @@ public class FetchPhase implements SearchPhase {
throw new FetchPhaseExecutionException(context, "Failed to load uid from the index, missing internal _uid field, current fields in the doc [" + fieldNames + "]");
}
private Document loadDocument(SearchContext context, FieldSelector fieldSelector, int docId) {
private Document loadDocument(SearchContext context, ResetFieldSelector fieldSelector, int docId) {
try {
fieldSelector.reset();
return context.searcher().doc(docId, fieldSelector);
} catch (IOException e) {
throw new FetchPhaseExecutionException(context, "Failed to fetch doc id [" + docId + "]", e);
}
}
private FieldSelector buildFieldSelectors(SearchContext context) {
private ResetFieldSelector buildFieldSelectors(SearchContext context) {
if (context.hasScriptFields() && !context.hasFieldNames()) {
// we ask for script fields, and no field names, don't load the source
return UidFieldSelector.INSTANCE;