Example usage for org.apache.lucene.search IndexSearcher IndexSearcher

List of usage examples for org.apache.lucene.search IndexSearcher IndexSearcher

Introduction

In this page you can find the example usage for org.apache.lucene.search IndexSearcher IndexSearcher.

Prototype

public IndexSearcher(IndexReaderContext context) 

Source Link

Document

Creates a searcher searching the provided top-level IndexReaderContext .

Usage

From source file:at.ac.univie.mminf.luceneSKOS.test.termexpansion.LabelbasedTermExpansionTest.java

License:Apache License

/**
 * This test indexes a sample metadata record (=lucene document) having a
 * "title", "description", and "subject" field.
 * <p/>/*from  www .j av  a  2  s.c o m*/
 * A search for "arms" returns that record as a result because "arms" is
 * defined as an alternative label for "weapons", the term which is
 * contained in the subject field.
 *
 * @throws IOException
 */
@Test
public void labelBasedTermExpansion() throws IOException {

    /* defining the document to be indexed */
    Document doc = new Document();
    doc.add(new Field("title", "Spearhead", TextField.TYPE_STORED));
    doc.add(new Field("description",
            "Roman iron spearhead. The spearhead was attached to one end of a wooden shaft..."
                    + "The spear was mainly a thrusting weapon, but could also be thrown. "
                    + "It was the principal weapon of the auxiliary soldier... "
                    + "(second - fourth century, Arbeia Roman Fort).",
            TextField.TYPE_NOT_STORED));
    doc.add(new Field("subject", "weapons", TextField.TYPE_NOT_STORED));

    /* setting up the SKOS analyzer */
    String skosFile = "src/test/resources/skos_samples/ukat_examples.n3";
    String indexPath = "build/";

    /* ExpansionType.URI->the field to be analyzed (expanded) contains URIs */
    Analyzer skosAnalyzer = new SKOSAnalyzer(indexPath, skosFile, ExpansionType.LABEL);

    /* Define different analyzers for different fields */
    Map<String, Analyzer> analyzerPerField = new HashMap<>();
    analyzerPerField.put("subject", skosAnalyzer);
    PerFieldAnalyzerWrapper indexAnalyzer = new PerFieldAnalyzerWrapper(new SimpleAnalyzer(), analyzerPerField);

    /* setting up a writer with a default (simple) analyzer */
    writer = new IndexWriter(new RAMDirectory(), new IndexWriterConfig(indexAnalyzer));

    /* adding the document to the index */
    writer.addDocument(doc);

    /* defining a query that searches over all fields */
    BooleanQuery.Builder builder = new BooleanQuery.Builder();
    builder.add(new TermQuery(new Term("title", "arms")), BooleanClause.Occur.SHOULD)
            .add(new TermQuery(new Term("description", "arms")), BooleanClause.Occur.SHOULD)
            .add(new TermQuery(new Term("subject", "arms")), BooleanClause.Occur.SHOULD);

    /* creating a new searcher */
    searcher = new IndexSearcher(DirectoryReader.open(writer, false));

    TopDocs results = searcher.search(builder.build(), 10);

    /* the document matches because "arms" is among the expanded terms */
    assertEquals(1, results.totalHits);

    /* defining a query that searches for a broader concept */
    Query query = new TermQuery(new Term("subject", "military equipment"));

    results = searcher.search(query, 10);

    /* ... also returns the document as result */
    assertEquals(1, results.totalHits);
}

From source file:at.ac.univie.mminf.luceneSKOS.test.termexpansion.URIbasedTermExpansionTest.java

License:Apache License

/**
 * This test indexes a sample metadata record (=lucene document) having a
 * "title", "description", and "subject" field, which is semantically
 * enriched by a URI pointing to a SKOS concept "weapons".
 * <p/>//from   w  w  w .  j av  a  2s.  c o m
 * A search for "arms" returns that record as a result because "arms" is
 * defined as an alternative label (altLabel) for the concept "weapons".
 *
 * @throws IOException
 */
@Test
public void uriBasedTermExpansion() throws IOException {

    /* defining the document to be indexed */
    Document doc = new Document();
    doc.add(new Field("title", "Spearhead", TextField.TYPE_STORED));
    doc.add(new Field("description",
            "Roman iron spearhead. The spearhead was attached to one end of a wooden shaft..."
                    + "The spear was mainly a thrusting weapon, but could also be thrown. "
                    + "It was the principal weapon of the auxiliary soldier... "
                    + "(second - fourth century, Arbeia Roman Fort).",
            TextField.TYPE_NOT_STORED));
    doc.add(new Field("subject", "http://www.ukat.org.uk/thesaurus/concept/859", TextField.TYPE_NOT_STORED));

    /* setting up the SKOS analyzer */
    String skosFile = "src/test/resources/skos_samples/ukat_examples.n3";
    String indexPath = "build/";

    /* ExpansionType.URI->the field to be analyzed (expanded) contains URIs */
    Analyzer skosAnalyzer = new SKOSAnalyzer(indexPath, skosFile, ExpansionType.URI);

    /* Define different analyzers for different fields */
    Map<String, Analyzer> analyzerPerField = new HashMap<>();
    analyzerPerField.put("subject", skosAnalyzer);
    PerFieldAnalyzerWrapper indexAnalyzer = new PerFieldAnalyzerWrapper(new SimpleAnalyzer(), analyzerPerField);

    /* setting up a writer with a default (simple) analyzer */
    writer = new IndexWriter(new RAMDirectory(), new IndexWriterConfig(indexAnalyzer));

    /* adding the document to the index */
    writer.addDocument(doc);

    /* defining a query that searches over all fields */
    BooleanQuery.Builder builder = new BooleanQuery.Builder();
    builder.add(new TermQuery(new Term("title", "arms")), BooleanClause.Occur.SHOULD)
            .add(new TermQuery(new Term("description", "arms")), BooleanClause.Occur.SHOULD)
            .add(new TermQuery(new Term("subject", "arms")), BooleanClause.Occur.SHOULD);

    /* creating a new searcher */
    searcher = new IndexSearcher(DirectoryReader.open(writer, false));

    TopDocs results = searcher.search(builder.build(), 10);

    /* the document matches because "arms" is among the expanded terms */
    assertEquals(1, results.totalHits);

    /* defining a query that searches for a broader concept */
    Query query = new TermQuery(new Term("subject", "military equipment"));

    results = searcher.search(query, 10);

    /* ... also returns the document as result */
    assertEquals(1, results.totalHits);

}

From source file:at.ac.univie.mminf.luceneSKOS.URIbasedTermExpansionTest.java

License:Apache License

/**
 * This test indexes a sample metadata record (=lucene document) having a
 * "title", "description", and "subject" field, which is semantically enriched
 * by a URI pointing to a SKOS concept "weapons".
 * /*from www.j  a va2 s  . c om*/
 * A search for "arms" returns that record as a result because "arms" is
 * defined as an alternative label (altLabel) for the concept "weapons".
 * 
 * @throws IOException
 */
@Test
public void uriBasedTermExpansion() throws IOException {

    /* defining the document to be indexed */
    Document doc = new Document();
    doc.add(new Field("title", "Spearhead", Field.Store.YES, Field.Index.ANALYZED));
    doc.add(new Field("description",
            "Roman iron spearhead. The spearhead was attached to one end of a wooden shaft..."
                    + "The spear was mainly a thrusting weapon, but could also be thrown. "
                    + "It was the principal weapon of the auxiliary soldier... "
                    + "(second - fourth century, Arbeia Roman Fort).",
            Field.Store.NO, Field.Index.ANALYZED));
    doc.add(new Field("subject", "http://www.ukat.org.uk/thesaurus/concept/859", Field.Store.NO,
            Field.Index.ANALYZED));

    /* setting up the SKOS analyzer */
    String skosFile = "src/test/resources/skos_samples/ukat_examples.n3";

    /* ExpansionType.URI->the field to be analyzed (expanded) contains URIs */
    Analyzer skosAnalyzer = new SKOSAnalyzer(matchVersion, skosFile, ExpansionType.URI);

    /* Define different analyzers for different fields */
    Map<String, Analyzer> analyzerPerField = new HashMap<String, Analyzer>();
    analyzerPerField.put("subject", skosAnalyzer);
    PerFieldAnalyzerWrapper indexAnalyzer = new PerFieldAnalyzerWrapper(new SimpleAnalyzer(matchVersion),
            analyzerPerField);

    /* setting up a writer with a default (simple) analyzer */
    writer = new IndexWriter(new RAMDirectory(), new IndexWriterConfig(matchVersion, indexAnalyzer));

    /* adding the document to the index */
    writer.addDocument(doc);

    /* defining a query that searches over all fields */
    BooleanQuery query1 = new BooleanQuery();
    query1.add(new TermQuery(new Term("title", "arms")), BooleanClause.Occur.SHOULD);
    query1.add(new TermQuery(new Term("description", "arms")), BooleanClause.Occur.SHOULD);
    query1.add(new TermQuery(new Term("subject", "arms")), BooleanClause.Occur.SHOULD);

    /* creating a new searcher */
    searcher = new IndexSearcher(IndexReader.open(writer, false));

    TopDocs results = searcher.search(query1, 10);

    /* the document matches because "arms" is among the expanded terms */
    Assert.assertEquals(1, results.totalHits);

    /* defining a query that searches for a broader concept */
    Query query2 = new TermQuery(new Term("subject", "military equipment"));

    results = searcher.search(query2, 10);

    /* ... also returns the document as result */
    Assert.assertEquals(1, results.totalHits);

}

From source file:at.fh_kufstein.InformationRetrievalUebung2.Main.java

public static void main(String[] args) {
    try {/*from   w w w  . ja v  a 2  s  . c o  m*/
        Directory dir = getBookIndexDirectory();
        IndexSearcher searcher = new IndexSearcher(dir);
        QueryParser parser = new QueryParser(Version.LUCENE_30, SEARCH_FIELD, new SimpleAnalyzer());
        Query query = parser.parse(getUserQuery());
        TopDocs docs = searcher.search(query, MAX_HITS);
        printResults(docs);
        searcher.close();
        dir.close();
    } catch (IOException | ParseException ex) {
        Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:at.lux.fotoretrieval.retrievalengines.LucenePathIndexRetrievalEngine.java

License:Open Source License

public List<ResultListEntry> getImagesBySemantics(String xPath, Vector objects, String whereToSearch,
        boolean recursive, JProgressBar progress) {
    if (progress != null) {
        progress.setString("Query expansion running");
    }//from ww w  .j  a v  a 2s.com
    List<Graph> graphList = getExpandedGraphs(xPath, whereToSearch);
    LinkedList<ResultListEntry> results = new LinkedList<ResultListEntry>();
    if (progress != null) {
        progress.setMinimum(0);
        progress.setMaximum(graphList.size());
        progress.setValue(0);
        progress.setString("Querying expanded graphs");
    }
    int countGraph = 0;
    try {
        indexSearcher = new IndexSearcher(parsePathIndexDirectory(whereToSearch));
        for (Graph graph : graphList) {
            results.addAll(searchForGraph(graph, whereToSearch));
            countGraph++;
            if (progress != null)
                progress.setValue(countGraph);
        }
        indexSearcher.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    Collections.sort(results);
    LinkedList<ResultListEntry> sorted = new LinkedList<ResultListEntry>();
    HashSet<String> doublettes = new HashSet<String>();
    for (ResultListEntry entry : results) {
        String descriptionPath = entry.getDescriptionPath();
        if (!doublettes.contains(descriptionPath)) {
            doublettes.add(descriptionPath);
            sorted.add(entry);
        }
    }
    return sorted.subList(0, Math.min(sorted.size(), maxResults));
}

From source file:at.lux.fotoretrieval.retrievalengines.LucenePathIndexRetrievalEngine.java

License:Open Source License

/**
 * Searches for all available nodes with given query String
 *
 * @param queryString   query like "Mathias Lux" or some text inside a node.
 * @param whereToSearch defines the base directory for the search
 * @return a List of Matching nodes with their associated weights
 *//*from   w  w  w  .j a va 2  s. c  om*/
public List<Node> getNodes(String queryString, String whereToSearch) {
    LinkedList<Node> result = new LinkedList<Node>();
    try {
        IndexSearcher searcher = new IndexSearcher(parseSemanticIndexDirectory(whereToSearch));
        String[] fieldsToSearchInForNodes = new String[] { "label", "givenname", "organization", "familyname",
                "all" };
        MultiFieldQueryParser qParse = new MultiFieldQueryParser(fieldsToSearchInForNodes,
                new StandardAnalyzer());
        Query query = qParse.parse(queryString);
        Hits hits = searcher.search(query);
        int hitsCount = hits.length();
        if (hitsCount > maxResults)
            hitsCount = maxResults;

        for (int i = 0; i < hitsCount; i++) {
            Document d = hits.doc(i);
            StringBuilder sb = new StringBuilder(20);
            sb.append(hits.score(i));
            sb.append(": ");
            sb.append(d.get("label"));
            Node node = new Node(Integer.parseInt(d.get("id")), hits.score(i), d.get("label"));
            node.setType(d.get("type"));
            result.add(node);
        }

    } catch (IOException e) {
        e.printStackTrace();
    } catch (ParseException e) {
        System.err.println("QueryString was: " + queryString);
        e.printStackTrace();
    }
    return result;
}

From source file:at.lux.fotoretrieval.retrievalengines.LucenePathIndexRetrievalEngineTest.java

License:Open Source License

public void testSearch() {
    try {//from   w  ww . j a v a  2 s  .com
        QueryParser qParser = new QueryParser("graph", new WhitespaceAnalyzer());
        IndexSearcher search = new IndexSearcher(
                "C:\\Dokumente und Einstellungen\\Mathias\\Eigene Dateien\\JavaProjects\\Caliph\\testdata\\idx_paths");
        Hits h = search.search(qParser.parse("_*_0_1"));
        for (int i = 0; i < h.length(); i++) {
            System.out.println(h.score(i) + ": " + h.doc(i).get("graph"));
        }
    } catch (IOException e) {
        e.printStackTrace();
        fail(e.toString());
    } catch (ParseException e) {
        e.printStackTrace();
        fail(e.toString());
    }
}

From source file:at.lux.fotoretrieval.retrievalengines.LucenePathIndexRetrievalEngineTest.java

License:Open Source License

public void testPrecisionAndRecall() {
    try {/*from w w w .j av  a  2  s . c  om*/
        String repository = "C:\\Java\\JavaProjects\\CaliphEmir\\testdata";
        //            String repository = "C:\\Dokumente und Einstellungen\\Mathias\\Eigene Dateien\\JavaProjects\\Caliph\\testdata";
        IndexSearcher is = new IndexSearcher(repository + "\\idx_paths");
        IndexReader ir = IndexReader.open(repository + "\\idx_paths");

        for (int i = 0; i < ir.numDocs(); i++) {
            testQuery(ir, new Graph(ir.document(i).getValues("graph")[0]), is);
        }

    } catch (IOException e) {
        e.printStackTrace();
        fail(e.toString());
    } catch (ParseException e) {
        e.printStackTrace();
    }
}

From source file:at.lux.fotoretrieval.retrievalengines.LucenePathIndexRetrievalEngineTest.java

License:Open Source License

public void testPrecisionAndRecallFullText() {
    try {// www . ja v  a 2s  . co  m
        String repository = "C:\\Java\\JavaProjects\\CaliphEmir\\testdata";
        //            String repository = "C:\\Dokumente und Einstellungen\\Mathias\\Eigene Dateien\\JavaProjects\\Caliph\\testdata";
        IndexSearcher is = new IndexSearcher(repository + "\\idx_paths");
        IndexReader ir = IndexReader.open(repository + "\\idx_paths");

        for (int i = 0; i < ir.numDocs(); i++) {
            testDirectQuery(ir, new Graph(ir.document(i).getValues("graph")[0]), is);
        }

    } catch (IOException e) {
        e.printStackTrace();
        fail(e.toString());
    } catch (ParseException e) {
        e.printStackTrace();
    }
}

From source file:at.lux.fotoretrieval.retrievalengines.LucenePathIndexRetrievalEngineTest.java

License:Open Source License

private void testDirectQuery(IndexReader ir, Graph query, IndexSearcher is) throws IOException, ParseException {
    IndexReader reader = IndexReader.open("C:\\Java\\JavaProjects\\CaliphEmir\\testdata\\idx_semantic");
    IndexSearcher searcher = new IndexSearcher("C:\\Java\\JavaProjects\\CaliphEmir\\testdata\\idx_fulltext");

    HashMap<Integer, String> node2label = new HashMap<Integer, String>();
    for (int j = 0; j < reader.numDocs(); j++) {
        String id = reader.document(j).getValues("id")[0];
        String label = reader.document(j).getValues("label")[0];
        node2label.put(Integer.parseInt(id), label);
    }//from  www  .j  ava  2s .  com
    // create results from mcs:
    LinkedList<ResultHolder> resultsMcs = new LinkedList<ResultHolder>();
    for (int j = 0; j < ir.numDocs(); j++) {
        Graph model = new Graph(ir.document(j).getValues("graph")[0]);
        float mcsSimilarity = query.getMcsSimilarity(model);
        String[] file = ir.document(j).getValues("file");
        for (int i = 0; i < file.length; i++) {
            String s = file[i];
            resultsMcs.add(new ResultHolder(mcsSimilarity, s));
        }
    }
    Collections.sort(resultsMcs);
    //            for (Iterator<ResultHolder> iterator = resultsMcs.iterator(); iterator.hasNext();) {
    //                ResultHolder r = iterator.next();
    //                System.out.println(r.getDocumentNumber() + ": " + r.getSimilarity());
    //            }

    // create results from search:
    StringBuilder qBuilder = new StringBuilder(64);
    for (Iterator<Node> iterator = query.getNodes().iterator(); iterator.hasNext();) {
        Node node = iterator.next();
        //            qBuilder.append("\"");
        qBuilder.append(node2label.get(node.getNodeID()));
        qBuilder.append(" ");
        //            qBuilder.append("\" ");
    }
    //        System.out.println(query);
    QueryParser qParse = new QueryParser("all", new WhitespaceAnalyzer());
    Query q = qParse.parse(qBuilder.toString());
    Hits hits = searcher.search(q);
    LinkedList<ResultHolder> resultsSearch = new LinkedList<ResultHolder>();
    for (int i = 0; i < hits.length(); i++) {
        String graph = hits.doc(i).getValues("file")[0];
        //            int docID = -1;
        //            for (int j = 0; j < ir.numDocs(); j++) {
        //                Graph model = new Graph(ir.document(j).getValues("graph")[0]);
        //                if (model.toString().equals(graph)) docID = j;
        //            }
        resultsSearch.add(new ResultHolder(hits.score(i), graph));
    }
    Collections.sort(resultsSearch);
    printPrecisionRecallPlotFileBased(resultsMcs, resultsSearch);
}