List of usage examples for org.apache.lucene.search Sort Sort
public Sort(SortField... fields)
From source file:com.khepry.frackhem.entities.Toxicities.java
License:Apache License
public QueryResult queryViaLucene(String queryField, String queryValue, Integer maxDocs, String sortOrder, Boolean allowLeadingWildcard) throws IOException, ParseException { QueryResult queryResult = new QueryResult(); list.clear();/*from w ww . j a v a2 s. co m*/ documents.clear(); String message; String[] sortColumns = sortOrder.split(","); if (!initialized) { queryResult = initialize(indexFolderPath, taxonomyFolderPath); } if (!queryResult.isNotValid()) { QueryParser parser = new QueryParser(Version.LUCENE_44, queryField, analyzer); parser.setAllowLeadingWildcard(allowLeadingWildcard); Query query = parser.parse(queryValue); SortField[] sortFields = new SortField[sortColumns.length]; String[] pieces; for (int i = 0; i < sortColumns.length; i++) { pieces = sortColumns[i].split(":"); if (pieces.length > 1) { switch (pieces[1].toLowerCase()) { case "integer": sortFields[i] = new SortField(sortColumns[i], SortField.Type.INT); break; default: sortFields[i] = new SortField(sortColumns[i], SortField.Type.STRING); break; } } else { sortFields[i] = new SortField(sortColumns[i], SortField.Type.STRING); } } Sort sort = new Sort(sortFields); queryResult.setTopFieldCollector(TopFieldCollector.create(sort, maxDocs, true, false, false, false)); List<FacetRequest> facetRequests = new ArrayList<>(); facetRequests.add(new CountFacetRequest(new CategoryPath("toxRecognized", "CasEdfId"), 200)); facetRequests.add(new CountFacetRequest(new CategoryPath("toxRecognized", "Toxicity"), 200)); facetRequests.add(new CountFacetRequest(new CategoryPath("toxSuspected", "CasEdfId"), 200)); facetRequests.add(new CountFacetRequest(new CategoryPath("toxSuspected", "Toxicity"), 200)); FacetSearchParams facetSearchParams = new FacetSearchParams(facetRequests); queryResult.setFacetsCollector(FacetsCollector.create(facetSearchParams, indexReader, taxonomyReader)); indexSearcher.search(query, MultiCollector.wrap(queryResult.getTopFieldCollector(), queryResult.getFacetsCollector())); List<Document> documents = new ArrayList<>(); for (ScoreDoc scoreDoc : queryResult.getTopFieldCollector().topDocs().scoreDocs) { Document document = indexSearcher.doc(scoreDoc.doc); documents.add(document); Toxicity toxicity = new Toxicity(document); list.add((E) toxicity); if (outputDebugInfo) { for (IndexableField field : document.getFields()) { System.out.print(field.stringValue()); System.out.print("\t"); } System.out.println(); } } queryResult.setDocuments(documents); if (outputDebugInfo) { for (FacetResult facetResult : queryResult.getFacetsCollector().getFacetResults()) { for (FacetResultNode node0 : facetResult.getFacetResultNode().subResults) { if (node0.label.toString().indexOf("/Toxicity/") > -1) { if (node0.label.toString().indexOf(",") == -1) { System.out.println(node0.label + ": " + node0.value); } } else { System.out.println(node0.label + ": " + node0.value); } // for (FacetResultNode node1 : node0.subResults) { // System.out.println(node1.label + ": " + node1.value); // } } } } } return queryResult; }
From source file:com.krawler.spring.auditTrailModule.auditTrailDAOImpl.java
License:Open Source License
/** * * @param requestParams//from w w w . ja v a 2 s.c o m * @return * @throws com.krawler.common.service.ServiceException */ public KwlReturnObject getAuditIndexData(HashMap<String, Object> requestParams) throws ServiceException { int start = 0; int limit = 30; List ll = new ArrayList(); int dl = 0; try { StringBuilder query = new StringBuilder(); String[] searchWithIndex = { AUDIT_INDEX_details, AUDIT_INDEX_userName, AUDIT_INDEX_ipAddr }; String indexPath = storageHandlerImpl.GetAuditTrailIndexPath(); String searchtext = ""; if (requestParams.containsKey("search") && requestParams.get("search") != null) { searchtext = requestParams.get("search").toString().toLowerCase(); if (searchtext.length() > 0) { searchtext = QueryParser.escape(searchtext); if (searchtext.length() > 2) { searchtext = !searchtext.contains(" ") ? "*" + searchtext + "*" : searchtext; //Add '*'(wildcard) for searching on single token } query.append(searchtext); } } String groupid = ""; if (requestParams.containsKey("groupid") && requestParams.get("groupid") != null) { groupid = requestParams.get("groupid").toString(); if (groupid.length() > 0) { if (!groupid.equals(Document)) { // document groupid query.append(query.length() > 0 ? " AND " : ""); query.append("("); query.append(AUDIT_INDEX_auditGroupId); query.append(":"); query.append(groupid); query.append(")"); } } } // check for document group id if yes, then include all related audit actions also String companyid = ""; if (requestParams.containsKey("companyid") && requestParams.get("companyid") != null) { companyid = requestParams.get("companyid").toString(); query.append(query.length() > 0 ? " AND " : ""); query.append("("); query.append(AUDIT_INDEX_companyId); query.append(":"); query.append(companyid); query.append(")"); } if (Document.equals(groupid)) { if (query.length() > 0) { query.append(query.length() > 0 ? " AND " : ""); query.append("(("); query.append(AUDIT_INDEX_auditGroupId); query.append(":"); query.append(groupid); query.append(")"); } if (query.length() > 0) { query.append(query.length() > 0 ? " OR " : ""); query.append("("); query.append(AUDIT_INDEX_action); query.append(":"); query.append(CAMPAIGN_DOC_UPLOAD); query.append(")"); } if (query.length() > 0) { query.append(query.length() > 0 ? " OR " : ""); query.append("("); query.append(AUDIT_INDEX_action); query.append(":"); query.append(LEAD_DOC_UPLOAD); query.append(")"); } if (query.length() > 0) { query.append(query.length() > 0 ? " OR " : ""); query.append("("); query.append(AUDIT_INDEX_action); query.append(":"); query.append(CONTACT_DOC_UPLOAD); query.append(")"); } if (query.length() > 0) { query.append(query.length() > 0 ? " OR " : ""); query.append("("); query.append(AUDIT_INDEX_action); query.append(":"); query.append(PRODUCT_DOC_UPLOAD); query.append(")"); } if (query.length() > 0) { query.append(query.length() > 0 ? " OR " : ""); query.append("("); query.append(AUDIT_INDEX_action); query.append(":"); query.append(ACCOUNT_DOC_UPLOAD); query.append(")"); } if (query.length() > 0) { query.append(query.length() > 0 ? " OR " : ""); query.append("("); query.append(AUDIT_INDEX_action); query.append(":"); query.append(OPPORTUNITY_DOC_UPLOAD); query.append(")"); } if (query.length() > 0) { query.append(query.length() > 0 ? " OR " : ""); query.append("("); query.append(AUDIT_INDEX_action); query.append(":"); query.append(CASE_DOC_UPLOAD); query.append(")"); } if (query.length() > 0) { query.append(query.length() > 0 ? " OR " : ""); query.append("("); query.append(AUDIT_INDEX_action); query.append(":"); query.append(ACTIVITY_DOC_UPLOAD); query.append(")"); } if (query.length() > 0) { query.append(query.length() > 0 ? " OR " : ""); query.append("("); query.append(AUDIT_INDEX_action); query.append(":"); query.append(CAMPAIGN_DOC_DOWNLOAD); query.append(")"); } if (query.length() > 0) { query.append(query.length() > 0 ? " OR " : ""); query.append("("); query.append(AUDIT_INDEX_action); query.append(":"); query.append(LEAD_DOC_DOWNLOAD); query.append(")"); } if (query.length() > 0) { query.append(query.length() > 0 ? " OR " : ""); query.append("("); query.append(AUDIT_INDEX_action); query.append(":"); query.append(CONTACT_DOC_DOWNLOAD); query.append(")"); } if (query.length() > 0) { query.append(query.length() > 0 ? " OR " : ""); query.append("("); query.append(AUDIT_INDEX_action); query.append(":"); query.append(PRODUCT_DOC_DOWNLOAD); query.append(")"); } if (query.length() > 0) { query.append(query.length() > 0 ? " OR " : ""); query.append("("); query.append(AUDIT_INDEX_action); query.append(":"); query.append(ACCOUNT_DOC_DOWNLOAD); query.append(")"); } if (query.length() > 0) { query.append(query.length() > 0 ? " OR " : ""); query.append("("); query.append(AUDIT_INDEX_action); query.append(":"); query.append(OPPORTUNITY_DOC_DOWNLOAD); query.append(")"); } if (query.length() > 0) { query.append(query.length() > 0 ? " OR " : ""); query.append("("); query.append(AUDIT_INDEX_action); query.append(":"); query.append(CASE_DOC_DOWNLOAD); query.append(")"); } if (query.length() > 0) { query.append(query.length() > 0 ? " OR " : ""); query.append("("); query.append(AUDIT_INDEX_action); query.append(":"); query.append(ACTIVITY_DOC_DOWNLOAD); query.append(")"); } if (query.length() > 0) { query.append(query.length() > 0 ? " OR " : ""); query.append("("); query.append(AUDIT_INDEX_action); query.append(":"); query.append(CAMPAIGN_DOC_DELETED); query.append(")"); } if (query.length() > 0) { query.append(query.length() > 0 ? " OR " : ""); query.append("("); query.append(AUDIT_INDEX_action); query.append(":"); query.append(LEAD_DOC_DELETED); query.append(")"); } if (query.length() > 0) { query.append(query.length() > 0 ? " OR " : ""); query.append("("); query.append(AUDIT_INDEX_action); query.append(":"); query.append(CONTACT_DOC_DELETED); query.append(")"); } if (query.length() > 0) { query.append(query.length() > 0 ? " OR " : ""); query.append("("); query.append(AUDIT_INDEX_action); query.append(":"); query.append(PRODUCT_DOC_DELETED); query.append(")"); } if (query.length() > 0) { query.append(query.length() > 0 ? " OR " : ""); query.append("("); query.append(AUDIT_INDEX_action); query.append(":"); query.append(ACCOUNT_DOC_DELETED); query.append(")"); } if (query.length() > 0) { query.append(query.length() > 0 ? " OR " : ""); query.append("("); query.append(AUDIT_INDEX_action); query.append(":"); query.append(OPPORTUNITY_DOC_DELETED); query.append(")"); } if (query.length() > 0) { query.append(query.length() > 0 ? " OR " : ""); query.append("("); query.append(AUDIT_INDEX_action); query.append(":"); query.append(CASE_DOC_DELETED); query.append(")"); } if (query.length() > 0) { query.append(query.length() > 0 ? " OR " : ""); query.append("("); query.append(AUDIT_INDEX_action); query.append(":"); query.append(ACTIVITY_DOC_DELETED); query.append("))"); } } Date fromDate = null; Date toDate = null; if (requestParams.containsKey("frm") && requestParams.get("frm") != null && requestParams.containsKey("to") && requestParams.get("to") != null) { fromDate = new Date(requestParams.get("frm").toString()); toDate = new Date(requestParams.get("to").toString()); query.append(query.length() > 0 ? " AND " : ""); query.append("("); query.append(AUDIT_INDEX_auditTime); query.append(":["); query.append(fromDate.getTime()); query.append(" TO "); query.append(toDate.getTime()); query.append("])"); } if (query.length() > 0) { Sort sort = null; if (requestParams.containsKey("field")) { sort = new Sort(new SortField(requestParams.get("field").toString(), SortField.STRING, requestParams.get("direction").toString().equals("DESC") ? true : false)); } else sort = new Sort(new SortField(AUDIT_INDEX_auditTime, SortField.STRING, true)); if (requestParams.containsKey("start") && requestParams.containsKey("limit") && !StringUtil.isNullOrEmpty(requestParams.get("start").toString())) { start = Integer.parseInt(requestParams.get("start").toString()); limit = Integer.parseInt(requestParams.get("limit").toString()); } Hits hitResult = LuceneSearchObj.searchIndexWithSort(query.toString(), searchWithIndex, indexPath, sort); if (hitResult != null) { dl = hitResult.length(); int count = Math.min(dl, start + limit); String auditUUID; AuditTrail audittrail; for (int i = start; i < count; i++) { org.apache.lucene.document.Document doc = hitResult.doc(i); auditUUID = doc.get(AUDIT_INDEX_transactionId); audittrail = (AuditTrail) get(AuditTrail.class, auditUUID); if (audittrail == null) { count = Math.min(dl, count + 1); } else ll.add(audittrail); } } } } catch (IOException ex) { throw ServiceException.FAILURE(ex.getMessage(), ex); } catch (Exception ex) { throw ServiceException.FAILURE(ex.getMessage(), ex); } return new KwlReturnObject(true, KWLErrorMsgs.S01, "", ll, dl); }
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 .j a v 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 static void main(String[] args) throws Exception { Query allBooks = new MatchAllDocsQuery(); QueryParser parser = new QueryParser(Version.LUCENE_30, // #1 "contents", // #1 new StandardAnalyzer( // #1 Version.LUCENE_30)); // #1 BooleanQuery query = new BooleanQuery(); // #1 query.add(allBooks, BooleanClause.Occur.SHOULD); // #1 query.add(parser.parse("java OR action"), BooleanClause.Occur.SHOULD); // #1 Directory directory = TestUtil.getBookIndexDirectory(); // #2 SortingExample example = new SortingExample(directory); // #2 example.displayResults(query, Sort.RELEVANCE); example.displayResults(query, Sort.INDEXORDER); example.displayResults(query, new Sort(new SortField("category", SortField.STRING))); example.displayResults(query, new Sort(new SortField("pubmonth", SortField.INT, true))); example.displayResults(query, new Sort(new SortField("category", SortField.STRING), SortField.FIELD_SCORE, new SortField("pubmonth", SortField.INT, true))); example.displayResults(query,//from w w w . j av a 2 s . co m new Sort(new SortField[] { SortField.FIELD_SCORE, new SortField("category", SortField.STRING) })); directory.close(); }
From source file:com.leavesfly.lia.extsearch.sorting.DistanceSortingTest.java
License:Apache License
public void testNeareastRestaurantToWork() throws Exception { Sort sort = new Sort(new SortField("unused", new DistanceComparatorSource(10, 10))); TopFieldDocs docs = searcher.search(query, null, 3, sort); // #1 assertEquals(4, docs.totalHits); // #2 assertEquals(3, docs.scoreDocs.length); // #3 FieldDoc fieldDoc = (FieldDoc) docs.scoreDocs[0]; // #4 assertEquals("(10,10) -> (9,6) = sqrt(17)", new Float(Math.sqrt(17)), fieldDoc.fields[0]); // #5 Document document = searcher.doc(fieldDoc.doc); // #6 assertEquals("Los Betos", document.get("name")); // dumpDocs(sort, docs); }
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;//from ww w. java 2s . 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 + ")"); } }
From source file:com.liusoft.dlog4j.search.SearchProxy.java
License:Open Source License
/** * ?/*from w w w .j a v a 2 s . com*/ * @param params * @return * @throws Exception */ public static List search(SearchParameter params) throws Exception { if (params == null) return null; SearchEnabled searching = (SearchEnabled) params.getSearchObject().newInstance(); StringBuffer path = new StringBuffer(_baseIndexPath); path.append(searching.name()); File f = new File(path.toString()); if (!f.exists()) return null; IndexSearcher searcher = new IndexSearcher(path.toString()); //? BooleanQuery comboQuery = new BooleanQuery(); int _query_count = 0; StringTokenizer st = new StringTokenizer(params.getSearchKey()); while (st.hasMoreElements()) { String q = st.nextToken(); String[] indexFields = searching.getIndexFields(); for (int i = 0; i < indexFields.length; i++) { QueryParser qp = new QueryParser(indexFields[i], analyzer); try { Query subjectQuery = qp.parse(q); comboQuery.add(subjectQuery, BooleanClause.Occur.SHOULD); _query_count++; } catch (Exception e) { log.error("Add query parameter failed. key=" + q, e); } } } if (_query_count == 0)//? return null; //?? MultiFilter multiFilter = null; HashMap conds = params.getConditions(); if (conds != null) { Iterator keys = conds.keySet().iterator(); while (keys.hasNext()) { if (multiFilter == null) multiFilter = new MultiFilter(0); String key = (String) keys.next(); multiFilter.add(new FieldFilter(key, conds.get(key).toString())); } } /* * Creates a sort, possibly in reverse, * by terms in the given field with the type of term values explicitly given. */ SortField[] s_fields = new SortField[2]; s_fields[0] = SortField.FIELD_SCORE; s_fields[1] = new SortField(searching.getKeywordField(), SortField.INT, true); Sort sort = new Sort(s_fields); Hits hits = searcher.search(comboQuery, multiFilter, sort); int numResults = hits.length(); //System.out.println(numResults + " found............................"); int result_count = Math.min(numResults, MAX_RESULT_COUNT); List results = new ArrayList(result_count); for (int i = 0; i < result_count; i++) { Document doc = (Document) hits.doc(i); //Java Object result = params.getSearchObject().newInstance(); Enumeration fields = doc.fields(); while (fields.hasMoreElements()) { Field field = (Field) fields.nextElement(); //System.out.println(field.name()+" -- "+field.stringValue()); if (CLASSNAME_FIELD.equals(field.name())) continue; //? if (!field.isStored()) continue; //System.out.println("=========== begin to mapping ============"); //String --> anything Class fieldType = getNestedPropertyType(result, field.name()); //System.out.println(field.name()+", class = " + fieldType.getName()); Object fieldValue = null; if (fieldType.equals(Date.class)) fieldValue = new Date(Long.parseLong(field.stringValue())); else fieldValue = ConvertUtils.convert(field.stringValue(), fieldType); //System.out.println(fieldValue+", class = " + fieldValue.getClass().getName()); setNestedProperty(result, field.name(), fieldValue); } results.add(result); } return results; }
From source file:com.marc.lastweek.business.entities.classifiedad.repository.ClassifiedAdRepository.java
License:Open Source License
@SuppressWarnings("unchecked") public List<ClassifiedAd> indexBasedSearch(FilterParameters parameters, Calendar from, int start, int count) { FullTextQuery fullTextQuery = this.getFullTextQuery(parameters, from); SortField[] sortFields = new SortField[2]; sortFields[0] = new SortField("publicationDate", SortField.STRING, true); sortFields[1] = SortField.FIELD_SCORE; Sort sort = new Sort(sortFields); fullTextQuery.setSort(sort);/*from www . j a v a2s. c o m*/ fullTextQuery.setFirstResult(start); fullTextQuery.setMaxResults(count); try { List<ClassifiedAd> result = fullTextQuery.list(); return result; } catch (IllegalArgumentException e) { return new ArrayList<ClassifiedAd>(); } }
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); }/*from w w w . j a v a 2 s . c om*/ s.close(); r.close(); dir.close(); }
From source file:com.mathworks.xzheng.advsearching.SortingExample.java
License:Apache License
public static void main(String[] args) throws Exception { Query allBooks = new MatchAllDocsQuery(); QueryParser parser = new QueryParser(Version.LUCENE_46, // #1 "contents", // #1 new StandardAnalyzer( // #1 Version.LUCENE_46)); // #1 BooleanQuery query = new BooleanQuery(); // #1 query.add(allBooks, BooleanClause.Occur.SHOULD); // #1 query.add(parser.parse("java OR action"), BooleanClause.Occur.SHOULD); // #1 Directory directory = TestUtil.getBookIndexDirectory(); // #2 SortingExample example = new SortingExample(directory); // #2 example.displayResults(query, Sort.RELEVANCE); example.displayResults(query, Sort.INDEXORDER); example.displayResults(query, new Sort(new SortField("category", SortField.STRING))); example.displayResults(query, new Sort(new SortField("pubmonth", SortField.INT, true))); example.displayResults(query, new Sort(new SortField("category", SortField.STRING), SortField.FIELD_SCORE, new SortField("pubmonth", SortField.INT, true))); example.displayResults(query,/* w w w . ja v a 2s .c o m*/ new Sort(new SortField[] { SortField.FIELD_SCORE, new SortField("category", SortField.STRING) })); directory.close(); }