use of org.apache.lucene.document.SortedDocValuesField in project elasticsearch by elastic.
the class CollapsingTopDocsCollectorTests method testCollapseString.
public void testCollapseString() throws Exception {
CollapsingDocValuesProducer producer = new CollapsingDocValuesProducer<BytesRef>() {
@Override
public BytesRef randomGroup(int maxGroup) {
return new BytesRef(Integer.toString(randomIntBetween(0, maxGroup - 1)));
}
@Override
public void add(Document doc, BytesRef value, boolean multivalued) {
if (multivalued) {
doc.add(new SortedSetDocValuesField("field", value));
} else {
doc.add(new SortedDocValuesField("field", value));
}
}
@Override
public SortField sortField(boolean multivalued) {
if (multivalued) {
return new SortedSetSortField("field", false);
} else {
return new SortField("field", SortField.Type.STRING_VAL);
}
}
};
assertSearchCollapse(producer, false);
}
use of org.apache.lucene.document.SortedDocValuesField in project OpenGrok by OpenGrok.
the class AnalyzerGuru method populateDocument.
/**
* Populate a Lucene document with the required fields.
*
* @param doc The document to populate
* @param file The file to index
* @param path Where the file is located (from source root)
* @param fa The analyzer to use on the file
* @param xrefOut Where to write the xref (possibly {@code null})
* @throws IOException If an exception occurs while collecting the data
*/
public void populateDocument(Document doc, File file, String path, FileAnalyzer fa, Writer xrefOut) throws IOException {
String date = DateTools.timeToString(file.lastModified(), DateTools.Resolution.MILLISECOND);
doc.add(new Field(QueryBuilder.U, Util.path2uid(path, date), string_ft_stored_nanalyzed_norms));
doc.add(new Field(QueryBuilder.FULLPATH, file.getAbsolutePath(), string_ft_nstored_nanalyzed_norms));
doc.add(new SortedDocValuesField(QueryBuilder.FULLPATH, new BytesRef(file.getAbsolutePath())));
try {
HistoryReader hr = HistoryGuru.getInstance().getHistoryReader(file);
if (hr != null) {
doc.add(new TextField(QueryBuilder.HIST, hr));
// date = hr.getLastCommentDate() //RFE
}
} catch (HistoryException e) {
LOGGER.log(Level.WARNING, "An error occurred while reading history: ", e);
}
doc.add(new Field(QueryBuilder.DATE, date, string_ft_stored_nanalyzed_norms));
doc.add(new SortedDocValuesField(QueryBuilder.DATE, new BytesRef(date)));
if (path != null) {
doc.add(new TextField(QueryBuilder.PATH, path, Store.YES));
Project project = Project.getProject(path);
if (project != null) {
doc.add(new TextField(QueryBuilder.PROJECT, project.getPath(), Store.YES));
}
}
if (fa != null) {
Genre g = fa.getGenre();
if (g == Genre.PLAIN || g == Genre.XREFABLE || g == Genre.HTML) {
doc.add(new Field(QueryBuilder.T, g.typeName(), string_ft_stored_nanalyzed_norms));
}
fa.analyze(doc, StreamSource.fromFile(file), xrefOut);
String type = fa.getFileTypeName();
doc.add(new StringField(QueryBuilder.TYPE, type, Store.YES));
}
}
use of org.apache.lucene.document.SortedDocValuesField in project querydsl by querydsl.
the class LuceneQueryTest method createDocument.
private Document createDocument(final String docTitle, final String docAuthor, final String docText, final int docYear, final double docGross) {
Document doc = new Document();
// Reusing field for performance
if (titleField == null) {
titleField = new TextField("title", docTitle, Store.YES);
doc.add(titleField);
titleSortedField = new SortedDocValuesField("title", new BytesRef(docTitle));
doc.add(titleSortedField);
} else {
titleField.setStringValue(docTitle);
titleSortedField.setBytesValue(new BytesRef(docTitle));
doc.add(titleField);
doc.add(titleSortedField);
}
if (authorField == null) {
authorField = new TextField("author", docAuthor, Store.YES);
doc.add(authorField);
authorSortedField = new SortedDocValuesField("author", new BytesRef(docAuthor));
doc.add(authorSortedField);
} else {
authorField.setStringValue(docAuthor);
authorSortedField.setBytesValue(new BytesRef(docAuthor));
doc.add(authorField);
doc.add(authorSortedField);
}
if (textField == null) {
textField = new TextField("text", docText, Store.YES);
doc.add(textField);
textSortedField = new SortedDocValuesField("text", new BytesRef(docText));
doc.add(textSortedField);
} else {
textField.setStringValue(docText);
textSortedField.setBytesValue(new BytesRef(docText));
doc.add(textField);
doc.add(textSortedField);
}
if (yearField == null) {
yearField = new IntField("year", docYear, Store.YES);
doc.add(yearField);
yearSortedField = new NumericDocValuesField("year", docYear);
doc.add(yearSortedField);
} else {
yearField.setIntValue(docYear);
yearSortedField.setLongValue(docYear);
doc.add(yearField);
doc.add(yearSortedField);
}
if (grossField == null) {
grossField = new DoubleField("gross", docGross, Store.YES);
doc.add(grossField);
grossSortedField = new DoubleDocValuesField("gross", docGross);
doc.add(grossSortedField);
} else {
grossField.setDoubleValue(docGross);
grossSortedField.setDoubleValue(docGross);
doc.add(grossField);
doc.add(grossSortedField);
}
return doc;
}
use of org.apache.lucene.document.SortedDocValuesField in project gitblit by gitblit.
the class TicketIndexer method toDocField.
private void toDocField(Document doc, Lucene lucene, String value) {
if (StringUtils.isEmpty(value)) {
return;
}
doc.add(new org.apache.lucene.document.Field(lucene.name(), value, TextField.TYPE_STORED));
doc.add(new SortedDocValuesField(lucene.name(), new BytesRef(value)));
}
use of org.apache.lucene.document.SortedDocValuesField in project jackrabbit-oak by apache.
the class LuceneDocumentMaker method addTypedOrderedFields.
private boolean addTypedOrderedFields(List<Field> fields, PropertyState property, String pname, PropertyDefinition pd) {
// Ignore and warn if property multi-valued as not supported
if (property.getType().isArray()) {
log.warn("[{}] Ignoring ordered property {} of type {} for path {} as multivalued ordered property not supported", getIndexName(), pname, Type.fromTag(property.getType().tag(), true), path);
return false;
}
int tag = property.getType().tag();
int idxDefinedTag = pd.getType();
// Try converting type to the defined type in the index definition
if (tag != idxDefinedTag) {
log.debug("[{}] Ordered property defined with type {} differs from property {} with type {} in " + "path {}", getIndexName(), Type.fromTag(idxDefinedTag, false), property.toString(), Type.fromTag(tag, false), path);
tag = idxDefinedTag;
}
String name = FieldNames.createDocValFieldName(pname);
boolean fieldAdded = false;
Field f = null;
try {
if (tag == Type.LONG.tag()) {
//TODO Distinguish fields which need to be used for search and for sort
//If a field is only used for Sort then it can be stored with less precision
f = new NumericDocValuesField(name, property.getValue(Type.LONG));
} else if (tag == Type.DATE.tag()) {
String date = property.getValue(Type.DATE);
f = new NumericDocValuesField(name, FieldFactory.dateToLong(date));
} else if (tag == Type.DOUBLE.tag()) {
f = new DoubleDocValuesField(name, property.getValue(Type.DOUBLE));
} else if (tag == Type.BOOLEAN.tag()) {
f = new SortedDocValuesField(name, new BytesRef(property.getValue(Type.BOOLEAN).toString()));
} else if (tag == Type.STRING.tag()) {
f = new SortedDocValuesField(name, new BytesRef(property.getValue(Type.STRING)));
}
if (f != null) {
fields.add(f);
fieldAdded = true;
}
} catch (Exception e) {
log.warn("[{}] Ignoring ordered property. Could not convert property {} of type {} to type {} for path {}", getIndexName(), pname, Type.fromTag(property.getType().tag(), false), Type.fromTag(tag, false), path, e);
}
return fieldAdded;
}
Aggregations