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:com.bsiag.smartfield.server.services.lookup.CityLookupService.java

License:Open Source License

@Override
public void initializeService(ServiceRegistration registration) {
    super.initializeService(registration);

    String path = BundleContextUtility.resolve("${workspace_loc}/com.bsiag.smartfield.server/resources/index");
    try {/*from  www  . j  av a 2 s  .c  o m*/
        Directory index = new SimpleFSDirectory(new File(path));
        searcher = new IndexSearcher(index);

        byKeyQueryParser = new QueryParser(Version.LUCENE_34, "zip", new StandardAnalyzer(Version.LUCENE_34));
        byTextQueryParser = new MultiFieldQueryParser(Version.LUCENE_34, new String[] { "zip", "city" },
                new StandardAnalyzer(Version.LUCENE_34));
        byTextQueryParser.setDefaultOperator(QueryParser.Operator.AND);
    } catch (IOException e) {
        LOG.error(e.getMessage(), e);
    }
}

From source file:com.bugull.mongo.lucene.backend.IndexReopenTask.java

License:Apache License

@Override
public void run() {
    BuguIndex index = BuguIndex.getInstance();
    if (index.isReopening()) {
        return;/*  w w  w  .j a v a  2  s .c o m*/
    }
    index.setReopening(true);
    IndexSearcherCache searcherCache = IndexSearcherCache.getInstance();
    Map<String, IndexSearcher> map = searcherCache.getAll();
    for (Entry<String, IndexSearcher> entry : map.entrySet()) {
        IndexSearcher searcher = entry.getValue();
        IndexReader reader = searcher.getIndexReader();
        IndexReader newReader = null;
        try {
            newReader = IndexReader.openIfChanged(reader, true);
        } catch (IOException ex) {
            logger.error("Something is wrong when reopen the Lucene IndexReader", ex);
        }
        if (newReader != null && newReader != reader) {
            try {
                reader.decRef();
            } catch (IOException ex) {
                logger.error("Something is wrong when decrease the reference of the lucene IndexReader", ex);
            }
            IndexSearcher newSearcher = new IndexSearcher(newReader);
            searcherCache.put(entry.getKey(), newSearcher);
        }
    }
    index.setReopening(false);
}

From source file:com.bugull.mongo.lucene.cache.IndexSearcherCache.java

License:Apache License

public IndexSearcher get(String name) {
    IndexSearcher searcher = cache.get(name);
    if (searcher != null) {
        return searcher;
    }/*from  w w  w.j a v a  2  s  . c  om*/

    synchronized (this) {
        searcher = cache.get(name);
        if (searcher == null) {
            IndexWriter writer = IndexWriterCache.getInstance().get(name);
            IndexReader reader = null;
            try {
                reader = IndexReader.open(writer, true);
            } catch (CorruptIndexException ex) {
                logger.error("Something is wrong when open lucene IndexWriter", ex);
            } catch (IOException ex) {
                logger.error("Something is wrong when open lucene IndexWriter", ex);
            }
            searcher = new IndexSearcher(reader);
            cache.put(name, searcher);
        }
    }
    return searcher;
}

From source file:com.bugull.mongo.lucene.holder.IndexSearcherHolder.java

License:Apache License

public IndexSearcher get(String name) {
    IndexSearcher searcher = map.get(name);
    if (searcher != null) {
        return searcher;
    }/* w  w  w  . j  a va2s  .com*/

    synchronized (this) {
        searcher = map.get(name);
        if (searcher == null) {
            IndexWriter writer = IndexWriterHolder.getInstance().get(name);
            IndexReader reader = null;
            try {
                reader = IndexReader.open(writer, true);
            } catch (CorruptIndexException ex) {
                logger.error("Something is wrong when open lucene IndexWriter", ex);
            } catch (IOException ex) {
                logger.error("Something is wrong when open lucene IndexWriter", ex);
            }
            searcher = new IndexSearcher(reader);
            map.put(name, searcher);
        }
    }
    return searcher;
}

From source file:com.bull.aurocontrol.csst.poc.index.interval.BaseIntervalQueryTest.java

License:Apache License

protected IndexSearcher getSearcher() throws IOException {
    return new IndexSearcher(IndexReader.open(indexWriter, true));
}

From source file:com.burkeware.search.api.internal.provider.SearcherProvider.java

License:Open Source License

@Override
public IndexSearcher get() throws IOException {
    IndexReader indexReader = readerProvider.get();
    return new IndexSearcher(indexReader);
}

From source file:com.chenyi.langeasy.lucene.SearchTest.java

License:Apache License

public static void search() throws Exception {
    String index = "F:/Personal/ws_indigo/lucene/index";
    String field = "contents";

    IndexReader reader = DirectoryReader.open(FSDirectory.open(Paths.get(index)));
    IndexSearcher searcher = new IndexSearcher(reader);
    Analyzer analyzer = new StandardAnalyzer();

    QueryParser parser = new QueryParser(field, analyzer);
    String[] wordLst = new String[] { "abaft", "abase", "abnegation", "abominate", "abridge", "adjure",
            "adroit", "adulterate", "aggrandize", "allegory", "amalgam", "amalgamate", "amiable", "amortize",
            "anachronism", "apposite", "approbatory", "archetype", "arrogate", "artifice", "aseptic",
            "asperity", "assay", "attenuate", "baneful", "bauble", "behoove", "bemuse", "besmirch", "betroth",
            "boor", "bumptious", "bungler", "burgeon", "canard", "castigate", "catharsis", "cavil", "chaffing",
            "chary", "choleric", "chortle", "churlishness", "circumlocution", "circumlocutory", "cloture",
            "coda", "coffer", "cogitate", "cognate", "cognizant", "comeliness", "commiserate", "complaisance",
            "conjoin", "connotative", "contumacious", "contusion", "conviviality", "convoke", "corpulence",
            "curmudgeon", "dally", "dauntless", "decadence", "decisiveness", "delineate", "deliquesce",
            "denigrate", "deprecate", "depredation", "descant", "despoil", "determinate", "dichotomy",
            "digress", "dilettante", "disapprobation", "disheartened", "disputatious", "dissemble", "dissonant",
            "distention", "divestiture", "ebullience", "ecclesiastic", "edify", "educe", "efface",
            "effervescence", "effluvium", "effrontery", "effusive", "egress", "elaboration", "ellipsis",
            "embarkation", "enamored", "encomium", "encumber", "enervate", "enfeeble", "epicure", "epigram",
            "equinox", "equivocations", "estimable", "euphony", "evanescent", "exculpate", "exigent",
            "extemporize", "extricable", "exultation", "fatuous", "fecund", "fervid", "fetter", "froward",
            "fulsome", "fustian", "garrulous", "germane", "gerrymander", "gibber", "hackneyed", "harangue",
            "heretic", "homeostasis", "idiosyncrasy", "impale", "impenitent", "imperturbable", "impiety",
            "impolitic", "imprecate", "improvident", "impugn", "imputation", "inchoate", "incommodious",
            "incorporeal", "indigence", "indolent", "indubitably", "infamy", "ingenuous", "inimical",
            "instigate", "insubordinate", "intercede", "inveterate", "iota", "irreproachable", "kith",
            "knavery", "lambaste", "lambent", "latency", "levity", "licentious", "ligneous", "litigate",
            "lugubrious", "macerate", "maculate", "malediction", "malinger", "maudlin", "mendacious",
            "mesmerize", "minatory", "miscreant", "miser", "mollify", "morose", "munificent", "nefariousness",
            "nettle", "noisome", "obdurate", "obeisance", "obfuscate", "objurgate", "obstinate", "obtrude",
            "obviate", "odium", "omniscient", "opalescent", "opprobrious", "ossify", "ostensible", "ostracize",
            "palindrome", "palliate", "pallor", "panegyric", "parsimonious", "peccadillo", "pedagogue",
            "pellucid", "penitent", "perdition", "perfidious", "perquisite", "phlegmatic", "pinioned",
            "plenary", "polemicist", "portend", "prate", "prefatory", "preponderate", "prescience",
            "prevaricate", "promontory", "propinquity", "propitiate", "proselytize", "provident", "qualm",
            "quiescence", "raconteur", "ramification", "recondite", "recumbent", "recusant", "redolent",
            "refurbish", "relegate", "remonstrate", "renascence", "repast", "reprehend", "reprobate", "reproof",
            "repudiate", "retroaction", "revile", "rivet", "roseate", "ruffian", "sagacious", "salutatory",
            "sapid", "saturnine", "savant", "scanty", "sedulous", "servile", "slovenly", "soliloquy",
            "solubility", "spelunker", "splenetic", "suffuse", "sunder", "suppliant", "surreptitious", "torpid",
            "traduce", "tranquillity", "transmutation", "transpire", "troth", "truncate", "tumid", "turpitude",
            "unfeigned", "unwonted", "usury", "winsome", "zealot", "zephyr" };
    List<String> unmatchList = new ArrayList<>();
    for (String word : wordLst) {
        boolean result = search(searcher, parser, word);
        if (!result) {
            unmatchList.add(word);/*from w w  w . j  av  a 2 s  . c o  m*/
        }
    }
    reader.close();

    System.out.println("findWTotal: " + findWTotal);
    System.out.println("findSTotal: " + findSTotal);
    System.out.println(new JSONArray(unmatchList).toString(3));
    System.out.println(unmatchList.size());
    System.out.println();

    String[] arr2 = new String[] { "abaft", "approbatory", "betroth", "bungler", "churlishness", "contumacious",
            "deliquesce", "extricable", "froward", "imprecate", "incommodious", "ligneous", "minatory",
            "nefariousness", "objurgate", "obtrude", "opprobrious", "preponderate", "recusant", "retroaction",
            "sapid", "sedulous", "traduce", "tumid", "unwonted" };
    String[] unmatchArr = new String[unmatchList.size()];
    unmatchArr = unmatchList.toArray(unmatchArr);
    CompareResult.compare(arr2, unmatchArr);

    // boolean result = search(searcher, parser, "abc");
}

From source file:com.chimpler.example.FacetLuceneAdvancedSearcher.java

License:Apache License

public static void main(String args[]) throws Exception {
    if (args.length != 5) {
        System.err.println(/*from  ww w  . ja  v a 2 s .c o  m*/
                "Parameters: [index directory] [taxonomy directory] [query] [field drilldown] [value drilldown]");
        System.exit(1);
    }

    String indexDirectory = args[0];
    String taxonomyDirectory = args[1];
    String query = args[2];
    String fieldDrilldown = args[3];
    String valueDrilldown = args[4];

    IndexReader indexReader = DirectoryReader.open(FSDirectory.open(new File(indexDirectory)));
    IndexSearcher indexSearcher = new IndexSearcher(indexReader);

    TaxonomyReader taxonomyReader = new DirectoryTaxonomyReader(FSDirectory.open(new File(taxonomyDirectory)));

    CategoryPath drillDownCategoryPath = new CategoryPath(fieldDrilldown + "/" + valueDrilldown, '/');

    FacetSearchParams searchParams = new FacetSearchParams();
    searchParams.facetRequests.add(new CountFacetRequest(new CategoryPath("author"), 100));
    searchParams.facetRequests.add(new CountFacetRequest(new CategoryPath("book_category"), 100));
    searchParams.facetRequests.add(new CountFacetRequest(drillDownCategoryPath, 100));

    ComplexPhraseQueryParser queryParser = new ComplexPhraseQueryParser(LUCENE_VERSION, "title",
            new StandardAnalyzer(LUCENE_VERSION));

    Query luceneQuery = queryParser.parse(query);
    //luceneQuery = DrillDownQuery.query(luceneQuery, drillDownCategoryPath);

    // Collectors to get top results and facets
    TopScoreDocCollector topScoreDocCollector = TopScoreDocCollector.create(10, true);
    FacetsCollector facetsCollector = FacetsCollector.create(searchParams, indexReader, taxonomyReader);
    indexSearcher.search(luceneQuery, MultiCollector.wrap(topScoreDocCollector, facetsCollector));
    System.out.println("Found:");

    for (ScoreDoc scoreDoc : topScoreDocCollector.topDocs().scoreDocs) {
        Document document = indexReader.document(scoreDoc.doc);
        System.out.printf("- book: id=%s, title=%s, book_category=%s, authors=%s, score=%f\n",
                document.get("id"), document.get("title"), document.get("book_category"),
                document.get("authors"), scoreDoc.score);
    }

    System.out.println("Facets:");
    for (FacetResult facetResult : facetsCollector.getFacetResults()) {
        System.out.println("- " + facetResult.getFacetResultNode().label);
        for (FacetResultNode facetResultNode : facetResult.getFacetResultNode().subResults) {
            System.out.printf("    - %s (%f)\n", facetResultNode.label.toString(), facetResultNode.value);
            for (FacetResultNode subFacetResultNode : facetResultNode.subResults) {
                System.out.printf("        - %s (%f)\n", subFacetResultNode.label.toString(),
                        subFacetResultNode.value);
            }
        }
    }
    taxonomyReader.close();
    indexReader.close();
}

From source file:com.chimpler.example.FacetLuceneIndexer.java

License:Apache License

public static void main(String args[]) throws Exception {
    //      if (args.length != 3) {
    //         System.err.println("Parameters: [index directory] [taxonomy directory] [json file]");
    //         System.exit(1);
    //      }/*  w ww .ja  v  a  2  s  .c  om*/

    String indexDirectory = "index";
    String taxonomyDirectory = "taxonomy";
    String jsonFileName = "/home/qiuqiang/workspace/facet-lucene-example/books.json";

    IndexWriterConfig writerConfig = new IndexWriterConfig(LUCENE_VERSION,
            new WhitespaceAnalyzer(LUCENE_VERSION));
    writerConfig.setOpenMode(OpenMode.APPEND);
    IndexWriter indexWriter = new IndexWriter(FSDirectory.open(new File(indexDirectory)), writerConfig);

    TaxonomyWriter taxonomyWriter = new DirectoryTaxonomyWriter(MMapDirectory.open(new File(taxonomyDirectory)),
            OpenMode.APPEND);

    TaxonomyReader taxonomyReader = new DirectoryTaxonomyReader(FSDirectory.open(new File(taxonomyDirectory)));

    String content = IOUtils.toString(new FileInputStream(jsonFileName));
    JSONArray bookArray = new JSONArray(content);

    Field idField = new IntField("id", 0, Store.YES);
    Field titleField = new TextField("title", "", Store.YES);
    Field authorsField = new TextField("authors", "", Store.YES);
    Field bookCategoryField = new TextField("book_category", "", Store.YES);

    indexWriter.deleteAll();

    FacetFields facetFields = new FacetFields(taxonomyWriter);

    for (int i = 0; i < bookArray.length(); i++) {
        Document document = new Document();

        JSONObject book = bookArray.getJSONObject(i);
        int id = book.getInt("id");
        String title = book.getString("title");
        String bookCategory = book.getString("book_category");

        List<CategoryPath> categoryPaths = new ArrayList<CategoryPath>();

        String authorsString = "";
        JSONArray authors = book.getJSONArray("authors");
        for (int j = 0; j < authors.length(); j++) {
            String author = authors.getString(j);
            if (j > 0) {
                authorsString += ", ";
            }
            categoryPaths.add(new CategoryPath("author", author));
            authorsString += author;
        }
        categoryPaths.add(new CategoryPath("book_category" + bookCategory, '/'));

        idField.setIntValue(id);
        titleField.setStringValue(title);
        authorsField.setStringValue(authorsString);
        bookCategoryField.setStringValue(bookCategory);

        facetFields.addFields(document, categoryPaths);

        document.add(idField);
        document.add(titleField);
        document.add(authorsField);
        document.add(bookCategoryField);

        indexWriter.addDocument(document);

        System.out.printf("Book: id=%d, title=%s, book_category=%s, authors=%s\n", id, title, bookCategory,
                authors);
    }

    taxonomyWriter.prepareCommit();
    try {
        taxonomyWriter.commit();
    } catch (Exception e) {
        taxonomyWriter.rollback();
    }

    //      taxonomyWriter.close();
    //      
    //      indexWriter.commit();
    //      indexWriter.close();

    String query = "story";

    IndexReader indexReader = DirectoryReader.open(indexWriter, false);
    IndexReader indexReader2 = DirectoryReader.open(indexWriter, false);
    System.out.println(indexReader == indexReader2);

    IndexSearcher indexSearcher = new IndexSearcher(indexReader);

    TaxonomyReader newTaxonomyReader = DirectoryTaxonomyReader.openIfChanged(taxonomyReader);
    if (newTaxonomyReader != null) {
        TaxonomyReader tmp = taxonomyReader;
        taxonomyReader = newTaxonomyReader;
        tmp.close();
    } else {
        System.out.println("null");
    }

    ArrayList<FacetRequest> facetRequests = new ArrayList<FacetRequest>();
    facetRequests.add(new CountFacetRequest(new CategoryPath("author"), 100));
    facetRequests.add(new CountFacetRequest(new CategoryPath("book_category"), 100));

    FacetSearchParams searchParams = new FacetSearchParams(facetRequests);

    ComplexPhraseQueryParser queryParser = new ComplexPhraseQueryParser(LUCENE_VERSION, "title",
            new StandardAnalyzer(LUCENE_VERSION));
    Query luceneQuery = queryParser.parse(query);

    // Collectors to get top results and facets
    TopScoreDocCollector topScoreDocCollector = TopScoreDocCollector.create(10, true);
    FacetsCollector facetsCollector = FacetsCollector.create(searchParams, indexReader, taxonomyReader);
    indexSearcher.search(luceneQuery, MultiCollector.wrap(topScoreDocCollector, facetsCollector));
    System.out.println("Found:");

    for (ScoreDoc scoreDoc : topScoreDocCollector.topDocs().scoreDocs) {
        Document document = indexReader.document(scoreDoc.doc);
        System.out.printf("- book: id=%s, title=%s, book_category=%s, authors=%s, score=%f\n",
                document.get("id"), document.get("title"), document.get("book_category"),
                document.get("authors"), scoreDoc.score);
    }

    System.out.println("Facets:");
    for (FacetResult facetResult : facetsCollector.getFacetResults()) {
        System.out.println("- " + facetResult.getFacetResultNode().label);
        for (FacetResultNode facetResultNode : facetResult.getFacetResultNode().subResults) {
            System.out.printf("    - %s (%f)\n", facetResultNode.label.toString(), facetResultNode.value);
            for (FacetResultNode subFacetResultNode : facetResultNode.subResults) {
                System.out.printf("        - %s (%f)\n", subFacetResultNode.label.toString(),
                        subFacetResultNode.value);
            }
        }
    }
    taxonomyReader.close();
    indexReader.close();

    taxonomyWriter.commit();
    taxonomyWriter.close();

    indexWriter.commit();
    indexWriter.close();

}

From source file:com.chimpler.example.FacetLuceneSearcher.java

License:Apache License

public static void main(String args[]) throws Exception {
    //      if (args.length != 3) {
    //         System.err.println("Parameters: [index directory] [taxonomy directory] [query]");
    //         System.exit(1);
    //      }//  w  w w  . ja  va2 s. c om

    String indexDirectory = "index";
    String taxonomyDirectory = "taxonomy";
    String query = "story";

    IndexReader indexReader = DirectoryReader.open(FSDirectory.open(new File(indexDirectory)));
    IndexSearcher indexSearcher = new IndexSearcher(indexReader);

    TaxonomyReader taxonomyReader = new DirectoryTaxonomyReader(FSDirectory.open(new File(taxonomyDirectory)));

    ArrayList<FacetRequest> facetRequests = new ArrayList<FacetRequest>();
    facetRequests.add(new CountFacetRequest(new CategoryPath("author"), 100));
    facetRequests.add(new CountFacetRequest(new CategoryPath("book_category"), 100));

    FacetSearchParams searchParams = new FacetSearchParams(facetRequests);

    ComplexPhraseQueryParser queryParser = new ComplexPhraseQueryParser(LUCENE_VERSION, "title",
            new StandardAnalyzer(LUCENE_VERSION));
    Query luceneQuery = queryParser.parse(query);

    // Collectors to get top results and facets
    TopScoreDocCollector topScoreDocCollector = TopScoreDocCollector.create(10, true);
    FacetsCollector facetsCollector = FacetsCollector.create(searchParams, indexReader, taxonomyReader);
    indexSearcher.search(luceneQuery, MultiCollector.wrap(topScoreDocCollector, facetsCollector));
    System.out.println("Found:");

    for (ScoreDoc scoreDoc : topScoreDocCollector.topDocs().scoreDocs) {
        Document document = indexReader.document(scoreDoc.doc);
        System.out.printf("- book: id=%s, title=%s, book_category=%s, authors=%s, score=%f\n",
                document.get("id"), document.get("title"), document.get("book_category"),
                document.get("authors"), scoreDoc.score);
    }

    System.out.println("Facets:");
    for (FacetResult facetResult : facetsCollector.getFacetResults()) {
        System.out.println("- " + facetResult.getFacetResultNode().label);
        for (FacetResultNode facetResultNode : facetResult.getFacetResultNode().subResults) {
            System.out.printf("    - %s (%f)\n", facetResultNode.label.toString(), facetResultNode.value);
            for (FacetResultNode subFacetResultNode : facetResultNode.subResults) {
                System.out.printf("        - %s (%f)\n", subFacetResultNode.label.toString(),
                        subFacetResultNode.value);
            }
        }
    }
    taxonomyReader.close();
    indexReader.close();
}