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.helger.pd.indexer.lucene.PDLucene.java

License:Apache License

/**
 * Get a searcher on this index./*from   www.  jav a 2  s. c om*/
 *
 * @return <code>null</code> if no reader or no searcher could be obtained
 * @throws IOException
 *         On IO error
 */
@Nullable
public IndexSearcher getSearcher() throws IOException {
    _checkClosing();
    final IndexReader aReader = _getReader();
    if (aReader == null) {
        // Index not readable
        return null;
    }

    if (m_aSearchReader == aReader) {
        // Reader did not change - use cached searcher
        assert m_aSearcher != null;
        return m_aSearcher;
    }

    // Create new searcher only if necessary
    m_aSearchReader = aReader;
    m_aSearcher = new IndexSearcher(aReader);
    return m_aSearcher;
}

From source file:com.hrstc.lucene.queryexpansion.PatentClassCodeBasedQueryExpansion.java

License:Apache License

public PatentClassCodeBasedQueryExpansion(String classCodesIndexDir, int Nbr_Terms, float alpha, float beta,
        float decay) throws IOException, Exception {
    Directory classCodesDir = FSDirectory.open(new File(classCodesIndexDir));
    classCodesSearcher = new IndexSearcher(DirectoryReader.open(classCodesDir));
    parameters = new HashMap<>();
    parameters.put(RocchioQueryExpansion.ROCCHIO_ALPHA_FLD, alpha);
    parameters.put(RocchioQueryExpansion.ROCCHIO_BETA_FLD, beta);
    parameters.put(RocchioQueryExpansion.DECAY_FLD, decay);
    this.Nbr_Terms = Nbr_Terms;

}

From source file:com.hrstc.lucene.queryexpansion.PatentRocchioQueryExpansion.java

License:Apache License

public PatentRocchioQueryExpansion(String indexDir, int Nbr_Docs, int Nbr_Terms, int source, float alpha,
        float beta, float gamma, float decay) throws IOException, Exception {
    Directory dir = FSDirectory.open(new File(indexDir));
    searcher = new IndexSearcher(DirectoryReader.open(dir));
    parameters = new HashMap<>();
    parameters.put(RocchioQueryExpansion.ROCCHIO_ALPHA_FLD, alpha);
    parameters.put(RocchioQueryExpansion.ROCCHIO_BETA_FLD, beta);
    parameters.put(RocchioQueryExpansion.ROCCHIO_GAMMA_FLD, gamma);
    parameters.put(RocchioQueryExpansion.DECAY_FLD, decay);
    this.Nbr_Docs = Nbr_Docs;
    this.Nbr_Terms = Nbr_Terms;
    this.source = source;
    if (source <= 0 || source == 4 || source == 6 || source > 7) {
        throw new Exception("Invalid source of expansion!");
    }//  ww w  .j  a  va  2 s .  co m
}

From source file:com.hrstc.lucene.queryreduction.PatentClassCodeBasedQueryReduction.java

License:Apache License

public PatentClassCodeBasedQueryReduction(String classCodesIndexDir, int Nbr_Terms)
        throws IOException, Exception {
    Directory classCodesDir = FSDirectory.open(new File(classCodesIndexDir));
    classCodesSearcher = new IndexSearcher(DirectoryReader.open(classCodesDir));
    this.Nbr_Terms = Nbr_Terms;

}

From source file:com.hrstc.lucene.queryreduction.PatentRocchioQueryReduction.java

License:Apache License

public PatentRocchioQueryReduction(String indexDir, int Nbr_Docs, int Nbr_Terms, int source, float alpha,
        float beta, float gamma, float decay) throws IOException, Exception {
    Directory dir = FSDirectory.open(new File(indexDir));
    searcher = new IndexSearcher(DirectoryReader.open(dir));
    parameters = new HashMap<>();
    parameters.put(RocchioQueryReduction.ROCCHIO_ALPHA_FLD, alpha);
    parameters.put(RocchioQueryReduction.ROCCHIO_BETA_FLD, beta);
    parameters.put(RocchioQueryReduction.ROCCHIO_GAMMA_FLD, gamma);
    parameters.put(RocchioQueryReduction.DECAY_FLD, decay);
    this.Nbr_Docs = Nbr_Docs;
    this.Nbr_Terms = Nbr_Terms;
    this.source = source;
    if (source <= 0 || source == 4 || source == 6 || source > 7) {
        throw new Exception("Invalid source of expansion!");
    }/*w  w  w  . j av a2  s  .c  om*/
}

From source file:com.humi.lucene.test.SearchFiles.java

License:Apache License

/** Simple command-line based search demo. */
public static void main(String[] args) throws Exception {
    String usage = "Usage:\tjava org.apache.lucene.demo.SearchFiles [-index dir] [-field f] [-repeat n] [-queries file] [-query string] [-raw] [-paging hitsPerPage]\n\nSee http://lucene.apache.org/core/4_1_0/demo/ for details.";
    if (args.length > 0 && ("-h".equals(args[0]) || "-help".equals(args[0]))) {
        System.out.println(usage);
        System.exit(0);/*from  w  ww  . ja v a  2  s .  c  o m*/
    }

    String index = "index";
    String field = "contents";
    String queries = null;
    int repeat = 0;
    boolean raw = false;
    String queryString = null;
    int hitsPerPage = 10;

    for (int i = 0; i < args.length; i++) {
        if ("-index".equals(args[i])) {
            index = args[i + 1];
            i++;
        } else if ("-field".equals(args[i])) {
            field = args[i + 1];
            i++;
        } else if ("-queries".equals(args[i])) {
            queries = args[i + 1];
            i++;
        } else if ("-query".equals(args[i])) {
            queryString = args[i + 1];
            i++;
        } else if ("-repeat".equals(args[i])) {
            repeat = Integer.parseInt(args[i + 1]);
            i++;
        } else if ("-raw".equals(args[i])) {
            raw = true;
        } else if ("-paging".equals(args[i])) {
            hitsPerPage = Integer.parseInt(args[i + 1]);
            if (hitsPerPage <= 0) {
                System.err.println("There must be at least 1 hit per page.");
                System.exit(1);
            }
            i++;
        }
    }

    IndexReader reader = DirectoryReader.open(FSDirectory.open(new File(index)));
    IndexSearcher searcher = new IndexSearcher(reader);
    // :Post-Release-Update-Version.LUCENE_XY:
    Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_4_10_0);

    BufferedReader in = null;
    if (queries != null) {
        in = new BufferedReader(new InputStreamReader(new FileInputStream(queries), StandardCharsets.UTF_8));
    } else {
        in = new BufferedReader(new InputStreamReader(System.in, StandardCharsets.UTF_8));
    }
    // :Post-Release-Update-Version.LUCENE_XY:
    QueryParser parser = new QueryParser(Version.LUCENE_4_10_0, field, analyzer);
    while (true) {
        if (queries == null && queryString == null) { // prompt the user
            System.out.println("Enter query: ");
        }

        String line = queryString != null ? queryString : in.readLine();

        if (line == null || line.length() == -1) {
            break;
        }

        line = line.trim();
        if (line.length() == 0) {
            break;
        }

        Query query = parser.parse(line);
        System.out.println("Searching for: " + query.toString(field));

        if (repeat > 0) { // repeat & time as benchmark
            Date start = new Date();
            for (int i = 0; i < repeat; i++) {
                searcher.search(query, null, 100);
            }
            Date end = new Date();
            System.out.println("Time: " + (end.getTime() - start.getTime()) + "ms");
        }

        doPagingSearch(in, searcher, query, hitsPerPage, raw, queries == null && queryString == null);

        if (queryString != null) {
            break;
        }
    }
    reader.close();
}

From source file:com.ibm.jaql.lang.expr.index.ProbeLuceneFn.java

License:Apache License

@Override
public JsonIterator iter(Context context) throws Exception {
    JsonRecord fd = (JsonRecord) exprs[0].eval(context);
    if (fd == null) {
        return JsonIterator.NULL;
    }/*  w  ww . ja v a 2s.c  o  m*/
    JsonString loc = (JsonString) fd.get(new JsonString("location"));
    if (loc == null) {
        return JsonIterator.NULL;
    }
    JsonString jquery = (JsonString) exprs[1].eval(context);
    if (jquery == null) {
        return JsonIterator.NULL;
    }

    HashSet<String> fields = null;
    JsonIterator iter = exprs[2].iter(context);
    for (JsonValue sv : iter) {
        JsonString s = (JsonString) sv;
        if (s != null) {
            if (fields == null) {
                fields = new HashSet<String>();
            }
            fields.add(s.toString());
        }
    }
    final FieldSelector fieldSelector = (fields == null) ? null
            : new SetBasedFieldSelector(fields, new HashSet<String>());

    final IndexSearcher searcher = new IndexSearcher(loc.toString());
    Analyzer analyzer = new StandardAnalyzer();
    QueryParser qp = new QueryParser("key", analyzer);
    Query query = qp.parse(jquery.toString());

    query = searcher.rewrite(query);
    final Scorer scorer = query.weight(searcher).scorer(searcher.getIndexReader());
    final BufferedJsonRecord rec = new BufferedJsonRecord();
    final JsonString jdoc = new JsonString("doc");
    final MutableJsonLong jdocid = new MutableJsonLong();

    return new JsonIterator(rec) {
        @Override
        public boolean moveNext() throws Exception {
            if (!scorer.next()) {
                return false;
            }
            rec.clear();
            int i = scorer.doc();
            jdocid.set(i);
            rec.add(jdoc, jdocid);
            if (fieldSelector != null) {
                Document doc = searcher.doc(i, fieldSelector);
                for (Object x : doc.getFields()) {
                    Field f = (Field) x;
                    String name = f.name();
                    byte[] val = f.binaryValue();
                    ByteArrayInputStream bais = new ByteArrayInputStream(val); // TODO: reuse
                    DataInputStream in = new DataInputStream(bais); // TODO: reuse
                    JsonValue ival = serializer.read(in, null);
                    rec.add(new JsonString(name), ival);
                }
            }
            return true; // currentValue == rec
        }
    };
}

From source file:com.ibm.watson.developer_cloud.professor_languo.pipeline.primary_search.LuceneSearcher.java

License:Open Source License

@Override
public void initialize(Properties properties) throws SearchException {
    String resDirPath = properties.getProperty(ConfigurationConstants.INGESTION_BASE_DIR) + File.separator;
    if (properties.getProperty(ConfigurationConstants.INDEX_DIR_TYPE)
            .equals(ConfigurationConstants.IndexDirTypes.FS.toString())) {
        String indexDirPath = resDirPath + properties.getProperty(ConfigurationConstants.INDEX_DIR);
        try {/*from ww  w. j  a v a 2  s.c  om*/
            Directory indexDir = FSDirectory.open(new File(indexDirPath).toPath());
            indexReader = DirectoryReader.open(indexDir);
            searcher = new IndexSearcher(indexReader);
        } catch (IOException e) {
            throw new SearchException(e);
        }
    } else {
        throw new SearchException(Messages.getString("RetrieveAndRank.LUCENE_SEARCHER_INIT")); //$NON-NLS-1$
    }
    candidateAnswerNum = Integer.parseInt(properties.getProperty(ConfigurationConstants.CANDIDATE_ANSWER_NUM));
}

From source file:com.ibm.watson.developer_cloud.professor_languo.pipeline.primary_search.LuceneSearcher.java

License:Open Source License

/**
 * @deprecated - should only be used in the Unit Tests<br>
 *             Initialize this {@link LuceneSearcher} using a given Lucene directory
 *
 * @param indexDir - The {@link Directory} containing the index
 * @throws SearchException//w w  w  .j  ava  2  s .  co m
 */
private void initialize(Directory indexDir) throws SearchException {
    try {
        indexReader = DirectoryReader.open(indexDir);
        searcher = new IndexSearcher(indexReader);
    } catch (IOException e) {
        throw new SearchException(e);
    }
}

From source file:com.icdd.lucence.SearchFiles.java

License:Apache License

public void searchFiles() {
    // /*from   ww w  .ja  va 2s.  c o m*/
    String index = "F:/download/index/";
    // 
    String field = "contents";
    // 
    String queries = null;
    // 
    String queryString = null;
    int repeat = 0;
    // ????
    boolean raw = false;
    // ?10?
    int hitsPerPage = 10;

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

        BufferedReader in = null;
        if (queries != null) {
            in = Files.newBufferedReader(Paths.get(queries), StandardCharsets.UTF_8);
        } else {
            in = new BufferedReader(new InputStreamReader(System.in, StandardCharsets.UTF_8));
        }
        QueryParser parser = new QueryParser(field, analyzer);
        while (true) {
            if (queryString == null && queries == null) {
                System.out.println("Enter query:");
            }

            String line = queryString != null ? queryString : in.readLine();

            if (line == null || line.length() == -1) {
                break;
            }
            line = line.trim();
            if (line.length() == 0) {
                break;
            }
            try {
                Query query = parser.parse(line);
                System.out.println("Searching for:" + query.toString(field));

                if (repeat > 0) { // repeat & time as benchmark
                    Date start = new Date();
                    for (int i = 0; i < repeat; i++) {
                        searcher.search(query, 100);
                    }
                    Date end = new Date();
                    System.out.println("Time: " + (end.getTime() - start.getTime()) + "ms");
                }

                doPagingSearch(in, searcher, query, hitsPerPage, raw, queries == null && queryString == null);

            } catch (ParseException e) {

                e.printStackTrace();
            }

            if (queryString != null) {
                break;
            }
        }
        reader.close();
    } catch (IOException e) {

        e.printStackTrace();
    }
}