List of usage examples for org.apache.lucene.search Sort Sort
public Sort(SortField... fields)
From source file:com.hyeb.service.SearchServiceImpl.java
License:Open Source License
@SuppressWarnings("unchecked") @Transactional(readOnly = true)//from w w w .j a v a2 s. c o m public Page<Article> search(String keyword, Pageable pageable) { if (StringUtils.isEmpty(keyword)) { return new Page<Article>(); } if (pageable == null) { pageable = new Pageable(); } try { String text = QueryParser.escape(keyword); QueryParser titleParser = new QueryParser("title", new IKAnalyzer()); titleParser.setDefaultOperator(QueryParser.AND_OPERATOR); Query titleQuery = titleParser.parse(text); FuzzyQuery titleFuzzyQuery = new FuzzyQuery(new Term("title", text), FUZZY_QUERY_MINIMUM_SIMILARITY); Query contentQuery = new TermQuery(new Term("content", text)); Query isPublicationQuery = new TermQuery(new Term("isPublication", "true")); BooleanQuery textQuery = new BooleanQuery(); BooleanQuery query = new BooleanQuery(); textQuery.add(titleQuery, Occur.SHOULD); textQuery.add(titleFuzzyQuery, Occur.SHOULD); textQuery.add(contentQuery, Occur.SHOULD); query.add(isPublicationQuery, Occur.MUST); query.add(textQuery, Occur.MUST); FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(entityManager); FullTextQuery fullTextQuery = fullTextEntityManager.createFullTextQuery(query, Article.class); fullTextQuery.setSort(new Sort(new SortField[] { new SortField("isTop", SortField.Type.STRING, true), new SortField(null, SortField.Type.SCORE), new SortField("createDate", SortField.Type.LONG, true) })); fullTextQuery.setFirstResult((pageable.getPageNumber() - 1) * pageable.getPageSize()); fullTextQuery.setMaxResults(pageable.getPageSize()); return new Page<Article>(fullTextQuery.getResultList(), fullTextQuery.getResultSize(), pageable); } catch (ParseException e) { e.printStackTrace(); } return new Page<Article>(); }
From source file:com.hyeb.service.SearchServiceImpl.java
License:Open Source License
@SuppressWarnings("unchecked") @Transactional(readOnly = true)/*w w w.j av a2s .c om*/ public Page<Product> search(String keyword, BigDecimal startPrice, BigDecimal endPrice, OrderType orderType, Pageable pageable) { if (StringUtils.isEmpty(keyword)) { return new Page<Product>(); } if (pageable == null) { pageable = new Pageable(); } try { String text = QueryParser.escape(keyword); TermQuery snQuery = new TermQuery(new Term("sn", text)); Query keywordQuery = new QueryParser("keyword", new IKAnalyzer()).parse(text); QueryParser nameParser = new QueryParser("name", new IKAnalyzer()); nameParser.setDefaultOperator(QueryParser.AND_OPERATOR); Query nameQuery = nameParser.parse(text); FuzzyQuery nameFuzzyQuery = new FuzzyQuery(new Term("name", text), FUZZY_QUERY_MINIMUM_SIMILARITY); TermQuery introductionQuery = new TermQuery(new Term("introduction", text)); TermQuery isMarketableQuery = new TermQuery(new Term("isMarketable", "true")); TermQuery isListQuery = new TermQuery(new Term("isList", "true")); TermQuery isGiftQuery = new TermQuery(new Term("isGift", "false")); BooleanQuery textQuery = new BooleanQuery(); BooleanQuery query = new BooleanQuery(); textQuery.add(snQuery, Occur.SHOULD); textQuery.add(keywordQuery, Occur.SHOULD); textQuery.add(nameQuery, Occur.SHOULD); textQuery.add(nameFuzzyQuery, Occur.SHOULD); textQuery.add(introductionQuery, Occur.SHOULD); query.add(isMarketableQuery, Occur.MUST); query.add(isListQuery, Occur.MUST); query.add(isGiftQuery, Occur.MUST); query.add(textQuery, Occur.MUST); if (startPrice != null && endPrice != null && startPrice.compareTo(endPrice) > 0) { BigDecimal temp = startPrice; startPrice = endPrice; endPrice = temp; } if (startPrice != null && startPrice.compareTo(new BigDecimal(0)) >= 0 && endPrice != null && endPrice.compareTo(new BigDecimal(0)) >= 0) { NumericRangeQuery<Double> numericRangeQuery = NumericRangeQuery.newDoubleRange("price", startPrice.doubleValue(), endPrice.doubleValue(), true, true); query.add(numericRangeQuery, Occur.MUST); } else if (startPrice != null && startPrice.compareTo(new BigDecimal(0)) >= 0) { NumericRangeQuery<Double> numericRangeQuery = NumericRangeQuery.newDoubleRange("price", startPrice.doubleValue(), null, true, false); query.add(numericRangeQuery, Occur.MUST); } else if (endPrice != null && endPrice.compareTo(new BigDecimal(0)) >= 0) { NumericRangeQuery<Double> numericRangeQuery = NumericRangeQuery.newDoubleRange("price", null, endPrice.doubleValue(), false, true); query.add(numericRangeQuery, Occur.MUST); } FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(entityManager); FullTextQuery fullTextQuery = fullTextEntityManager.createFullTextQuery(query, Product.class); SortField[] sortFields = null; if (orderType == OrderType.priceAsc) { sortFields = new SortField[] { new SortField("price", SortField.Type.DOUBLE, false), new SortField("createDate", SortField.Type.LONG, true) }; } else if (orderType == OrderType.priceDesc) { sortFields = new SortField[] { new SortField("price", SortField.Type.DOUBLE, true), new SortField("createDate", SortField.Type.LONG, true) }; } else if (orderType == OrderType.salesDesc) { sortFields = new SortField[] { new SortField("sales", SortField.Type.INT, true), new SortField("createDate", SortField.Type.LONG, true) }; } else if (orderType == OrderType.scoreDesc) { sortFields = new SortField[] { new SortField("score", SortField.Type.INT, true), new SortField("createDate", SortField.Type.LONG, true) }; } else if (orderType == OrderType.dateDesc) { sortFields = new SortField[] { new SortField("createDate", SortField.Type.LONG, true) }; } else { sortFields = new SortField[] { new SortField("isTop", SortField.Type.STRING, true), new SortField(null, SortField.Type.SCORE), new SortField("modifyDate", SortField.Type.LONG, true) }; } fullTextQuery.setSort(new Sort(sortFields)); fullTextQuery.setFirstResult((pageable.getPageNumber() - 1) * pageable.getPageSize()); fullTextQuery.setMaxResults(pageable.getPageSize()); return new Page<Product>(fullTextQuery.getResultList(), fullTextQuery.getResultSize(), pageable); } catch (Exception e) { e.printStackTrace(); } return new Page<Product>(); }
From source file:com.inkubator.sms.gateway.dao.impl.GroupPhoneDaoImpl.java
@Override public List<GroupPhone> getAllByFullTextService(String parameter, int first, int pageSize, Order order) { FullTextSession fullTextSession = Search.getFullTextSession(getCurrentSession()); Criteria criteria = fullTextSession.createCriteria(GroupPhone.class); Sort sort;// w w w. j a v a 2s .c o m if (order.isAscending()) { sort = new Sort(new SortField(order.getPropertyName(), SortField.STRING_VAL)); } else { sort = new Sort(new SortField(order.getPropertyName(), SortField.STRING_VAL, true)); } FullTextQuery fullTextQuery1 = doSearchFullText(parameter, fullTextSession); fullTextQuery1.setCriteriaQuery(criteria); fullTextQuery1.setFirstResult(first); fullTextQuery1.setMaxResults(pageSize); fullTextQuery1.setSort(sort); return fullTextQuery1.list(); }
From source file:com.inkubator.sms.gateway.dao.impl.LoginHistoryDaoImpl.java
@Override public List<LoginHistory> getAllByFullTextService(String parameter, int minResult, int maxResult, Order order) { System.out.println(parameter + "muahahahhahahha"); FullTextSession fullTextSession = Search.getFullTextSession(getCurrentSession()); org.apache.lucene.search.Sort sort;// w ww. java 2s . c om if (order.isAscending()) { sort = new Sort(new SortField(order.getPropertyName(), SortField.STRING_VAL)); } else { sort = new Sort(new SortField(order.getPropertyName(), SortField.STRING_VAL, true)); } System.out.println("Nilai sorrt " + order.getPropertyName()); FullTextQuery fullTextQuery1 = doSearchFullText(parameter, fullTextSession); fullTextQuery1.setFirstResult(minResult); fullTextQuery1.setMaxResults(maxResult); fullTextQuery1.setSort(sort); return fullTextQuery1.list(); }
From source file:com.inkubator.sms.gateway.dao.impl.PhoneBookDaoImpl.java
@Override public List<PhoneBook> getAllByFullTextService(String parameter, int first, int pageSize, Order order) { FullTextSession fullTextSession = Search.getFullTextSession(getCurrentSession()); Criteria criteria = fullTextSession.createCriteria(getEntityClass()); Sort sort;/*from w w w.j a v a 2s . co m*/ if (order.isAscending()) { sort = new Sort(new SortField(order.getPropertyName(), SortField.STRING_VAL)); } else { sort = new Sort(new SortField(order.getPropertyName(), SortField.STRING_VAL, true)); } FullTextQuery fullTextQuery1 = doSearchFullText(parameter, fullTextSession); fullTextQuery1.setCriteriaQuery(criteria); fullTextQuery1.setFirstResult(first); fullTextQuery1.setMaxResults(pageSize); fullTextQuery1.setSort(sort); return fullTextQuery1.list(); }
From source file:com.inkubator.sms.gateway.dao.impl.RoleDaoImpl.java
@Override public List<Role> getAllByFullTextService(String parameter, int minResult, int maxResult, Order order) { FullTextSession fullTextSession = Search.getFullTextSession(getCurrentSession()); org.apache.lucene.search.Sort sort;//from w w w . java 2 s.com if (order.isAscending()) { sort = new Sort(new SortField(order.getPropertyName(), SortField.STRING_VAL)); } else { sort = new Sort(new SortField(order.getPropertyName(), SortField.STRING_VAL, true)); } System.out.println("Nilai sort " + order.getPropertyName()); FullTextQuery fullTextQuery1 = doSearchFullText(parameter, fullTextSession); fullTextQuery1.setFirstResult(minResult); fullTextQuery1.setMaxResults(maxResult); fullTextQuery1.setSort(sort); // fullTextQuery1.setSort(sort); return fullTextQuery1.list(); }
From source file:com.inkubator.sms.gateway.dao.impl.SmsActivityDaoImpl.java
@Override public List<SmsActivity> getAllByFullTextService(String parameter, int minResult, int maxResult, Order order) { FullTextSession fullTextSession = Search.getFullTextSession(getCurrentSession()); org.apache.lucene.search.Sort sort;/*from w w w .j a v a 2 s. c o m*/ if (order.isAscending()) { sort = new Sort(new SortField(order.getPropertyName(), SortField.STRING_VAL)); } else { sort = new Sort(new SortField(order.getPropertyName(), SortField.STRING_VAL, true)); } System.out.println("Nilai sorrt " + order.getPropertyName()); FullTextQuery fullTextQuery1 = doSearchFullText(parameter, fullTextSession); fullTextQuery1.setFirstResult(minResult); fullTextQuery1.setMaxResults(maxResult); fullTextQuery1.setSort(sort); return fullTextQuery1.list(); }
From source file:com.inkubator.sms.gateway.dao.impl.TaskDefinitionDaoImpl.java
@Override public List<TaskDefinition> getAllByFullTextService(String parameter, int minResult, int maxResult, Order orde) {//from www .j a va 2 s .c om FullTextSession fullTextSession = Search.getFullTextSession(getCurrentSession()); Criteria criteria = fullTextSession.createCriteria(TaskDefinition.class).setFetchMode("modemDefinition", FetchMode.JOIN); org.apache.lucene.search.Sort sort; if (orde.isAscending()) { sort = new Sort(new SortField(orde.getPropertyName(), SortField.STRING_VAL)); } else { sort = new Sort(new SortField(orde.getPropertyName(), SortField.STRING_VAL, true)); } System.out.println("Nilai sorrt " + orde.getPropertyName()); FullTextQuery fullTextQuery1 = doSearchFullText(parameter, fullTextSession); fullTextQuery1.setCriteriaQuery(criteria); fullTextQuery1.setFirstResult(minResult); fullTextQuery1.setMaxResults(maxResult); fullTextQuery1.setSort(sort); return fullTextQuery1.list(); }
From source file:com.inkubator.sms.gateway.dao.impl.UserDaoImpl.java
@Override public List<SmsGatewayUser> getAllByFullTextService(String parameter, int minResult, int maxResult, Order order) {//from w ww. jav a2s . c o m FullTextSession fullTextSession = Search.getFullTextSession(getCurrentSession()); org.apache.lucene.search.Sort sort; if (order.isAscending()) { sort = new Sort(new SortField(order.getPropertyName(), SortField.STRING_VAL)); } else { sort = new Sort(new SortField(order.getPropertyName(), SortField.STRING_VAL, true)); } System.out.println("Nilai sorrt " + order.getPropertyName()); FullTextQuery fullTextQuery1 = doSearchFullText(parameter, fullTextSession); fullTextQuery1.setFirstResult(minResult); fullTextQuery1.setMaxResults(maxResult); fullTextQuery1.setSort(sort); return fullTextQuery1.list(); }
From source file:com.intuit.tank.search.util.SearchUtils.java
License:Open Source License
/** * returns a sort or null if no sort is specified. * /*from w w w . j a va 2 s. c o m*/ * @param searchQuery * the query containing the sorts * @return the lucene sort or null if no sort is specified */ public static final Sort createLuceneSort(SearchQuery searchQuery) { Sort ret = null; Collection<SortOrder> sorts = searchQuery.getSortOrder(); if (!sorts.isEmpty()) { SortField[] array = new SortField[sorts.size()]; int i = 0; for (SortOrder sortOrder : sorts) { array[i] = new SortField(sortOrder.getField(), SortField.STRING, sortOrder.isDescending()); } ret = new Sort(array); } return ret; }