-
Notifications
You must be signed in to change notification settings - Fork 25.3k
Support older postings formats #85303
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
354624f
Support old postings formats
ywelsch 56d0e7b
javadoc
ywelsch bbaa535
review comments
ywelsch 31d37af
Merge remote-tracking branch 'elastic/master' into old-postings
ywelsch eb30319
Merge remote-tracking branch 'elastic/master' into old-postings
ywelsch b8ffa8d
Merge remote-tracking branch 'elastic/master' into old-postings
ywelsch 0d40083
Verify / rewrite mappings using full analysis service
ywelsch b7dd421
allow queries on text field type
ywelsch 7f590ad
make analyzer lenient and updateable
ywelsch 8d03678
Merge remote-tracking branch 'elastic/master' into old-postings
ywelsch c33e835
fix tests
ywelsch ac09d02
fix test
ywelsch e99a176
test fixes
ywelsch c0e508f
use constant scoring
ywelsch df7a1f5
Revert "use constant scoring"
ywelsch 3d8e3d4
Revert "test fixes"
ywelsch c0f63da
Revert "fix test"
ywelsch b216511
Revert "fix tests"
ywelsch 66af9f3
Revert "make analyzer lenient and updateable"
ywelsch eb3d85f
Revert "allow queries on text field type"
ywelsch 45e7f4c
Revert "Verify / rewrite mappings using full analysis service"
ywelsch a0a9da6
undo postings test
ywelsch 806901c
more undo
ywelsch File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
217 changes: 217 additions & 0 deletions
217
.../java/org/elasticsearch/xpack/lucene/bwc/codecs/LegacyAdaptingPerFieldPostingsFormat.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,217 @@ | ||
/* | ||
* @notice | ||
* 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. | ||
* | ||
* Modifications copyright (C) 2021 Elasticsearch B.V. | ||
*/ | ||
package org.elasticsearch.xpack.lucene.bwc.codecs; | ||
|
||
import org.apache.lucene.codecs.FieldsConsumer; | ||
import org.apache.lucene.codecs.FieldsProducer; | ||
import org.apache.lucene.codecs.NormsProducer; | ||
import org.apache.lucene.codecs.PostingsFormat; | ||
import org.apache.lucene.codecs.perfield.PerFieldPostingsFormat; | ||
import org.apache.lucene.index.FieldInfo; | ||
import org.apache.lucene.index.Fields; | ||
import org.apache.lucene.index.IndexOptions; | ||
import org.apache.lucene.index.MergeState; | ||
import org.apache.lucene.index.SegmentReadState; | ||
import org.apache.lucene.index.SegmentWriteState; | ||
import org.apache.lucene.index.Terms; | ||
import org.elasticsearch.core.IOUtils; | ||
|
||
import java.io.Closeable; | ||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.IdentityHashMap; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.TreeMap; | ||
|
||
/** | ||
* Modified version of {@link PerFieldPostingsFormat} that allows swapping in | ||
* {@link org.elasticsearch.xpack.lucene.bwc.codecs.lucene50.BWCLucene50PostingsFormat} instead of | ||
* {@link org.apache.lucene.backward_codecs.lucene50.Lucene50PostingsFormat} when reading from older | ||
* codecs. The former has full support for older Lucene versions (going back to Lucene 5) while the | ||
* latter only supports Lucene 7 and above (as it was shipped with backwards-codecs of Lucene 9 that | ||
* only has support for N-2). | ||
* | ||
* This class can probably be removed once we are on Lucene 10 and Lucene50PostingsFormat is no longer | ||
* shipped as part of bwc jars. | ||
* | ||
* Swapping out formats can be done via the {@link #getPostingsFormat(String) method}. | ||
*/ | ||
public abstract class LegacyAdaptingPerFieldPostingsFormat extends PostingsFormat { | ||
/** Name of this {@link PostingsFormat}. */ | ||
public static final String PER_FIELD_NAME = "PerField40"; | ||
|
||
/** {@link FieldInfo} attribute name used to store the format name for each field. */ | ||
public static final String PER_FIELD_FORMAT_KEY = PerFieldPostingsFormat.class.getSimpleName() + ".format"; | ||
|
||
/** {@link FieldInfo} attribute name used to store the segment suffix name for each field. */ | ||
public static final String PER_FIELD_SUFFIX_KEY = PerFieldPostingsFormat.class.getSimpleName() + ".suffix"; | ||
|
||
/** Sole constructor. */ | ||
protected LegacyAdaptingPerFieldPostingsFormat() { | ||
super(PER_FIELD_NAME); | ||
} | ||
|
||
static String getSuffix(String formatName, String suffix) { | ||
return formatName + "_" + suffix; | ||
} | ||
|
||
protected PostingsFormat getPostingsFormat(String formatName) { | ||
throw new IllegalArgumentException(formatName); | ||
} | ||
|
||
private class FieldsWriter extends FieldsConsumer { | ||
final SegmentWriteState writeState; | ||
final List<Closeable> toClose = new ArrayList<Closeable>(); | ||
|
||
FieldsWriter(SegmentWriteState writeState) { | ||
this.writeState = writeState; | ||
} | ||
|
||
@Override | ||
public void write(Fields fields, NormsProducer norms) throws IOException { | ||
throw new IllegalStateException("This codec should only be used for reading, not writing"); | ||
} | ||
|
||
@Override | ||
public void merge(MergeState mergeState, NormsProducer norms) throws IOException { | ||
throw new IllegalStateException("This codec should only be used for reading, not writing"); | ||
} | ||
|
||
@Override | ||
public void close() throws IOException { | ||
IOUtils.close(toClose); | ||
} | ||
} | ||
|
||
private static class FieldsReader extends FieldsProducer { | ||
|
||
private final Map<String, FieldsProducer> fields = new TreeMap<>(); | ||
private final Map<String, FieldsProducer> formats = new HashMap<>(); | ||
private final String segment; | ||
|
||
// clone for merge | ||
FieldsReader(FieldsReader other) { | ||
Map<FieldsProducer, FieldsProducer> oldToNew = new IdentityHashMap<>(); | ||
// First clone all formats | ||
for (Map.Entry<String, FieldsProducer> ent : other.formats.entrySet()) { | ||
FieldsProducer values = ent.getValue().getMergeInstance(); | ||
formats.put(ent.getKey(), values); | ||
oldToNew.put(ent.getValue(), values); | ||
} | ||
|
||
// Then rebuild fields: | ||
for (Map.Entry<String, FieldsProducer> ent : other.fields.entrySet()) { | ||
FieldsProducer producer = oldToNew.get(ent.getValue()); | ||
assert producer != null; | ||
fields.put(ent.getKey(), producer); | ||
} | ||
|
||
segment = other.segment; | ||
} | ||
|
||
FieldsReader(final SegmentReadState readState, LegacyAdaptingPerFieldPostingsFormat legacyAdaptingPerFieldPostingsFormat) | ||
throws IOException { | ||
|
||
// Read _X.per and init each format: | ||
boolean success = false; | ||
try { | ||
// Read field name -> format name | ||
for (FieldInfo fi : readState.fieldInfos) { | ||
if (fi.getIndexOptions() != IndexOptions.NONE) { | ||
final String fieldName = fi.name; | ||
final String formatName = fi.getAttribute(PER_FIELD_FORMAT_KEY); | ||
if (formatName != null) { | ||
// null formatName means the field is in fieldInfos, but has no postings! | ||
final String suffix = fi.getAttribute(PER_FIELD_SUFFIX_KEY); | ||
if (suffix == null) { | ||
throw new IllegalStateException("missing attribute: " + PER_FIELD_SUFFIX_KEY + " for field: " + fieldName); | ||
} | ||
PostingsFormat format = legacyAdaptingPerFieldPostingsFormat.getPostingsFormat(formatName); | ||
String segmentSuffix = getSuffix(formatName, suffix); | ||
if (formats.containsKey(segmentSuffix) == false) { | ||
formats.put(segmentSuffix, format.fieldsProducer(new SegmentReadState(readState, segmentSuffix))); | ||
} | ||
fields.put(fieldName, formats.get(segmentSuffix)); | ||
} | ||
} | ||
} | ||
success = true; | ||
} finally { | ||
if (success == false) { | ||
IOUtils.closeWhileHandlingException(formats.values()); | ||
} | ||
} | ||
|
||
this.segment = readState.segmentInfo.name; | ||
} | ||
|
||
@Override | ||
public Iterator<String> iterator() { | ||
return Collections.unmodifiableSet(fields.keySet()).iterator(); | ||
} | ||
|
||
@Override | ||
public Terms terms(String field) throws IOException { | ||
FieldsProducer fieldsProducer = fields.get(field); | ||
return fieldsProducer == null ? null : fieldsProducer.terms(field); | ||
} | ||
|
||
@Override | ||
public int size() { | ||
return fields.size(); | ||
} | ||
|
||
@Override | ||
public void close() throws IOException { | ||
IOUtils.close(formats.values()); | ||
} | ||
|
||
@Override | ||
public void checkIntegrity() throws IOException { | ||
for (FieldsProducer producer : formats.values()) { | ||
producer.checkIntegrity(); | ||
} | ||
} | ||
|
||
@Override | ||
public FieldsProducer getMergeInstance() { | ||
return new FieldsReader(this); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "PerFieldPostings(segment=" + segment + " formats=" + formats.size() + ")"; | ||
} | ||
} | ||
|
||
@Override | ||
public final FieldsConsumer fieldsConsumer(SegmentWriteState state) throws IOException { | ||
return new FieldsWriter(state); | ||
} | ||
|
||
@Override | ||
public final FieldsProducer fieldsProducer(SegmentReadState state) throws IOException { | ||
return new FieldsReader(state, this); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we introduced this adapter because of line 125? I tried to remove this class by converting the
PerFieldPostingsFormat.format
attribute fromLucene50
toBWCLucene50
in FieldInfos, but it didn't work because of line 126. I think it's fine to have this class.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Another motivation for this class is that it keeps the information in these older indices as much "as-is" as possible, not requiring a conversion step before reading. Furthermore, future versions won't have to be aware that that conversion step happened in some prior versions of archive, if we were to remove it e.g. in the future. The nice bit is that this conversion can go away in ES 9 as we won't have a conflicting Lucene50PostingsFormat shipped anymore as part of bwc jars.