List of usage examples for org.apache.lucene.index IndexableField name
public String name();
From source file:br.bireme.ngrams.CompareResults.java
private static void writeDocDifferences(final String similarity, final Document doc1, final Document doc2, final BufferedWriter bwriter) throws IOException { assert similarity != null; assert doc1 != null; assert doc2 != null; assert bwriter != null; final StringBuilder builder = new StringBuilder(); final Set<String> diff = new HashSet<>(); final String id1 = doc1.get("id"); final String id2 = doc2.get("id"); for (IndexableField fld : doc1.getFields()) { final String name = fld.name(); if (name.endsWith("~notnormalized")) { if (!name.startsWith("id~")) { final String value1 = fld.stringValue(); final String value2 = doc2.get(name); if (((value1 == null) && (null != value2)) || !value1.equals(value2)) { final String name2 = name.substring(0, name.lastIndexOf('~')); diff.add("[" + name2 + "]|" + value1 + "|" + value2); }// w w w. j a v a 2 s .com } } } if (diff.isEmpty()) { builder.append("<identical>|"); builder.append(id1 + "|" + id2 + "\n"); } else { if (similarity.equals("1.0")) { builder.append("<very similar>|"); } else { builder.append("<similar>|"); } builder.append(id1 + "|" + id2 + "\n"); for (String di : diff) { builder.append(di); builder.append("\n"); } } builder.append("\n"); bwriter.append(builder.toString()); }
From source file:cn.hbu.cs.esearch.service.impl.EsearchSearchServiceImpl.java
License:Apache License
private static Map<String, String[]> convert(Document document, SearchRequest.SearchType searchType) { Map<String, String[]> map = new HashMap<String, String[]>(); if (document != null) { List<IndexableField> fields = document.getFields(); Iterator<IndexableField> iter = fields.iterator(); while (iter.hasNext()) { IndexableField fld = iter.next(); String fieldname = fld.name(); if (searchType == SearchRequest.SearchType.SIMPLE_QUERY) { if (fieldname.equals("_path") || fieldname.equals("_name")) { map.put(fieldname, document.getValues(fieldname)); }//from w ww . j a v a2s . com } else if (searchType == SearchRequest.SearchType.QUERY_AND_FETCH) { map.put(fieldname, document.getValues(fieldname)); } } } return map; }
From source file:com.baidu.rigel.biplatform.tesseract.resultset.isservice.ResultRecord.java
License:Open Source License
/** * ResultRecord//from w w w . j av a 2 s. c om * * @param doc * doc */ public ResultRecord(Document doc) { super(); List<IndexableField> idxFields = doc.getFields(); List<String> fieldNameList = new ArrayList<String>(); List<String> fieldList = new ArrayList<String>(); for (IndexableField field : idxFields) { fieldNameList.add(field.name()); fieldList.add(field.stringValue()); } this.fieldArray = fieldList.toArray(new String[0]); this.meta = new Meta(fieldNameList.toArray(new String[0])); }
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 . ja v a 2 s .c om*/ 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.bluedragon.search.search.QueryRun.java
License:Open Source License
private void addRow(IndexSearcher searcher, int docid, float score, int rank, int searchCount, int recordsSearched) throws CorruptIndexException, Exception { DocumentWrap document = new DocumentWrap(searcher.doc(docid)); queryResultData.addRow(1);/*from w ww. j a v a2 s. c om*/ queryResultData.setCurrentRow(queryResultData.getSize()); // Add in the standard columns that we know we have for every search queryResultData.setCell(1, new cfStringData(document.getId())); queryResultData.setCell(2, new cfStringData(document.getName())); queryResultData.setCell(3, new cfNumberData(score)); queryResultData.setCell(4, new cfNumberData(searchCount)); queryResultData.setCell(5, new cfNumberData(recordsSearched)); queryResultData.setCell(6, new cfNumberData(rank + 1)); String uC = queryAttributes.getUniqueColumn(); // Now we do the custom ones List<IndexableField> fields = document.getDocument().getFields(); Iterator<IndexableField> it = fields.iterator(); while (it.hasNext()) { IndexableField fieldable = it.next(); String fieldName = fieldable.name().toLowerCase(); // Check for the unique if (uniqueSet != null && fieldName.equals(uC)) { if (uniqueSet.contains(fieldable.stringValue())) { queryResultData.deleteRow(queryResultData.getSize()); return; } else uniqueSet.add(fieldable.stringValue()); } // Check to see if we have this column if (fieldName.equals("contents") && !queryAttributes.getContentFlag()) continue; if (!activeColumns.containsKey(fieldName)) { int newcolumn = queryResultData.addColumnData(fieldable.name().toUpperCase(), cfArrayData.createArray(1), null); activeColumns.put(fieldName, newcolumn); } int column = activeColumns.get(fieldName); if (column <= 6) continue; queryResultData.setCell(column, new cfStringData(fieldable.stringValue())); } // Do the context stuff if enable if (queryAttributes.getContextPassages() > 0) { Scorer scorer = new QueryScorer(queryAttributes.getQuery()); SimpleHTMLFormatter formatter = new SimpleHTMLFormatter(queryAttributes.getContextHighlightStart(), queryAttributes.getContextHighlightEnd()); Highlighter highlighter = new Highlighter(formatter, scorer); Fragmenter fragmenter = new SimpleFragmenter(queryAttributes.getContextBytes()); highlighter.setTextFragmenter(fragmenter); String nextContext = ""; String contents = document.getAttribute(DocumentWrap.CONTENTS); if (contents != null) { TokenStream tokenStream = AnalyzerFactory.get("simple").tokenStream(DocumentWrap.CONTENTS, new StringReader(contents)); String[] fragments = null; try { fragments = highlighter.getBestFragments(tokenStream, contents, queryAttributes.getContextPassages()); if (fragments.length == 1) { nextContext = fragments[0] + "..."; } else { StringBuilder context = new StringBuilder(); for (int f = 0; f < fragments.length; f++) { context.append("..."); context.append(fragments[f]); } context.append("..."); nextContext = context.toString(); } } catch (Exception e) { } // Add in the context if (!activeColumns.containsKey("context")) { int newcolumn = queryResultData.addColumnData("CONTEXT", cfArrayData.createArray(1), null); activeColumns.put("context", newcolumn); } queryResultData.setCell(activeColumns.get("context"), new cfStringData(nextContext)); } } }
From source file:com.core.nlp.document.Document.java
License:Apache License
/** * <p>Removes field with the specified name from the document. * If multiple fields exist with this name, this method removes the first field that has been added. * If there is no field with the specified name, the document remains unchanged.</p> * <p> Note that the removeField(s) methods like the add method only make sense * prior to adding a document to an index. These methods cannot * be used to change the content of an existing index! In order to achieve this, * a document has to be deleted from an index and a new changed version of that * document has to be added.</p>//w w w . j av a2 s. co m */ public final void removeField(String name) { Iterator<IndexableField> it = fields.iterator(); while (it.hasNext()) { IndexableField field = it.next(); if (field.name().equals(name)) { it.remove(); return; } } }
From source file:com.core.nlp.document.Document.java
License:Apache License
/** * <p>Removes all fields with the given name from the document. * If there is no field with the specified name, the document remains unchanged.</p> * <p> Note that the removeField(s) methods like the add method only make sense * prior to adding a document to an index. These methods cannot * be used to change the content of an existing index! In order to achieve this, * a document has to be deleted from an index and a new changed version of that * document has to be added.</p>//from w ww. j a v a 2 s . c o m */ public final void removeFields(String name) { Iterator<IndexableField> it = fields.iterator(); while (it.hasNext()) { IndexableField field = it.next(); if (field.name().equals(name)) { it.remove(); } } }
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./* w ww.j a v a 2 s . c om*/ * * @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 w w w.jav a2 s . c o 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.core.nlp.document.Document.java
License:Apache License
/** Returns a field with the given name if any exist in this document, or * null. If multiple fields exists with this name, this method returns the * first value added./* ww w. ja v a2s . c o m*/ */ public final IndexableField getField(String name) { for (IndexableField field : fields) { if (field.name().equals(name)) { return field; } } return null; }