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

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

Introduction

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

Prototype

public TopFieldDocs search(Query query, int n, Sort sort, boolean doDocScores) throws IOException 

Source Link

Document

Search implementation with arbitrary sorting, plus control over whether hit scores and max score should be computed.

Usage

From source file:com.esri.gpt.catalog.lucene.LuceneQueryAdapter.java

License:Apache License

/**
 * Executes a query against a Lucene index.
 * @param discoveryQuery the query to execute
 *//*from   w  w  w  .  j  a v  a  2s .  com*/
protected void executeQuery(DiscoveryQuery discoveryQuery)
        throws DiscoveryException, ParseException, CorruptIndexException, IOException {

    IndexSearcher searcher = null;
    try {

        // initialize
        searcher = getIndexAdapter().newSearcher();
        this.maxDoc = searcher.maxDoc();
        boolean bExecuteQuery = true;
        boolean bProcessHits = true;
        RequestContext reqContext = this.getIndexAdapter().getRequestContext();
        BooleanQuery rootQuery = new BooleanQuery();
        DiscoveryFilter discoveryFilter = discoveryQuery.getFilter();
        DiscoveryResult discoveryResult = discoveryQuery.getResult();
        Discoverables returnables = discoveryQuery.getReturnables();
        if ((returnables == null) || (returnables.size() == 0) || (discoveryFilter.getMaxRecords() <= 0)) {
            bProcessHits = false;
        }

        // CSW query provider options
        boolean isDublinCoreResponse = true;
        boolean isBriefResponse = false;
        boolean isSummaryResponse = false;
        QueryOptions cswQueryOptions = (QueryOptions) reqContext.getObjectMap()
                .get("com.esri.gpt.server.csw.provider.components.QueryOptions");

        // build the query (if no query was supplied, we'll query everything)
        LogicalClauseAdapter logicalAdapter = new LogicalClauseAdapter(this);
        LogicalClause rootClause = discoveryFilter.getRootClause();
        if ((rootClause == null) || (rootClause.getClauses().size() == 0)) {
            if (discoveryFilter.getMaxRecords() <= QUERYALL_THRESHOLD) {
                LOGGER.finer("No filter was supplied, querying all...");
                logicalAdapter.appendSelectAll(rootQuery);
            } else {
                LOGGER.finer("No filter was supplied, query will not be executed.");
                bExecuteQuery = false;
            }
        } else {
            logicalAdapter.adaptLogicalClause(rootQuery, rootClause);
            if ((rootQuery.clauses() == null) && (rootQuery.clauses().size() > 0)) {
                bExecuteQuery = false;
            }
        }
        if (!bExecuteQuery)
            return;

        // execute the query and process the hits if required

        // set the sort option
        Sort sortOption = null;
        if (bProcessHits && (searcher.maxDoc() > 0)) {
            sortOption = makeSortOption(discoveryQuery);
        }

        // filters
        Filter filter = null;

        // make the access control filter
        MetadataAcl acl = new MetadataAcl(reqContext);
        AuthenticationStatus auth = reqContext.getUser().getAuthenticationStatus();
        boolean bAdmin = auth.getAuthenticatedRoles().hasRole("gptAdministrator");
        if (!bAdmin && !acl.isPolicyUnrestricted()) {
            String[] aclValues = acl.makeUserAcl();
            filter = new AclFilter(Storeables.FIELD_ACL, aclValues);
        }

        // isPartOf filter
        filter = IsPartOfFilter.make(reqContext, filter);

        // make the schema filter
        if (cswQueryOptions != null) {
            String schemaName = Val.chkStr(cswQueryOptions.getSchemaFilter());
            if (schemaName.length() > 0) {
                filter = new SchemaFilter(schemaName, filter);
                isDublinCoreResponse = cswQueryOptions.isDublinCoreResponse();
                if (!isDublinCoreResponse) {
                    String elementSetType = Val.chkStr(cswQueryOptions.getElementSetType());
                    if (elementSetType.equalsIgnoreCase("brief")) {
                        isBriefResponse = true;
                    } else if (elementSetType.equalsIgnoreCase("summary")) {
                        isSummaryResponse = true;
                    }
                }
            }
        }

        // determine the start/end positions
        int startRecord = discoveryFilter.getStartRecord() - 1;
        int maxRecords = discoveryFilter.getMaxRecords();
        if (startRecord < 0)
            startRecord = 0;
        int recordsPerPage = maxRecords;
        if (recordsPerPage <= 0)
            recordsPerPage = 1;
        int hitsToReturn = startRecord + recordsPerPage;
        int nextRecord = 0;
        int numDocs = 0;

        // execute the query 
        LOGGER.finer("Executing Lucene Query:\n" + rootQuery);
        TopDocs topDocs = null;
        if (filter != null) {
            if (sortOption != null) {
                topDocs = searcher.search(rootQuery, filter, hitsToReturn, sortOption);
            } else {
                topDocs = searcher.search(rootQuery, filter, hitsToReturn);
            }
        } else {
            if (sortOption != null) {
                topDocs = searcher.search(rootQuery, filter, hitsToReturn, sortOption);
            } else {
                topDocs = searcher.search(rootQuery, hitsToReturn);
            }
        }

        // determine the hit count
        int totalHits = topDocs.totalHits;
        ScoreDoc[] scoreDocs = topDocs.scoreDocs;
        if ((scoreDocs != null) && (scoreDocs.length) > 0) {
            numDocs = scoreDocs.length;
            if (totalHits > numDocs) {
                nextRecord = numDocs + 1;
            }
        }
        discoveryResult.setNumberOfHits(totalHits);
        LOGGER.finer("Total query hits: " + totalHits);

        if (startRecord > (totalHits - 1))
            bProcessHits = false;
        if (maxRecords <= 0)
            bProcessHits = false;
        int nTotal = numDocs - startRecord;
        if (!bProcessHits)
            return;

        // warn if many records were requested
        if (nTotal >= TOOMANY_WARNING_THRESHOLD) {
            LOGGER.warning("A request to process " + nTotal
                    + " discovery records was recieved and will be exceuted.\n" + discoveryQuery.toString());
        }

        // process the hits, build the results
        LOGGER.finer("Processing " + nTotal + " records from: " + (startRecord + 1) + " to: " + numDocs);
        Storeable storeable;
        DiscoveredRecords records = discoveryResult.getRecords();
        IndexReader reader = searcher.getIndexReader();
        for (int i = startRecord; i < numDocs; i++) {
            ScoreDoc scoreDoc = scoreDocs[i];
            Document document = reader.document(scoreDoc.doc);
            DiscoveredRecord record = new DiscoveredRecord();

            // Dublin Core based responses
            if (isDublinCoreResponse) {
                for (Discoverable target : returnables) {
                    ArrayList<Object> values = new ArrayList<Object>();
                    storeable = (Storeable) target.getStorable();

                    if (storeable instanceof AnyTextProperty) {
                        values = null;

                    } else if (storeable instanceof GeometryProperty) {
                        GeometryProperty geom = (GeometryProperty) storeable;
                        values.add(geom.readEnvelope(document));

                    } else if (target.getMeaning().getMeaningType().equals(PropertyMeaningType.XMLURL)) {
                        String uuid = document.get(Storeables.FIELD_UUID);
                        uuid = URLEncoder.encode(uuid, "UTF-8");
                        values.add("?getxml=" + uuid);

                    } else {
                        DatastoreField retrievalField = storeable.getRetrievalField();
                        Field[] fields = document.getFields(retrievalField.getName());
                        if (fields != null) {
                            for (Field f : fields) {
                                Object value = retrievalField.makeValueToReturn(f.stringValue());
                                values.add(value);
                            }
                        }
                    }

                    if (values != null) {
                        Object[] oValues = null;
                        if (values.size() >= 0)
                            oValues = values.toArray();
                        record.addField(target, oValues);
                    }
                }

                // non Dublin Core based responses
            } else {
                String responseXml = null;
                if (isBriefResponse && (responseXml == null)) {
                    Field field = document.getField(Storeables.FIELD_XML_BRIEF);
                    if (field != null) {
                        responseXml = field.stringValue();
                    }
                } else if (isSummaryResponse && (responseXml == null)) {
                    Field field = document.getField(Storeables.FIELD_XML_SUMMARY);
                    if (field != null) {
                        responseXml = field.stringValue();
                    }
                } else if (responseXml == null) {
                    Field field = document.getField(Storeables.FIELD_XML);
                    if (field != null) {
                        responseXml = field.stringValue();
                    }
                }
                record.setResponseXml(responseXml);
            }

            records.add(record);
        }
        int nPopulated = records.size();
        LOGGER.finer("Populated " + nPopulated + " records.");

    } finally {
        getIndexAdapter().closeSearcher(searcher);
    }
}

From source file:com.esri.gpt.server.assertion.handler.AsnCommentHandler.java

License:Apache License

/**
 * Queries comments./* w w w.ja v  a  2s. c om*/
 * @param context the assertion operation context
 * @throws Exception if an exception occurs
 */
private void query(AsnContext context) throws Exception {

    // initialize
    AsnOperation operation = context.getOperation();
    AsnAssertionSet asnSet = operation.getAssertionSet();
    AsnValueType vType = asnSet.getValueType();
    String subject = operation.getSubject().getURN();
    String predicate = vType.getRdfPredicate();

    // build a query to match all occurrences of the subject/predicate pair
    BooleanQuery query = new BooleanQuery();
    Query qSubject = new TermQuery(new Term(AsnConstants.FIELD_RDF_SUBJECT, subject));
    Query qPredicate = new TermQuery(new Term(AsnConstants.FIELD_RDF_PREDICATE, predicate));
    query.add(qSubject, BooleanClause.Occur.MUST);
    query.add(qPredicate, BooleanClause.Occur.MUST);

    // sort on descending timestamp
    String tsField = AsnConstants.FIELD_SYS_TIMESTAMP;
    Sort sortOption = new Sort(new SortField(tsField, SortField.STRING, true));

    // determine the start and end positions
    int startRecord = context.getRequestOptions().getStartRecord() - 1;
    int maxRecords = context.getRequestOptions().getMaxRecords();
    if (startRecord < 0)
        startRecord = 0;
    int recordsPerPage = maxRecords;
    if (recordsPerPage <= 0)
        recordsPerPage = 1;
    int hitsToReturn = startRecord + recordsPerPage;
    int nextRecord = 0;
    int numDocs = 0;

    IndexReader reader = null;
    IndexSearcher searcher = null;
    try {

        // make the reader and searcher, execute the search
        reader = this.getIndexAdapter().makeIndexReader();
        searcher = new IndexSearcher(reader);
        TopDocs topDocs = searcher.search(query, null, hitsToReturn, sortOption);
        ScoreDoc[] scoreDocs = null;
        int totalHits = topDocs.totalHits;
        if (maxRecords > 0) {
            scoreDocs = topDocs.scoreDocs;
            if ((scoreDocs != null) && (scoreDocs.length) > 0) {
                numDocs = scoreDocs.length;
                if (totalHits > numDocs) {
                    nextRecord = numDocs + 1;
                }
            }
        }

        // root property for the response
        String rootSubject = subject;
        String roorPredicate = operation.getPredicate().getURN() + "response";
        AsnProperty rootProp = new AsnProperty(rootSubject, roorPredicate, null);

        // hit count and next record
        String queryPfx = asnSet.getURNPrefix() + ":query";
        rootProp.getChildren().add(new AsnProperty(null, queryPfx + ":hits", "" + totalHits));
        if (nextRecord > 0) {
            rootProp.getChildren().add(new AsnProperty(null, queryPfx + ":nextRecord", "" + nextRecord));
        }

        // canCreate capability for the active user
        String canCreatePred = asnSet.getURNPrefix() + ":activeUser:canCreate";
        String canCreateVal = "" + context.getAuthorizer().canCreate(context, asnSet.getAuthPolicy());
        rootProp.getChildren().add(new AsnProperty(null, canCreatePred, canCreateVal));

        // process the documents, generate the response
        AsnAssertionRenderer renderer = new AsnAssertionRenderer();
        for (int i = startRecord; i < numDocs; i++) {
            Document document = reader.document(scoreDocs[i].doc);
            Assertion assertion = asnSet.newAssertion(context, false);
            assertion.load(document);
            rootProp.getChildren().add(renderer.makeProperty(context, assertion));
        }
        context.getOperationResponse().generateResponse(context, rootProp.getChildren());

    } finally {
        this.getIndexAdapter().closeReader(reader);
        this.getIndexAdapter().closeSearcher(searcher);
    }

}

From source file:com.leavesfly.lia.advsearching.FunctionQueryTest.java

License:Apache License

public void testRecency() throws Throwable {
    Directory dir = TestUtil.getBookIndexDirectory();
    IndexReader r = IndexReader.open(dir);
    IndexSearcher s = new IndexSearcher(r);
    s.setDefaultFieldSortScoring(true, true);

    QueryParser parser = new QueryParser(Version.LUCENE_30, "contents",
            new StandardAnalyzer(Version.LUCENE_30));
    Query q = parser.parse("java in action"); // #A
    Query q2 = new RecencyBoostingQuery(q, // #B
            2.0, 2 * 365, "pubmonthAsDay");
    Sort sort = new Sort(new SortField[] { SortField.FIELD_SCORE, new SortField("title2", SortField.STRING) });
    TopDocs hits = s.search(q2, null, 5, sort);

    for (int i = 0; i < hits.scoreDocs.length; i++) {
        Document doc = r.document(hits.scoreDocs[i].doc);
        System.out.println((1 + i) + ": " + doc.get("title") + ": pubmonth=" + doc.get("pubmonth") + " score="
                + hits.scoreDocs[i].score);
    }/*from  w ww . jav a 2 s .c  o m*/
    s.close();
    r.close();
    dir.close();
}

From source file:com.leavesfly.lia.advsearching.SortingExample.java

License:Apache License

public void displayResults(Query query, Sort sort) // #1
        throws IOException {
    IndexSearcher searcher = new IndexSearcher(directory);

    searcher.setDefaultFieldSortScoring(true, false); // #2

    TopDocs results = searcher.search(query, null, // #3
            20, sort); // #3

    System.out.println("\nResults for: " + // #4
            query.toString() + " sorted by " + sort);

    System.out.println(StringUtils.rightPad("Title", 30) + StringUtils.rightPad("pubmonth", 10)
            + StringUtils.center("id", 4) + StringUtils.center("score", 15));

    PrintStream out = new PrintStream(System.out, true, "UTF-8"); // #5

    DecimalFormat scoreFormatter = new DecimalFormat("0.######");
    for (ScoreDoc sd : results.scoreDocs) {
        int docID = sd.doc;
        float score = sd.score;
        Document doc = searcher.doc(docID);
        out.println(StringUtils.rightPad( // #6
                StringUtils.abbreviate(doc.get("title"), 29), 30) + // #6
                StringUtils.rightPad(doc.get("pubmonth"), 10) + // #6
                StringUtils.center("" + docID, 4) + // #6
                StringUtils.leftPad( // #6
                        scoreFormatter.format(score), 12)); // #6
        out.println("   " + doc.get("category"));
        // out.println(searcher.explain(query, docID)); // #7
    }//from   w w w .  j  a  v a2 s .c  om

    searcher.close();
}

From source file:com.leavesfly.lia.tool.SpatialLuceneExample.java

License:Apache License

public void findNear(String what, double latitude, double longitude, double radius)
        throws CorruptIndexException, IOException {
    IndexSearcher searcher = new IndexSearcher(directory);

    DistanceQueryBuilder dq;/*w w w  .  j ava 2  s.  co m*/
    dq = new DistanceQueryBuilder(latitude, // #A
            longitude, // #A
            radius, // #A
            latField, // #A
            lngField, // #A
            tierPrefix, // #A
            true); // #A

    Query tq;
    if (what == null)
        tq = new TermQuery(new Term("metafile", "doc")); // #B
    else
        tq = new TermQuery(new Term("name", what));

    DistanceFieldComparatorSource dsort; // #C
    dsort = new DistanceFieldComparatorSource( // #C
            dq.getDistanceFilter()); // #C
    Sort sort = new Sort(new SortField("foo", dsort)); // #C

    TopDocs hits = searcher.search(tq, dq.getFilter(), 10, sort);

    Map<Integer, Double> distances = // #D
            dq.getDistanceFilter().getDistances(); // #D

    System.out.println("Number of results: " + hits.totalHits);
    System.out.println("Found:");
    for (ScoreDoc sd : hits.scoreDocs) {
        int docID = sd.doc;
        Document d = searcher.doc(docID);

        String name = d.get("name");
        double rsLat = NumericUtils.prefixCodedToDouble(d.get(latField));
        double rsLng = NumericUtils.prefixCodedToDouble(d.get(lngField));
        Double geo_distance = distances.get(docID);

        System.out.printf(name + ": %.2f Miles\n", geo_distance);
        System.out.println("\t\t(" + rsLat + "," + rsLng + ")");
    }
}

From source file:com.mathworks.xzheng.advsearching.FunctionQueryTest.java

License:Apache License

public void testRecency() throws Throwable {
    Directory dir = TestUtil.getBookIndexDirectory();
    IndexReader r = IndexReader.open(dir);
    IndexSearcher s = new IndexSearcher(r);
    s.setDefaultFieldSortScoring(true, true);

    QueryParser parser = new QueryParser(Version.LUCENE_46, "contents",
            new StandardAnalyzer(Version.LUCENE_46));
    Query q = parser.parse("java in action"); // #A
    Query q2 = new RecencyBoostingQuery(q, // #B
            2.0, 2 * 365, "pubmonthAsDay");
    Sort sort = new Sort(new SortField[] { SortField.FIELD_SCORE, new SortField("title2", SortField.STRING) });
    TopDocs hits = s.search(q2, null, 5, sort);

    for (int i = 0; i < hits.scoreDocs.length; i++) {
        Document doc = r.document(hits.scoreDocs[i].doc);
        System.out.println((1 + i) + ": " + doc.get("title") + ": pubmonth=" + doc.get("pubmonth") + " score="
                + hits.scoreDocs[i].score);
    }// w w  w  .j  a va2s.  co m
    s.close();
    r.close();
    dir.close();
}

From source file:com.mathworks.xzheng.advsearching.SortingExample.java

License:Apache License

public void displayResults(Query query, Sort sort) // #1
        throws IOException {
    IndexSearcher searcher = new IndexSearcher(DirectoryReader.open(directory));

    searcher.setDefaultFieldSortScoring(true, false); // #2

    TopDocs results = searcher.search(query, null, // #3
            20, sort); // #3

    System.out.println("\nResults for: " + // #4
            query.toString() + " sorted by " + sort);

    System.out.println(StringUtils.rightPad("Title", 30) + StringUtils.rightPad("pubmonth", 10)
            + StringUtils.center("id", 4) + StringUtils.center("score", 15));

    PrintStream out = new PrintStream(System.out, true, "UTF-8"); // #5

    DecimalFormat scoreFormatter = new DecimalFormat("0.######");
    for (ScoreDoc sd : results.scoreDocs) {
        int docID = sd.doc;
        float score = sd.score;
        Document doc = searcher.doc(docID);
        out.println(StringUtils.rightPad( // #6
                StringUtils.abbreviate(doc.get("title"), 29), 30) + // #6
                StringUtils.rightPad(doc.get("pubmonth"), 10) + // #6
                StringUtils.center("" + docID, 4) + // #6
                StringUtils.leftPad( // #6
                        scoreFormatter.format(score), 12)); // #6
        out.println("   " + doc.get("category"));
        //out.println(searcher.explain(query, docID));   // #7
    }/*from  ww w  . ja v  a  2 s.c  o m*/

}

From source file:com.mathworks.xzheng.tools.SpatialLuceneExample.java

License:Apache License

public void findNear(String what, double latitude, double longitude, double radius)
        throws CorruptIndexException, IOException {
    IndexSearcher searcher = new IndexSearcher(DirectoryReader.open(directory));

    DistanceQueryBuilder dq;//from w  w w .  jav  a2s . c  om
    dq = new DistanceQueryBuilder(latitude, // #A
            longitude, // #A
            radius, // #A
            latField, // #A
            lngField, // #A
            tierPrefix, // #A
            true); // #A

    Query tq;
    if (what == null)
        tq = new TermQuery(new Term("metafile", "doc")); // #B
    else
        tq = new TermQuery(new Term("name", what));

    DistanceFieldComparatorSource dsort; // #C
    dsort = new DistanceFieldComparatorSource( // #C
            dq.getDistanceFilter()); // #C
    Sort sort = new Sort(new SortField("foo", dsort)); // #C

    TopDocs hits = searcher.search(tq, dq.getFilter(), 10, sort);

    Map<Integer, Double> distances = // #D
            dq.getDistanceFilter().getDistances(); // #D

    System.out.println("Number of results: " + hits.totalHits);
    System.out.println("Found:");
    for (ScoreDoc sd : hits.scoreDocs) {
        int docID = sd.doc;
        Document d = searcher.doc(docID);

        String name = d.get("name");
        double rsLat = NumericUtils.prefixCodedToDouble(d.get(latField));
        double rsLng = NumericUtils.prefixCodedToDouble(d.get(lngField));
        Double geo_distance = distances.get(docID);

        System.out.printf(name + ": %.2f Miles\n", geo_distance);
        System.out.println("\t\t(" + rsLat + "," + rsLng + ")");
    }
}