List of usage examples for org.apache.lucene.index IndexableField binaryValue
public BytesRef binaryValue();
From source file:com.basistech.lucene.tools.LuceneQueryTool.java
License:Apache License
private void printDocument(Document doc, int id, float score, PrintStream out) { Multimap<String, String> data = ArrayListMultimap.create(); List<String> orderedFieldNames = Lists.newArrayList(); if (showId) { orderedFieldNames.add("<id>"); data.put("<id>", Integer.toString(id)); }//w w w . j a v a2s.c o m if (showScore) { orderedFieldNames.add("<score>"); data.put("<score>", Double.toString(score)); } orderedFieldNames.addAll(fieldNames); Set<String> setFieldNames = Sets.newHashSet(); if (fieldNames.isEmpty()) { for (IndexableField f : doc.getFields()) { if (!setFieldNames.contains(f.name())) { orderedFieldNames.add(f.name()); } setFieldNames.add(f.name()); } } else { setFieldNames.addAll(fieldNames); } if (sortFields) { Collections.sort(orderedFieldNames); } for (IndexableField f : doc.getFields()) { if (setFieldNames.contains(f.name())) { if (f.stringValue() != null) { data.put(f.name(), f.stringValue()); } else if (f.binaryValue() != null) { data.put(f.name(), formatBinary(f.binaryValue().bytes)); } else { data.put(f.name(), "null"); } } } if (docsPrinted == 0 && formatter.getFormat() == Formatter.Format.TABULAR && !formatter.suppressNames()) { out.println(Joiner.on('\t').join(orderedFieldNames)); } String formatted = formatter.format(orderedFieldNames, data); if (!formatted.isEmpty()) { if (docsPrinted > 0 && formatter.getFormat() == Formatter.Format.MULTILINE) { out.println(); } out.println(formatted); ++docsPrinted; } }
From source file:com.codeReading.core.opengrok.search.Results.java
License:Open Source License
/** * Prints out results in html form. The following search helper fields are * required to be properly initialized: <ul> * <li>{@link SearchHelper#dataRoot}</li> * <li>{@link SearchHelper#contextPath}</li> * <li>{@link SearchHelper#searcher}</li> <li>{@link SearchHelper#hits}</li> * <li>{@link SearchHelper#historyContext} (ignored if {@code null})</li> * <li>{@link SearchHelper#sourceContext} (ignored if {@code null})</li> * <li>{@link SearchHelper#summerizer} (if sourceContext is not * {@code null})</li> <li>{@link SearchHelper#compressed} (if sourceContext * is not {@code null})</li> <li>{@link SearchHelper#sourceRoot} (if * sourceContext or historyContext is not {@code null})</li> </ul> * * @param out write destination//from w w w. j av a2s . c om * @param sh search helper which has all required fields set * @param start index of the first hit to print * @param end index of the last hit to print * @throws HistoryException * @throws IOException * @throws ClassNotFoundException */ public static void prettyPrint(Writer out, SearchHelper sh, int start, int end) throws HistoryException, IOException, ClassNotFoundException { String ctxE = Util.URIEncodePath(sh.contextPath); String xrefPrefix = sh.contextPath + Prefix.XREF_P; String morePrefix = sh.contextPath + Prefix.MORE_P; String xrefPrefixE = ctxE + Prefix.XREF_P; File xrefDataDir = new File(sh.dataRoot, Prefix.XREF_P.toString()); for (Map.Entry<String, ArrayList<Document>> entry : createMap(sh.searcher, sh.hits, start, end) .entrySet()) { String parent = entry.getKey(); out.write("<tr class=\"dir\"><td colspan=\"3\"><a href=\""); out.write(xrefPrefixE); out.write(Util.URIEncodePath(parent)); out.write("/\">"); out.write(parent); // htmlize ??? out.write("/</a>"); if (sh.desc != null) { out.write(" - <i>"); out.write(sh.desc.get(parent)); // htmlize ??? out.write("</i>"); } out.write("</td></tr>"); for (Document doc : entry.getValue()) { String rpath = doc.get("path"); String rpathE = Util.URIEncodePath(rpath); out.write("<tr>"); //Util.writeHAD(out, sh.contextPath, rpathE, false); out.write("<td class=\"f\"><a href=\""); out.write(xrefPrefixE); out.write(rpathE); out.write("\">"); out.write(rpath.substring(rpath.lastIndexOf('/') + 1)); // htmlize ??? out.write("</a></td><td><tt class=\"con\">"); if (sh.sourceContext != null) { Genre genre = Genre.get(doc.get("t")); Definitions tags = null; IndexableField tagsField = doc.getField("tags"); if (tagsField != null) { tags = Definitions.deserialize(tagsField.binaryValue().bytes); } if (Genre.XREFABLE == genre && sh.summerizer != null) { String xtags = getTags(xrefDataDir, rpath, sh.compressed); // FIXME use Highlighter from lucene contrib here, // instead of summarizer, we'd also get rid of // apache lucene in whole source ... out.write(sh.summerizer.getSummary(xtags).toString()); } else if (Genre.HTML == genre && sh.summerizer != null) { String htags = getTags(sh.sourceRoot, rpath, false); out.write(sh.summerizer.getSummary(htags).toString()); } else { FileReader r = genre == Genre.PLAIN ? new FileReader(new File(sh.sourceRoot, rpath)) : null; sh.sourceContext.getContext(r, out, xrefPrefix, morePrefix, rpath, tags, true, sh.builder.isDefSearch(), null); } } if (sh.historyContext != null) { sh.historyContext.getContext(new File(sh.sourceRoot, rpath), rpath, out, sh.contextPath); } out.write("</tt></td></tr>\n"); } } }
From source file:com.core.nlp.document.Document.java
License:Apache License
/** * Returns an array of byte arrays for of the fields that have the name specified * as the method parameter. This method returns an empty * array when there are no matching fields. It never * returns null.//ww w . ja va2 s. co m * * @param name the name of the field * @return a <code>BytesRef[]</code> of binary field values */ public final BytesRef[] getBinaryValues(String name) { final List<BytesRef> result = new ArrayList<>(); for (IndexableField field : fields) { if (field.name().equals(name)) { final BytesRef bytes = field.binaryValue(); if (bytes != null) { result.add(bytes); } } } return result.toArray(new BytesRef[result.size()]); }
From source file:com.core.nlp.document.Document.java
License:Apache License
/** * Returns an array of bytes for the first (or only) field that has the name * specified as the method parameter. This method will return <code>null</code> * if no binary fields with the specified name are available. * There may be non-binary fields with the same name. * * @param name the name of the field.//from ww w .j a v a 2 s .co m * @return a <code>BytesRef</code> containing the binary field value or <code>null</code> */ public final BytesRef getBinaryValue(String name) { for (IndexableField field : fields) { if (field.name().equals(name)) { final BytesRef bytes = field.binaryValue(); if (bytes != null) { return bytes; } } } return null; }
From source file:com.doculibre.constellio.services.ImportExportServicesImpl.java
License:Open Source License
@SuppressWarnings("unchecked") @Override//from www. java 2 s. co m public void importData(Directory directory, RecordCollection collection, ProgressInfo progressInfo) { try { ConnectorInstance connectorInstance = collection.getConnectorInstances().iterator().next(); RecordServices recordServices = ConstellioSpringUtils.getRecordServices(); String uniqueKeyMetaName = null; IndexField uniqueKeyIndexField = collection.getUniqueKeyIndexField(); for (ConnectorInstanceMeta connectorInstanceMeta : uniqueKeyIndexField.getConnectorInstanceMetas()) { if (connectorInstance.equals(connectorInstanceMeta.getConnectorInstance())) { uniqueKeyMetaName = connectorInstanceMeta.getName(); break; } } Pattern invalidDatePattern = Pattern .compile("^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}(\\.[0-9]*)?$"); IndexReader indexReader = DirectoryReader.open(directory); if (progressInfo != null) { progressInfo.setTotal(indexReader.numDocs()); } for (int i = 0; i < indexReader.numDocs(); i++) { Document document = indexReader.document(i); Record record = new Record(); record.setLastModified(new Date()); record.setConnectorInstance(connectorInstance); for (IndexableField field : document.getFields()) { // for (String fieldName : (Collection<String>) // indexReader.getFieldNames(FieldOption.ALL)) { if (field != null && field.fieldType().stored() && field.binaryValue() == null) { String metaName = field.name(); String metaContent = field.stringValue(); Matcher invalidDateMatcher = invalidDatePattern.matcher(metaContent); if (invalidDateMatcher.matches()) { metaContent = metaContent + "Z"; } if (uniqueKeyMetaName.equals(metaName)) { record.setUrl(metaContent); } RecordMeta meta = new RecordMeta(); ConnectorInstanceMeta connectorInstanceMeta = connectorInstance.getOrCreateMeta(metaName); meta.setConnectorInstanceMeta(connectorInstanceMeta); meta.setRecord(record); meta.setContent(metaContent); record.addContentMeta(meta); } } try { recordServices.makePersistent(record); // if (i % 500 == 0) { // EntityManager entityManager = // ConstellioPersistenceContext.getCurrentEntityManager(); // entityManager.getTransaction().commit(); // entityManager.getTransaction().begin(); // } } catch (Exception e) { e.printStackTrace(); } if (progressInfo != null) { progressInfo.setCurrentIndex(i); } } indexReader.close(); } catch (IOException e) { throw new RuntimeException(e); } }
From source file:com.doculibre.constellio.services.ImportExportServicesImpl.java
License:Open Source License
@SuppressWarnings("unchecked") @Override// w w w. j a v a 2s . c o m public void convertData(Directory directory, OutputStream output) { try { WritableWorkbook workbook = Workbook.createWorkbook(output); WritableSheet sheet = workbook.createSheet("fields", 0); WritableFont arial10font = new WritableFont(WritableFont.ARIAL, 10); WritableCellFormat arial10format = new WritableCellFormat(arial10font); IndexReader indexReader = DirectoryReader.open(directory); // { // int column = 0; // for (String fieldName : (Collection<String>) indexReader.getFieldNames()) { // Label label = new Label(column, 0, fieldName, arial10format); // sheet.addCell(label); // column++; // } // } int row = 1; for (int i = 0; i < indexReader.numDocs() /* && i != 502 */; i++) { Document document = indexReader.document(i); int column = 0; for (IndexableField field : document.getFields()) { if (row == 1) { Label label = new Label(column, 0, field.name(), arial10format); sheet.addCell(label); } if (field != null && field.fieldType().stored() && field.binaryValue() == null) { String indexedContent = field.stringValue(); indexedContent = convertText(indexedContent); Label label = new Label(column, row, indexedContent, arial10format); sheet.addCell(label); } column++; } row++; // if (i == 502) { // break; // } } indexReader.close(); workbook.write(); workbook.close(); } catch (IOException e) { throw new RuntimeException(e); } catch (RowsExceededException e) { throw new RuntimeException(e); } catch (WriteException e) { throw new RuntimeException(e); } }
From source file:com.epimorphics.server.indexers.LuceneResult.java
License:Apache License
/** * Returns all the values of a field. These will be either Strings (for literals and labels), * Resources (for URI fields) or Longs (for numeric fields) *//*from w w w. j a v a 2 s . c o m*/ public Object[] fieldValues(String fieldName) { IndexableField[] fields = doc.getFields(fieldName); Object[] results = new Object[fields.length]; for (int i = 0; i < fields.length; i++) { IndexableField field = fields[i]; Object value = field.numericValue(); if (value == null) { value = field.stringValue(); } if (value == null) { BytesRef ref = field.binaryValue(); value = ResourceFactory.createResource(ref.utf8ToString()); } results[i] = value; } return results; }
From source file:com.gemstone.gemfire.cache.lucene.internal.repository.serializer.SerializerUtil.java
License:Apache License
/** * Extract the gemfire key from a lucene document *//*from w w w .j a v a 2 s . com*/ public static Object getKey(Document doc) { IndexableField field = doc.getField(KEY_FIELD); if (field.stringValue() != null) { return field.stringValue(); } else { return keyFromBytes(field.binaryValue()); } }
From source file:com.gemstone.gemfire.cache.lucene.internal.repository.serializer.SerializerUtil.java
License:Apache License
/** * Extract the gemfire key term from a lucene document */// w w w . j a v a2 s . com public static Term getKeyTerm(Document doc) { IndexableField field = doc.getField(KEY_FIELD); if (field.stringValue() != null) { return new Term(KEY_FIELD, field.stringValue()); } else { return new Term(KEY_FIELD, field.binaryValue()); } }
From source file:com.lucure.core.codec.CompressingStoredFieldsWriter.java
License:Apache License
@Override public void writeField(FieldInfo info, IndexableField field) throws IOException { ++numStoredFieldsInDoc;/* w ww. j av a 2 s.c o m*/ int bits = 0; final BytesRef bytes; Number number = field.numericValue(); if (number != null) { if (number instanceof Byte || number instanceof Short || number instanceof Integer) { bits = NUMERIC_INT; } else if (number instanceof Long) { bits = NUMERIC_LONG; } else if (number instanceof Float) { bits = NUMERIC_FLOAT; } else if (number instanceof Double) { bits = NUMERIC_DOUBLE; } else { throw new IllegalArgumentException("cannot store numeric type " + number.getClass()); } bytes = null; } else { bytes = field.binaryValue(); if (bytes != null) { bits = BYTE_ARR; } else { bits = STRING; if (field.stringValue() == null) { throw new IllegalArgumentException("field " + field.name() + " is stored but does not have binaryValue, stringValue nor numericValue"); } } } final long infoAndBits = (((long) info.number) << TYPE_BITS) | bits; bufferedDocs.writeVLong(infoAndBits); boolean isRestricted = field instanceof RestrictedField; //set restricted bufferedDocs.writeByte((byte) (isRestricted ? 1 : 0)); if (isRestricted) { RestrictedField restrictedField = (RestrictedField) field; byte[] expression = restrictedField.getFieldVisibility().getExpression(); bufferedDocs.writeVInt(expression.length); bufferedDocs.writeBytes(expression, 0, expression.length); } switch (bits) { case BYTE_ARR: bufferedDocs.writeVInt(bytes.length); bufferedDocs.writeBytes(bytes.bytes, bytes.offset, bytes.length); break; case STRING: bufferedDocs.writeString(field.stringValue()); break; default: if (number instanceof Byte || number instanceof Short || number instanceof Integer) { bufferedDocs.writeInt(number.intValue()); } else if (number instanceof Long) { bufferedDocs.writeLong(number.longValue()); } else if (number instanceof Float) { bufferedDocs.writeInt(Float.floatToIntBits(number.floatValue())); } else { bufferedDocs.writeLong(Double.doubleToLongBits(number.doubleValue())); } break; } }