Search in sources :

Example 46 with IndexableField

use of org.apache.lucene.index.IndexableField in project OpenGrok by OpenGrok.

the class Results method prettyPrint.

/**
     * 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#summarizer} (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
     * @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 {
    Project p;
    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("/\">");
        // htmlize ???
        out.write(parent);
        out.write("/</a>");
        if (sh.desc != null) {
            out.write(" - <i>");
            // htmlize ???
            out.write(sh.desc.get(parent));
            out.write("</i>");
        }
        JSONArray messages;
        if ((p = Project.getProject(parent)) != null && (messages = Util.messagesToJson(p, RuntimeEnvironment.MESSAGES_MAIN_PAGE_TAG)).size() > 0) {
            out.write(" <a ");
            out.write("href=\"" + xrefPrefix + "/" + p.getName() + "\">");
            out.write("<span class=\"important-note important-note-rounded\" data-messages='" + messages + "'>!</span>");
            out.write("</a>");
        }
        out.write("</td></tr>");
        for (Document doc : entry.getValue()) {
            String rpath = doc.get(QueryBuilder.PATH);
            String rpathE = Util.URIEncodePath(rpath);
            DateFormat df;
            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("\"");
            if (RuntimeEnvironment.getInstance().isLastEditedDisplayMode()) {
                try {
                    // insert last edited date if possible
                    df = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
                    String dd = df.format(DateTools.stringToDate(doc.get("date")));
                    out.write(" class=\"result-annotate\" title=\"");
                    out.write("Last modified: ");
                    out.write(dd);
                    out.write("\"");
                } catch (ParseException ex) {
                    LOGGER.log(Level.WARNING, "An error parsing date information", ex);
                }
            }
            out.write(">");
            // htmlize ???
            out.write(rpath.substring(rpath.lastIndexOf('/') + 1));
            out.write("</a>");
            out.write("</td><td><tt class=\"con\">");
            if (sh.sourceContext != null) {
                Genre genre = Genre.get(doc.get("t"));
                Definitions tags = null;
                IndexableField tagsField = doc.getField(QueryBuilder.TAGS);
                if (tagsField != null) {
                    tags = Definitions.deserialize(tagsField.binaryValue().bytes);
                }
                Scopes scopes;
                IndexableField scopesField = doc.getField(QueryBuilder.SCOPES);
                if (scopesField != null) {
                    scopes = Scopes.deserialize(scopesField.binaryValue().bytes);
                } else {
                    scopes = new Scopes();
                }
                if (Genre.XREFABLE == genre && sh.summarizer != 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.summarizer.getSummary(xtags).toString());
                } else if (Genre.HTML == genre && sh.summarizer != null) {
                    String htags = getTags(sh.sourceRoot, rpath, false);
                    out.write(sh.summarizer.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, scopes);
                }
            }
            if (sh.historyContext != null) {
                sh.historyContext.getContext(new File(sh.sourceRoot, rpath), rpath, out, sh.contextPath);
            }
            out.write("</tt></td></tr>\n");
        }
    }
}
Also used : Definitions(org.opensolaris.opengrok.analysis.Definitions) ArrayList(java.util.ArrayList) JSONArray(org.json.simple.JSONArray) Document(org.apache.lucene.document.Document) IndexableField(org.apache.lucene.index.IndexableField) Project(org.opensolaris.opengrok.configuration.Project) Scopes(org.opensolaris.opengrok.analysis.Scopes) DateFormat(java.text.DateFormat) FileReader(java.io.FileReader) ParseException(java.text.ParseException) Genre(org.opensolaris.opengrok.analysis.FileAnalyzer.Genre) File(java.io.File) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 47 with IndexableField

use of org.apache.lucene.index.IndexableField in project neo4j by neo4j.

the class IndexType method clearDocument.

private void clearDocument(Document document) {
    Set<String> names = new HashSet<>();
    for (IndexableField field : document.getFields()) {
        names.add(field.name());
    }
    names.remove(LuceneLegacyIndex.KEY_DOC_ID);
    for (String name : names) {
        document.removeFields(name);
    }
}
Also used : IndexableField(org.apache.lucene.index.IndexableField) HashSet(java.util.HashSet)

Example 48 with IndexableField

use of org.apache.lucene.index.IndexableField in project neo4j by neo4j.

the class LuceneBatchInserterIndex method removeFromCache.

private void removeFromCache(long entityId) throws IOException, CorruptIndexException {
    IndexSearcher searcher = searcherManager.acquire();
    try {
        Query query = type.idTermQuery(entityId);
        TopDocs docs = searcher.search(query, 1);
        if (docs.totalHits > 0) {
            Document document = searcher.doc(docs.scoreDocs[0].doc);
            for (IndexableField field : document.getFields()) {
                String key = field.name();
                Object value = field.stringValue();
                removeFromCache(entityId, key, value);
            }
        }
    } finally {
        searcherManager.release(searcher);
    }
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) TopDocs(org.apache.lucene.search.TopDocs) IndexableField(org.apache.lucene.index.IndexableField) Query(org.apache.lucene.search.Query) Document(org.apache.lucene.document.Document)

Example 49 with IndexableField

use of org.apache.lucene.index.IndexableField in project neo4j by neo4j.

the class LuceneAllEntriesLabelScanReader method parse.

private LuceneNodeLabelRange parse(int id, Document document) {
    List<IndexableField> fields = document.getFields();
    int expectedLabelFields = fields.size() - 1;
    long[] scratchLabelIds = new long[expectedLabelFields];
    Bitmap[] scratchBitmaps = new Bitmap[expectedLabelFields];
    int i = 0;
    long rangeId = -1;
    for (IndexableField field : fields) {
        if (format.isRangeField(field)) {
            rangeId = format.rangeOf(field);
        } else if (format.isLabelBitmapField(field)) {
            scratchLabelIds[i] = format.labelId(field);
            scratchBitmaps[i] = format.readBitmap(field);
            i++;
        }
    }
    assert rangeId >= 0;
    final long[] labelIds;
    final Bitmap[] bitmaps;
    if (i < expectedLabelFields) {
        labelIds = Arrays.copyOf(scratchLabelIds, i);
        bitmaps = Arrays.copyOf(scratchBitmaps, i);
    } else {
        labelIds = scratchLabelIds;
        bitmaps = scratchBitmaps;
    }
    return LuceneNodeLabelRange.fromBitmapStructure(id, labelIds, getLongs(bitmaps, rangeId));
}
Also used : IndexableField(org.apache.lucene.index.IndexableField) Bitmap(org.neo4j.kernel.api.impl.labelscan.bitmaps.Bitmap)

Example 50 with IndexableField

use of org.apache.lucene.index.IndexableField in project neo4j by neo4j.

the class NodeRangeDocumentLabelScanStorageStrategy method labelsForNode.

@Override
public PrimitiveLongIterator labelsForNode(IndexSearcher searcher, long nodeId) {
    try {
        TopDocs topDocs = searcher.search(format.rangeQuery(format.bitmapFormat().rangeOf(nodeId)), 1);
        if (topDocs.scoreDocs.length < 1) {
            return PrimitiveLongCollections.emptyIterator();
        } else if (topDocs.scoreDocs.length > 1) {
            throw new RuntimeException("This label scan store seems to contain an incorrect number of entries (" + topDocs.scoreDocs.length + ")");
        }
        int doc = topDocs.scoreDocs[0].doc;
        PrimitiveLongSet labels = Primitive.longSet();
        for (IndexableField fields : searcher.doc(doc).getFields()) {
            if ("range".equals(fields.name())) {
                continue;
            }
            Number numericValue = fields.numericValue();
            if (numericValue != null) {
                Long bitmap = numericValue.longValue();
                if (format.bitmapFormat().hasLabel(bitmap, nodeId)) {
                    labels.add(Long.decode(fields.name()));
                }
            }
        }
        return labels.iterator();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : TopDocs(org.apache.lucene.search.TopDocs) IndexableField(org.apache.lucene.index.IndexableField) PrimitiveLongSet(org.neo4j.collection.primitive.PrimitiveLongSet) IOException(java.io.IOException)

Aggregations

IndexableField (org.apache.lucene.index.IndexableField)276 Document (org.apache.lucene.document.Document)90 CompressedXContent (org.elasticsearch.common.compress.CompressedXContent)75 Matchers.containsString (org.hamcrest.Matchers.containsString)57 BytesRef (org.apache.lucene.util.BytesRef)53 ArrayList (java.util.ArrayList)50 Field (org.apache.lucene.document.Field)34 Test (org.junit.Test)28 IOException (java.io.IOException)27 HashMap (java.util.HashMap)24 IndexReader (org.apache.lucene.index.IndexReader)24 Directory (org.apache.lucene.store.Directory)23 Map (java.util.Map)22 TopDocs (org.apache.lucene.search.TopDocs)22 Term (org.apache.lucene.index.Term)21 HashSet (java.util.HashSet)20 IndexWriterConfig (org.apache.lucene.index.IndexWriterConfig)20 DocumentMapper (org.elasticsearch.index.mapper.DocumentMapper)20 Query (org.apache.lucene.search.Query)19 Analyzer (org.apache.lucene.analysis.Analyzer)18