-
Notifications
You must be signed in to change notification settings - Fork 579
Faster indexing for learned sparse retrieval #2080
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
Open
thongnt99
wants to merge
5
commits into
castorini:master
Choose a base branch
from
thongnt99:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+246
−1
Open
Changes from 1 commit
Commits
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
79 changes: 79 additions & 0 deletions
79
src/main/java/io/anserini/collection/JsonTermWeightCollection.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,79 @@ | ||
| /* | ||
| * Anserini: A Lucene toolkit for reproducible information retrieval research | ||
| * | ||
| * Licensed 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 io.anserini.collection; | ||
|
|
||
| import com.fasterxml.jackson.databind.JsonNode; | ||
|
|
||
| import java.io.BufferedReader; | ||
| import java.io.IOException; | ||
| import java.nio.file.Path; | ||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
|
|
||
| /**xxw | ||
| * A JSON document collection where the user can specify directly the vector to be indexed. | ||
| */ | ||
| public class JsonTermWeightCollection extends DocumentCollection<JsonTermWeightCollection.Document> { | ||
| public JsonTermWeightCollection(Path path) { | ||
| this.path = path; | ||
| } | ||
|
|
||
| @Override | ||
| public FileSegment<JsonTermWeightCollection.Document> createFileSegment(BufferedReader bufferedReader) throws IOException { | ||
| return new JsonTermWeightCollection.Segment<>(bufferedReader); | ||
| } | ||
| @Override | ||
thongnt99 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| public FileSegment<JsonTermWeightCollection.Document> createFileSegment(Path path) throws IOException { | ||
| return new JsonTermWeightCollection.Segment<>(path); | ||
| } | ||
| public static class Segment<T extends JsonTermWeightCollection.Document> extends JsonCollection.Segment<T> { | ||
| public Segment(Path path) throws IOException { | ||
| super(path); | ||
| } | ||
| public Segment(BufferedReader bufferedReader) throws IOException { | ||
| super(bufferedReader); | ||
| } | ||
| @Override | ||
| protected Document createNewDocument(JsonNode json) { | ||
| return new Document(json); | ||
| } | ||
| } | ||
|
|
||
| public static class Document extends JsonCollection.Document implements SourceTermWeightDocument { | ||
| private Map<String, Float> vector; | ||
| public Document(JsonNode json) { | ||
| super(json); | ||
| this.vector = new HashMap<>(); | ||
| // We're going to take the map associated with "vector" and generate pseudo-document. | ||
| JsonNode vectorNode = json.get("vector"); | ||
|
|
||
| // Iterate through the features: | ||
| final StringBuilder sb = new StringBuilder(); | ||
| vectorNode.fields().forEachRemaining( e -> { | ||
| Float cnt = e.getValue().floatValue(); | ||
| // Generate pseudo-document by appending the feature cnt times, | ||
| // where cnt is the value of the feature | ||
| this.vector.put(e.getKey(), cnt); | ||
| }); | ||
| } | ||
|
|
||
| @Override | ||
| public Map<String, Float> vector() { | ||
| return this.vector; | ||
| } | ||
| } | ||
| } | ||
32 changes: 32 additions & 0 deletions
32
src/main/java/io/anserini/collection/SourceTermWeightDocument.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,32 @@ | ||
| /* | ||
| * Anserini: A Lucene toolkit for reproducible information retrieval research | ||
| * | ||
| * Licensed 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 io.anserini.collection; | ||
|
|
||
| import java.util.Map; | ||
|
|
||
| /** | ||
| * A raw document from a collection. A {@code SourceDocument} is explicitly distinguish a from a | ||
| * Lucene {@link org.apache.lucene.document.Document}, which is the Lucene representation that | ||
| * can be directly inserted into an index. | ||
| */ | ||
| public interface SourceTermWeightDocument{ | ||
| /** | ||
| * Return the vector containing term and weight | ||
| * @return a map that map term to weight | ||
| */ | ||
| Map<String, Float> vector(); | ||
| } |
79 changes: 79 additions & 0 deletions
79
src/main/java/io/anserini/index/generator/TermWeightDocumentGenerator.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,79 @@ | ||
| /* | ||
| * Anserini: A Lucene toolkit for reproducible information retrieval research | ||
| * | ||
| * Licensed 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 io.anserini.index.generator; | ||
|
|
||
| import io.anserini.collection.InvalidContentsException; | ||
| import io.anserini.collection.SourceDocument; | ||
| import io.anserini.collection.SourceTermWeightDocument; | ||
| import io.anserini.index.Constants; | ||
| import io.anserini.index.IndexCollection; | ||
| import org.apache.lucene.document.*; | ||
| import org.apache.lucene.util.BytesRef; | ||
|
|
||
| import java.util.Map; | ||
|
|
||
| /** | ||
| * Converts a {@link SourceDocument} into a Lucene {@link Document}, ready to be indexed. | ||
| * | ||
| * @param <T> type of the source document | ||
| */ | ||
| public class TermWeightDocumentGenerator<T extends SourceTermWeightDocument & SourceDocument> implements LuceneDocumentGenerator<T> { | ||
| protected IndexCollection.Args args; | ||
|
|
||
| protected TermWeightDocumentGenerator() { | ||
| } | ||
| /** | ||
thongnt99 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| * Constructor with config and counters | ||
| * | ||
| * @param args configuration arguments | ||
| */ | ||
| public TermWeightDocumentGenerator(IndexCollection.Args args) { | ||
| this.args = args; | ||
| } | ||
|
|
||
| @Override | ||
| public Document createDocument(T src) throws GeneratorException { | ||
| String id = src.id(); | ||
| Map<String, Float> vector; | ||
| try { | ||
| vector = src.vector(); | ||
| } catch (InvalidContentsException e) { | ||
| // Catch and rethrow; indexer will eat the exception at top level and increment counters accordingly. | ||
| throw new InvalidDocumentException(); | ||
| } | ||
|
|
||
| if (vector.size() == 0) { | ||
| throw new EmptyDocumentException(); | ||
| } | ||
|
|
||
| // Make a new, empty document. | ||
| final Document document = new Document(); | ||
|
|
||
| // Store the collection docid. | ||
| document.add(new StringField(Constants.ID, id, Field.Store.YES)); | ||
| // This is needed to break score ties by docid. | ||
| document.add(new BinaryDocValuesField(Constants.ID, new BytesRef(id))); | ||
|
|
||
| if (args.storeRaw) { | ||
| document.add(new StoredField(Constants.RAW, src.raw())); | ||
| } | ||
| for (String term : vector.keySet()){ | ||
| document.add(new FeatureField(Constants.CONTENTS, term, vector.get(term))); | ||
| } | ||
| return document; | ||
| } | ||
| } | ||
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
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
15 changes: 15 additions & 0 deletions
15
src/main/java/io/anserini/search/query/FeatureGenerator.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,15 @@ | ||
| package io.anserini.search.query; | ||
|
|
||
| import org.apache.lucene.analysis.Analyzer; | ||
| import org.apache.lucene.search.Query; | ||
|
|
||
| public interface FeatureGenerator{ | ||
| /** | ||
thongnt99 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| * Generate queries with terms as features | ||
| * @param field | ||
| * @param analyzer | ||
| * @param queryText | ||
| * @return | ||
| */ | ||
| Query buildFeatureQuery(String field, Analyzer analyzer, String queryText); | ||
| } | ||
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.