List of usage examples for org.apache.lucene.search Sort Sort
public Sort(SortField... fields)
From source file:org.apache.oodt.cas.workflow.instrepo.LuceneWorkflowInstanceRepository.java
License:Apache License
public int getNumWorkflowInstancesByStatus(String status) throws InstanceRepositoryException { IndexSearcher searcher = null;//from w w w. j a v a 2 s . com int numInsts = -1; try { reader = DirectoryReader.open(indexDir); } catch (IOException e) { e.printStackTrace(); } try { searcher = new IndexSearcher(reader); Term instIdTerm = new Term("workflow_inst_status", status); org.apache.lucene.search.Query query = new TermQuery(instIdTerm); Sort sort = new Sort(new SortField("workflow_inst_startdatetime", SortField.Type.STRING, true)); TopDocs topDocs = searcher.search(query, 1, sort); numInsts = topDocs.totalHits; } catch (IOException e) { LOG.log(Level.WARNING, "IOException when opening index directory: [" + idxFilePath + "] for search: Message: " + e.getMessage()); throw new InstanceRepositoryException(e.getMessage()); } finally { if (searcher != null) { try { //TODO Shutdown searcher } catch (Exception ignore) { } } } return numInsts; }
From source file:org.apache.oodt.cas.workflow.instrepo.LuceneWorkflowInstanceRepository.java
License:Apache License
public List getWorkflowInstances() throws InstanceRepositoryException { IndexSearcher searcher = null;/*from w w w .jav a 2 s.c o m*/ List wInsts = null; try { reader = DirectoryReader.open(indexDir); } catch (IOException e) { e.printStackTrace(); } try { searcher = new IndexSearcher(reader); Term instIdTerm = new Term("myfield", "myvalue"); org.apache.lucene.search.Query query = new TermQuery(instIdTerm); Sort sort = new Sort(new SortField("workflow_inst_startdatetime", SortField.Type.STRING, true)); TopDocs check = searcher.search(query, 1, sort); if (check.totalHits > 0) { TopDocs topDocs = searcher.search(query, check.totalHits, sort); ScoreDoc[] hits = topDocs.scoreDocs; if (topDocs.totalHits > 0) { wInsts = new Vector(hits.length); for (ScoreDoc hit : hits) { Document doc = searcher.doc(hit.doc); WorkflowInstance wInst = toWorkflowInstance(doc); wInsts.add(wInst); } } } } catch (IOException e) { LOG.log(Level.WARNING, "IOException when opening index directory: [" + idxFilePath + "] for search: Message: " + e.getMessage()); throw new InstanceRepositoryException(e.getMessage()); } finally { if (searcher != null) { try { //TODO Shutdown searcher } catch (Exception ignore) { } } } return wInsts; }
From source file:org.apache.oodt.cas.workflow.instrepo.LuceneWorkflowInstanceRepository.java
License:Apache License
public List getWorkflowInstancesByStatus(String status) throws InstanceRepositoryException { IndexSearcher searcher = null;/*from w w w .j av a2 s . c o m*/ List wInsts = null; try { reader = DirectoryReader.open(indexDir); } catch (IOException e) { e.printStackTrace(); } try { searcher = new IndexSearcher(reader); Term instIdTerm = new Term("workflow_inst_status", status); org.apache.lucene.search.Query query = new TermQuery(instIdTerm); Sort sort = new Sort(new SortField("workflow_inst_startdatetime", SortField.Type.STRING, true)); TopDocs check = searcher.search(query, 1, sort); if (check.totalHits > 0) { TopDocs topDocs = searcher.search(query, check.totalHits, sort); ScoreDoc[] hits = topDocs.scoreDocs; if (hits.length > 0) { wInsts = new Vector(hits.length); for (ScoreDoc hit : hits) { Document doc = searcher.doc(hit.doc); WorkflowInstance wInst = toWorkflowInstance(doc); wInsts.add(wInst); } } } } catch (IOException e) { LOG.log(Level.WARNING, "IOException when opening index directory: [" + idxFilePath + "] for search: Message: " + e.getMessage()); throw new InstanceRepositoryException(e.getMessage()); } finally { if (searcher != null) { try { //TODO Shutdown searcher } catch (Exception ignore) { } } } return wInsts; }
From source file:org.apache.oodt.cas.workflow.instrepo.LuceneWorkflowInstanceRepository.java
License:Apache License
protected List paginateWorkflows(int pageNum, String status) throws InstanceRepositoryException { List instIds = null;/*from w w w . j av a 2 s. c o m*/ IndexSearcher searcher = null; try { reader = DirectoryReader.open(indexDir); } catch (IOException e) { e.printStackTrace(); } try { searcher = new IndexSearcher(reader); // construct a Boolean query here BooleanQuery.Builder booleanQuery = new BooleanQuery.Builder(); Term instIdTerm = new Term("myfield", "myvalue"); if (status != null) { Term statusTerm = new Term("workflow_inst_status", status); booleanQuery.add(new TermQuery(statusTerm), BooleanClause.Occur.MUST); } booleanQuery.add(new TermQuery(instIdTerm), BooleanClause.Occur.MUST); Sort sort = new Sort(new SortField("workflow_inst_startdatetime", SortField.Type.STRING, true)); LOG.log(Level.FINE, "Querying LuceneWorkflowInstanceRepository: q: [" + booleanQuery + "]"); TopDocs check = searcher.search(booleanQuery.build(), 1, sort); if (check.totalHits > 0) { TopDocs topDocs = searcher.search(booleanQuery.build(), check.totalHits, sort); ScoreDoc[] hits = topDocs.scoreDocs; if (hits.length > 0) { int startNum = (pageNum - 1) * pageSize; if (startNum > hits.length) { startNum = 0; } instIds = new Vector(pageSize); for (int i = startNum; i < Math.min(hits.length, (startNum + pageSize)); i++) { Document instDoc = searcher.doc(hits[i].doc); WorkflowInstance inst = toWorkflowInstance(instDoc); instIds.add(inst.getId()); } } else { LOG.log(Level.WARNING, "No workflow instances found " + "when attempting to paginate!"); } } } catch (IOException e) { LOG.log(Level.WARNING, "IOException when opening index directory: [" + idxFilePath + "] for search: Message: " + e.getMessage()); throw new InstanceRepositoryException(e.getMessage()); } finally { if (searcher != null) { try { //TODO Shutdown searcher } catch (Exception ignore) { } } } return instIds; }
From source file:org.apache.solr.core.QueryResultKeyTest.java
License:Apache License
@Test public void testFiltersHashCode() { // the hashcode should be the same even when the list // of filters is in a different order Sort sort = new Sort(new SortField("test", SortField.Type.BYTE)); List<Query> filters = new ArrayList<Query>(); filters.add(new TermQuery(new Term("test", "field"))); filters.add(new TermQuery(new Term("test2", "field2"))); BooleanQuery query = new BooleanQuery(); query.add(new TermQuery(new Term("test", "field")), Occur.MUST); QueryResultKey qrk1 = new QueryResultKey(query, filters, sort, 1); List<Query> filters2 = new ArrayList<Query>(); filters2.add(new TermQuery(new Term("test2", "field2"))); filters2.add(new TermQuery(new Term("test", "field"))); QueryResultKey qrk2 = new QueryResultKey(query, filters2, sort, 1); assertEquals(qrk1.hashCode(), qrk2.hashCode()); }
From source file:org.apache.solr.handler.BlobHandler.java
License:Apache License
@Override public void handleRequestBody(final SolrQueryRequest req, SolrQueryResponse rsp) throws Exception { String httpMethod = (String) req.getContext().get("httpMethod"); String path = (String) req.getContext().get("path"); SolrConfigHandler.setWt(req, "json"); List<String> pieces = StrUtils.splitSmart(path, '/'); String blobName = null;//from w w w .j a v a 2 s . c o m if (pieces.size() >= 3) blobName = pieces.get(2); if ("POST".equals(httpMethod)) { if (blobName == null || blobName.isEmpty()) { rsp.add("error", "Name not found"); return; } String err = SolrConfigHandler.validateName(blobName); if (err != null) { log.warn("no blob name"); rsp.add("error", err); return; } if (req.getContentStreams() == null) { log.warn("no content stream"); rsp.add("error", "No stream"); return; } for (ContentStream stream : req.getContentStreams()) { ByteBuffer payload = SimplePostTool.inputStreamToByteArray(stream.getStream(), maxSize); MessageDigest m = MessageDigest.getInstance("MD5"); m.update(payload.array(), payload.position(), payload.limit()); String md5 = new BigInteger(1, m.digest()).toString(16); TopDocs duplicate = req.getSearcher().search(new TermQuery(new Term("md5", md5)), 1); if (duplicate.totalHits > 0) { rsp.add("error", "duplicate entry"); req.forward(null, new MapSolrParams( (Map) makeMap("q", "md5:" + md5, "fl", "id,size,version,timestamp,blobName")), rsp); log.warn("duplicate entry for blob :" + blobName); return; } TopFieldDocs docs = req.getSearcher().search(new TermQuery(new Term("blobName", blobName)), 1, new Sort(new SortField("version", SortField.Type.LONG, true))); long version = 0; if (docs.totalHits > 0) { StoredDocument doc = req.getSearcher().doc(docs.scoreDocs[0].doc); Number n = doc.getField("version").numericValue(); version = n.longValue(); } version++; String id = blobName + "/" + version; Map<String, Object> doc = makeMap("id", id, "md5", md5, "blobName", blobName, "version", version, "timestamp", new Date(), "size", payload.limit(), "blob", payload); verifyWithRealtimeGet(blobName, version, req, doc); log.info(MessageFormat.format("inserting new blob {0} ,size {1}, md5 {2}", doc.get("id"), String.valueOf(payload.limit()), md5)); indexMap(req, rsp, doc); log.info(" Successfully Added and committed a blob with id {} and size {} ", id, payload.limit()); break; } } else { int version = -1; if (pieces.size() > 3) { try { version = Integer.parseInt(pieces.get(3)); } catch (NumberFormatException e) { rsp.add("error", "Invalid version" + pieces.get(3)); return; } } if (ReplicationHandler.FILE_STREAM.equals(req.getParams().get(CommonParams.WT))) { if (blobName == null) { throw new SolrException(SolrException.ErrorCode.NOT_FOUND, "Please send the request in the format /blob/<blobName>/<version>"); } else { String q = "blobName:{0}"; if (version != -1) q = "id:{0}/{1}"; QParser qparser = QParser.getParser(MessageFormat.format(q, blobName, version), "lucene", req); final TopDocs docs = req.getSearcher().search(qparser.parse(), 1, new Sort(new SortField("version", SortField.Type.LONG, true))); if (docs.totalHits > 0) { rsp.add(ReplicationHandler.FILE_STREAM, new SolrCore.RawWriter() { @Override public void write(OutputStream os) throws IOException { StoredDocument doc = req.getSearcher().doc(docs.scoreDocs[0].doc); StorableField sf = doc.getField("blob"); FieldType fieldType = req.getSchema().getField("blob").getType(); ByteBuffer buf = (ByteBuffer) fieldType.toObject(sf); if (buf == null) { //should never happen unless a user wrote this document directly throw new SolrException(SolrException.ErrorCode.NOT_FOUND, "Invalid document . No field called blob"); } else { os.write(buf.array(), 0, buf.limit()); } } }); } else { throw new SolrException(SolrException.ErrorCode.NOT_FOUND, MessageFormat.format("Invalid combination of blobName {0} and version {1}", blobName, String.valueOf(version))); } } } else { String q = "*:*"; if (blobName != null) { q = "blobName:{0}"; if (version != -1) { q = "id:{0}/{1}"; } } req.forward(null, new MapSolrParams((Map) makeMap("q", MessageFormat.format(q, blobName, version), "fl", "id,size,version,timestamp,blobName,md5", "sort", "version desc")), rsp); } } }
From source file:org.apache.solr.handler.component.QueryElevationComponent.java
License:Apache License
@Override public void prepare(ResponseBuilder rb) throws IOException { SolrQueryRequest req = rb.req;/*from w ww . j a va 2s .c om*/ SolrParams params = req.getParams(); // A runtime param can skip if (!params.getBool(QueryElevationParams.ENABLE, true)) { return; } boolean exclusive = params.getBool(QueryElevationParams.EXCLUSIVE, false); // A runtime parameter can alter the config value for forceElevation boolean force = params.getBool(QueryElevationParams.FORCE_ELEVATION, forceElevation); boolean markExcludes = params.getBool(QueryElevationParams.MARK_EXCLUDES, false); Query query = rb.getQuery(); String qstr = rb.getQueryString(); if (query == null || qstr == null) { return; } qstr = getAnalyzedQuery(qstr); IndexReader reader = req.getSearcher().getIndexReader(); ElevationObj booster = null; try { booster = getElevationMap(reader, req.getCore()).get(qstr); } catch (Exception ex) { throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Error loading elevation", ex); } if (booster != null) { rb.req.getContext().put(BOOSTED, booster.ids); // Change the query to insert forced documents if (exclusive == true) { //we only want these results rb.setQuery(booster.include); } else { BooleanQuery newq = new BooleanQuery(true); newq.add(query, BooleanClause.Occur.SHOULD); newq.add(booster.include, BooleanClause.Occur.SHOULD); if (booster.exclude != null) { if (markExcludes == false) { for (TermQuery tq : booster.exclude) { newq.add(new BooleanClause(tq, BooleanClause.Occur.MUST_NOT)); } } else { //we are only going to mark items as excluded, not actually exclude them. This works //with the EditorialMarkerFactory rb.req.getContext().put(EXCLUDED, booster.excludeIds); } } rb.setQuery(newq); } ElevationComparatorSource comparator = new ElevationComparatorSource(booster); // if the sort is 'score desc' use a custom sorting method to // insert documents in their proper place SortSpec sortSpec = rb.getSortSpec(); if (sortSpec.getSort() == null) { sortSpec.setSort(new Sort(new SortField[] { new SortField("_elevate_", comparator, true), new SortField(null, SortField.Type.SCORE, false) })); } else { // Check if the sort is based on score SortField[] current = sortSpec.getSort().getSort(); Sort modified = this.modifySort(current, force, comparator); if (modified != null) { sortSpec.setSort(modified); } } // alter the sorting in the grouping specification if there is one GroupingSpecification groupingSpec = rb.getGroupingSpec(); if (groupingSpec != null) { SortField[] groupSort = groupingSpec.getGroupSort().getSort(); Sort modGroupSort = this.modifySort(groupSort, force, comparator); if (modGroupSort != null) { groupingSpec.setGroupSort(modGroupSort); } SortField[] withinGroupSort = groupingSpec.getSortWithinGroup().getSort(); Sort modWithinGroupSort = this.modifySort(withinGroupSort, force, comparator); if (modWithinGroupSort != null) { groupingSpec.setSortWithinGroup(modWithinGroupSort); } } } // Add debugging information if (rb.isDebug()) { List<String> match = null; if (booster != null) { // Extract the elevated terms into a list match = new ArrayList<String>(booster.priority.size()); for (Object o : booster.include.clauses()) { TermQuery tq = (TermQuery) ((BooleanClause) o).getQuery(); match.add(tq.getTerm().text()); } } SimpleOrderedMap<Object> dbg = new SimpleOrderedMap<Object>(); dbg.add("q", qstr); dbg.add("match", match); if (rb.isDebugQuery()) { rb.addDebugInfo("queryBoosting", dbg); } } }
From source file:org.apache.solr.handler.component.QueryElevationComponent.java
License:Apache License
private Sort modifySort(SortField[] current, boolean force, ElevationComparatorSource comparator) { boolean modify = false; ArrayList<SortField> sorts = new ArrayList<SortField>(current.length + 1); // Perhaps force it to always sort by score if (force && current[0].getType() != SortField.Type.SCORE) { sorts.add(new SortField("_elevate_", comparator, true)); modify = true;//www .j ava 2 s. com } for (SortField sf : current) { if (sf.getType() == SortField.Type.SCORE) { sorts.add(new SortField("_elevate_", comparator, !sf.getReverse())); modify = true; } sorts.add(sf); } return modify ? new Sort(sorts.toArray(new SortField[sorts.size()])) : null; }
From source file:org.apache.solr.search.QueryParsing.java
License:Apache License
/** * Returns null if the sortSpec is the standard sort desc. * <p/>// w ww . j a va 2 s . c o m * <p> * The form of the sort specification string currently parsed is: * </p> * <pre> * SortSpec ::= SingleSort [, SingleSort]* * SingleSort ::= <fieldname> SortDirection * SortDirection ::= top | desc | bottom | asc * </pre> * Examples: * <pre> * score desc #normal sort by score (will return null) * weight bottom #sort by weight ascending * weight desc #sort by weight descending * height desc,weight desc #sort by height descending, and use weight descending to break any ties * height desc,weight asc #sort by height descending, using weight ascending as a tiebreaker * </pre> */ public static Sort parseSort(String sortSpec, SolrQueryRequest req) { if (sortSpec == null || sortSpec.length() == 0) return null; List<SortField> lst = new ArrayList<SortField>(4); try { StrParser sp = new StrParser(sortSpec); while (sp.pos < sp.end) { sp.eatws(); final int start = sp.pos; // short circuit test for a really simple field name String field = sp.getId(null); Exception qParserException = null; if (field == null || !Character.isWhitespace(sp.peekChar())) { // let's try it as a function instead field = null; String funcStr = sp.val.substring(start); QParser parser = QParser.getParser(funcStr, FunctionQParserPlugin.NAME, req); Query q = null; try { if (parser instanceof FunctionQParser) { FunctionQParser fparser = (FunctionQParser) parser; fparser.setParseMultipleSources(false); fparser.setParseToEnd(false); q = fparser.getQuery(); if (fparser.localParams != null) { if (fparser.valFollowedParams) { // need to find the end of the function query via the string parser int leftOver = fparser.sp.end - fparser.sp.pos; sp.pos = sp.end - leftOver; // reset our parser to the same amount of leftover } else { // the value was via the "v" param in localParams, so we need to find // the end of the local params themselves to pick up where we left off sp.pos = start + fparser.localParamsEnd; } } else { // need to find the end of the function query via the string parser int leftOver = fparser.sp.end - fparser.sp.pos; sp.pos = sp.end - leftOver; // reset our parser to the same amount of leftover } } else { // A QParser that's not for function queries. // It must have been specified via local params. q = parser.getQuery(); assert parser.getLocalParams() != null; sp.pos = start + parser.localParamsEnd; } Boolean top = sp.getSortDirection(); if (null != top) { // we have a Query and a valid direction if (q instanceof FunctionQuery) { lst.add(((FunctionQuery) q).getValueSource().getSortField(top)); } else { lst.add((new QueryValueSource(q, 0.0f)).getSortField(top)); } continue; } } catch (Exception e) { // hang onto this in case the string isn't a full field name either qParserException = e; } } // if we made it here, we either have a "simple" field name, // or there was a problem parsing the string as a complex func/quer if (field == null) { // try again, simple rules for a field name with no whitespace sp.pos = start; field = sp.getSimpleString(); } Boolean top = sp.getSortDirection(); if (null == top) { throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Can't determine a Sort Order (asc or desc) in sort spec " + sp); } if (SCORE.equals(field)) { if (top) { lst.add(SortField.FIELD_SCORE); } else { lst.add(new SortField(null, SortField.Type.SCORE, true)); } } else if (DOCID.equals(field)) { lst.add(new SortField(null, SortField.Type.DOC, top)); } else { // try to find the field SchemaField sf = req.getSchema().getFieldOrNull(field); if (null == sf) { if (null != qParserException) { throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "sort param could not be parsed as a query, and is not a " + "field that exists in the index: " + field, qParserException); } throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "sort param field can't be found: " + field); } lst.add(sf.getSortField(top)); } } } catch (SyntaxError e) { throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "error in sort: " + sortSpec, e); } // normalize a sort on score desc to null if (lst.size() == 1 && lst.get(0) == SortField.FIELD_SCORE) { return null; } return new Sort(lst.toArray(new SortField[lst.size()])); }
From source file:org.apache.solr.search.SortSpecParsing.java
License:Apache License
private static SortSpec parseSortSpecImpl(String sortSpec, IndexSchema schema, SolrQueryRequest optionalReq) { if (sortSpec == null || sortSpec.length() == 0) return newEmptySortSpec(); List<SortField> sorts = new ArrayList<>(4); List<SchemaField> fields = new ArrayList<>(4); try {//from w ww . j a v a 2s .c o m StrParser sp = new StrParser(sortSpec); while (sp.pos < sp.end) { sp.eatws(); final int start = sp.pos; // short circuit test for a really simple field name String field = sp.getId(null); Exception qParserException = null; if ((field == null || !Character.isWhitespace(sp.peekChar())) && (optionalReq != null)) { // let's try it as a function instead field = null; String funcStr = sp.val.substring(start); QParser parser = QParser.getParser(funcStr, FunctionQParserPlugin.NAME, optionalReq); Query q = null; try { if (parser instanceof FunctionQParser) { FunctionQParser fparser = (FunctionQParser) parser; fparser.setParseMultipleSources(false); fparser.setParseToEnd(false); q = fparser.getQuery(); if (fparser.localParams != null) { if (fparser.valFollowedParams) { // need to find the end of the function query via the string parser int leftOver = fparser.sp.end - fparser.sp.pos; sp.pos = sp.end - leftOver; // reset our parser to the same amount of leftover } else { // the value was via the "v" param in localParams, so we need to find // the end of the local params themselves to pick up where we left off sp.pos = start + fparser.localParamsEnd; } } else { // need to find the end of the function query via the string parser int leftOver = fparser.sp.end - fparser.sp.pos; sp.pos = sp.end - leftOver; // reset our parser to the same amount of leftover } } else { // A QParser that's not for function queries. // It must have been specified via local params. q = parser.getQuery(); assert parser.getLocalParams() != null; sp.pos = start + parser.localParamsEnd; } Boolean top = sp.getSortDirection(); if (null != top) { // we have a Query and a valid direction if (q instanceof FunctionQuery) { sorts.add(((FunctionQuery) q).getValueSource().getSortField(top)); } else { sorts.add((new QueryValueSource(q, 0.0f)).getSortField(top)); } fields.add(null); continue; } } catch (Exception e) { // hang onto this in case the string isn't a full field name either qParserException = e; } } // if we made it here, we either have a "simple" field name, // or there was a problem parsing the string as a complex func/quer if (field == null) { // try again, simple rules for a field name with no whitespace sp.pos = start; field = sp.getSimpleString(); } Boolean top = sp.getSortDirection(); if (null == top) { throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Can't determine a Sort Order (asc or desc) in sort spec " + sp); } if (SCORE.equals(field)) { if (top) { sorts.add(SortField.FIELD_SCORE); } else { sorts.add(new SortField(null, SortField.Type.SCORE, true)); } fields.add(null); } else if (DOCID.equals(field)) { sorts.add(new SortField(null, SortField.Type.DOC, top)); fields.add(null); } else { // try to find the field SchemaField sf = schema.getFieldOrNull(field); if (null == sf) { if (null != qParserException) { throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "sort param could not be parsed as a query, and is not a " + "field that exists in the index: " + field, qParserException); } throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "sort param field can't be found: " + field); } sorts.add(sf.getSortField(top)); fields.add(sf); } } } catch (SyntaxError e) { throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "error in sort: " + sortSpec, e); } // normalize a sort on score desc to null if (sorts.size() == 1 && sorts.get(0) == SortField.FIELD_SCORE) { return newEmptySortSpec(); } Sort s = new Sort(sorts.toArray(new SortField[sorts.size()])); return new SortSpec(s, fields); }