-
Notifications
You must be signed in to change notification settings - Fork 25.2k
Store _doc_count field as custom term frequency #65776
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
Changes from 2 commits
dd98abd
9c42ab9
3c37312
300e8bc
4d77def
ffbcea4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch 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.index.mapper; | ||
|
||
import org.apache.lucene.analysis.Analyzer; | ||
import org.apache.lucene.analysis.TokenStream; | ||
import org.apache.lucene.analysis.tokenattributes.CharTermAttribute; | ||
import org.apache.lucene.analysis.tokenattributes.TermFrequencyAttribute; | ||
import org.apache.lucene.document.Field; | ||
import org.apache.lucene.document.FieldType; | ||
import org.apache.lucene.index.IndexOptions; | ||
|
||
/** | ||
* Custom field that allow storing an integer value as a term frequency in lucene. | ||
*/ | ||
public final class CustomTermFreqField extends Field { | ||
|
||
private static final FieldType FIELD_TYPE = new FieldType(); | ||
static { | ||
FIELD_TYPE.setTokenized(false); | ||
FIELD_TYPE.setOmitNorms(true); | ||
FIELD_TYPE.setIndexOptions(IndexOptions.DOCS_AND_FREQS); | ||
} | ||
|
||
private int fieldValue; | ||
|
||
public CustomTermFreqField(String fieldName, CharSequence term, int fieldValue) { | ||
super(fieldName, term, FIELD_TYPE); | ||
this.fieldValue = fieldValue; | ||
} | ||
|
||
public CustomTermFreqField(String fieldName, int fieldValue) { | ||
this(fieldName, fieldName, fieldValue); | ||
} | ||
|
||
public void setFieldValue(int fieldValue) { | ||
this.fieldValue = fieldValue; | ||
} | ||
|
||
@Override | ||
public TokenStream tokenStream(Analyzer analyzer, TokenStream reuse) { | ||
CustomTermFreqTokenStream stream; | ||
if (reuse instanceof CustomTermFreqTokenStream) { | ||
stream = (CustomTermFreqTokenStream) reuse; | ||
} else { | ||
stream = new CustomTermFreqTokenStream(); | ||
} | ||
stream.setValues((String) fieldsData, fieldValue); | ||
return stream; | ||
} | ||
|
||
private static final class CustomTermFreqTokenStream extends TokenStream { | ||
private final CharTermAttribute termAttribute = addAttribute(CharTermAttribute.class); | ||
private final TermFrequencyAttribute freqAttribute = addAttribute(TermFrequencyAttribute.class); | ||
private boolean used = true; | ||
private String value = null; | ||
private int freq = 0; | ||
|
||
private CustomTermFreqTokenStream() { | ||
} | ||
|
||
/** Sets the values */ | ||
void setValues(String value, int freq) { | ||
this.value = value; | ||
this.freq = freq; | ||
} | ||
|
||
@Override | ||
public boolean incrementToken() { | ||
if (used) { | ||
return false; | ||
} | ||
clearAttributes(); | ||
termAttribute.append(value); | ||
freqAttribute.setTermFrequency(freq); | ||
used = true; | ||
return true; | ||
} | ||
|
||
@Override | ||
public void reset() { | ||
used = false; | ||
} | ||
|
||
@Override | ||
public void close() { | ||
value = null; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,9 +18,6 @@ | |
*/ | ||
package org.elasticsearch.index.mapper; | ||
|
||
import org.apache.lucene.document.Field; | ||
import org.apache.lucene.document.NumericDocValuesField; | ||
import org.apache.lucene.search.DocValuesFieldExistsQuery; | ||
import org.apache.lucene.search.Query; | ||
import org.elasticsearch.common.xcontent.XContentParser; | ||
import org.elasticsearch.common.xcontent.XContentParserUtils; | ||
|
@@ -42,10 +39,10 @@ public static final class DocCountFieldType extends MappedFieldType { | |
|
||
public static final DocCountFieldType INSTANCE = new DocCountFieldType(); | ||
|
||
private static final Long defaultValue = 1L; | ||
public static final int defaultValue = 1; | ||
|
||
public DocCountFieldType() { | ||
super(NAME, false, false, true, TextSearchInfo.NONE, Collections.emptyMap()); | ||
super(NAME, false, false, false, TextSearchInfo.NONE, Collections.emptyMap()); | ||
} | ||
|
||
@Override | ||
|
@@ -55,12 +52,12 @@ public String typeName() { | |
|
||
@Override | ||
public String familyTypeName() { | ||
return NumberFieldMapper.NumberType.LONG.typeName(); | ||
return NumberFieldMapper.NumberType.INTEGER.typeName(); | ||
} | ||
|
||
@Override | ||
public Query existsQuery(QueryShardContext context) { | ||
return new DocValuesFieldExistsQuery(NAME); | ||
throw new IllegalArgumentException("Field [" + name() + "] of type [" + typeName() + "] does not support exists queries"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe use the same exception type as |
||
} | ||
|
||
@Override | ||
|
@@ -80,7 +77,7 @@ protected Object parseSourceValue(Object value) { | |
if ("".equals(value)) { | ||
return defaultValue; | ||
} else { | ||
return NumberFieldMapper.NumberType.objectToLong(value, false); | ||
return NumberFieldMapper.NumberType.INTEGER.parse(value, false); | ||
} | ||
} | ||
}; | ||
|
@@ -96,17 +93,14 @@ protected void parseCreateField(ParseContext context) throws IOException { | |
XContentParser parser = context.parser(); | ||
XContentParserUtils.ensureExpectedToken(XContentParser.Token.VALUE_NUMBER, parser.currentToken(), parser); | ||
|
||
long value = parser.longValue(false); | ||
int value = parser.intValue(false); | ||
if (value <= 0) { | ||
throw new IllegalArgumentException("Field [" + fieldType().name() + "] must be a positive integer."); | ||
throw new IllegalArgumentException("Field [" + fieldType().name() + "] must be a positive integer. Value [" | ||
+ value + "] is not allowed."); | ||
} | ||
final Field docCount = new NumericDocValuesField(NAME, value); | ||
context.doc().add(docCount); | ||
context.doc().add(new CustomTermFreqField(NAME, value)); | ||
} | ||
|
||
@Override | ||
public void preParse(ParseContext context) { } | ||
|
||
@Override | ||
public DocCountFieldType fieldType() { | ||
return (DocCountFieldType) super.fieldType(); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,9 +19,9 @@ | |
|
||
package org.elasticsearch.search.aggregations.bucket; | ||
|
||
import org.apache.lucene.index.DocValues; | ||
import org.apache.lucene.index.LeafReaderContext; | ||
import org.apache.lucene.index.NumericDocValues; | ||
import org.apache.lucene.index.PostingsEnum; | ||
import org.apache.lucene.index.Term; | ||
import org.elasticsearch.index.mapper.DocCountFieldMapper; | ||
|
||
import java.io.IOException; | ||
|
@@ -33,17 +33,25 @@ | |
*/ | ||
public class DocCountProvider { | ||
|
||
private NumericDocValues docCountValues; | ||
public static final int defaultValue = DocCountFieldMapper.DocCountFieldType.defaultValue; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should it be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Originally, I had it Changing it for both fields |
||
|
||
public long getDocCount(int doc) throws IOException { | ||
if (docCountValues != null && docCountValues.advanceExact(doc)) { | ||
return docCountValues.longValue(); | ||
private PostingsEnum docCountPostings; | ||
|
||
public int getDocCount(int doc) throws IOException { | ||
if (docCountPostings == null) { | ||
return defaultValue; | ||
} | ||
if (docCountPostings.docID() < doc) { | ||
docCountPostings.advance(doc); | ||
} | ||
if (docCountPostings.docID() == doc) { | ||
return docCountPostings.freq(); | ||
} else { | ||
return 1L; | ||
return defaultValue; | ||
} | ||
} | ||
|
||
public void setLeafReaderContext(LeafReaderContext ctx) throws IOException { | ||
docCountValues = DocValues.getNumeric(ctx.reader(), DocCountFieldMapper.NAME); | ||
docCountPostings = ctx.reader().postings(new Term(DocCountFieldMapper.NAME, DocCountFieldMapper.NAME)); | ||
} | ||
} |
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.
nit: I'd rather drop this constructor and require callers of this class to think about what is the right field name and term they should use