List of usage examples for org.apache.lucene.search FuzzyQuery FuzzyQuery
public FuzzyQuery(Term term, int maxEdits)
From source file:com.dp2345.service.impl.SearchServiceImpl.java
License:Open Source License
@SuppressWarnings("unchecked") @Transactional(readOnly = true)/*ww w . j a v a 2 s.com*/ 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(Version.LUCENE_35, "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.STRING, true), new SortField(null, SortField.SCORE), new SortField("createDate", SortField.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.dp2345.service.impl.SearchServiceImpl.java
License:Open Source License
@SuppressWarnings("unchecked") @Transactional(readOnly = true)/*w w w. jav a2 s .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(Version.LUCENE_35, "keyword", new IKAnalyzer()).parse(text); QueryParser nameParser = new QueryParser(Version.LUCENE_35, "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.DOUBLE, false), new SortField("createDate", SortField.LONG, true) }; } else if (orderType == OrderType.priceDesc) { sortFields = new SortField[] { new SortField("price", SortField.DOUBLE, true), new SortField("createDate", SortField.LONG, true) }; } else if (orderType == OrderType.salesDesc) { sortFields = new SortField[] { new SortField("sales", SortField.INT, true), new SortField("createDate", SortField.LONG, true) }; } else if (orderType == OrderType.scoreDesc) { sortFields = new SortField[] { new SortField("score", SortField.INT, true), new SortField("createDate", SortField.LONG, true) }; } else if (orderType == OrderType.dateDesc) { sortFields = new SortField[] { new SortField("createDate", SortField.LONG, true) }; } else { sortFields = new SortField[] { new SortField("isTop", SortField.STRING, true), new SortField(null, SortField.SCORE), new SortField("modifyDate", SortField.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.hyeb.service.SearchServiceImpl.java
License:Open Source License
@SuppressWarnings("unchecked") @Transactional(readOnly = true)// w w w.j a v a 2 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)//from w w w .j a va2s . c o m 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.mysema.query.lucene.LuceneExpressions.java
License:Apache License
/** * Create a fuzzy query/* w w w. j av a 2s . co m*/ * * @param path * @param value * @param maxEdits * @return */ public static BooleanExpression fuzzyLike(Path<String> path, String value, int maxEdits) { Term term = new Term(path.getMetadata().getName(), value); return new QueryElement(new FuzzyQuery(term, maxEdits)); }
From source file:com.mysema.query.lucene.LuceneUtils.java
License:Apache License
/** * Create a fuzzy query// ww w . jav a 2s .com * * @param path * @param value * @param minimumSimilarity * @return */ public static BooleanExpression fuzzyLike(Path<String> path, String value, float minimumSimilarity) { Term term = new Term(path.getMetadata().getExpression().toString(), value); return new QueryElement(new FuzzyQuery(term, minimumSimilarity)); }
From source file:com.querydsl.lucene3.LuceneExpressions.java
License:Apache License
/** * Create a fuzzy query//ww w . j a va 2s .c o m * * @param path path * @param value value to match * @param minimumSimilarity a value between 0 and 1 to set the required similarity * @return condition */ public static BooleanExpression fuzzyLike(Path<String> path, String value, float minimumSimilarity) { Term term = new Term(path.getMetadata().getName(), value); return new QueryElement(new FuzzyQuery(term, minimumSimilarity)); }
From source file:kr.debop4j.search.hibernate.query.FuzzyQueryTest.java
License:Apache License
@Test public void testFuzzyQuery() throws Exception { buildIndex(fts);/*from www .j ava 2 s .c om*/ try { String userInput = "title"; FuzzyQuery luceneQuery = new FuzzyQuery(new Term("title", userInput), 0.4f); log.debug("Query=" + luceneQuery.toString()); FullTextQuery ftq = fts.createFullTextQuery(luceneQuery, Dvd.class); List<Dvd> results = ftq.list(); Assertions.assertThat(results.size()).isEqualTo(5); // Assertions.assertThat(results.get(0).getTitle()).isEqualTo(titles[0]); for (Dvd dvd : results) { log.debug("Title=" + dvd.getTitle()); } } finally { for (Object element : fts.createQuery("from " + Dvd.class.getName()).list()) { fts.delete(element); } } }
From source file:kr.debop4j.search.hibernate.query.FuzzyQueryTest.java
License:Apache License
@Test public void testKoreanFuzzyQuery() throws Exception { buildIndex(fts);/* w w w .j av a 2 s . co m*/ try { // //TODO: ? FuzzyQuery ? . // String userInput = "?"; FuzzyQuery luceneQuery = new FuzzyQuery(new Term("title", userInput), 0.4f); log.debug("Query=" + luceneQuery.toString()); FullTextQuery ftq = fts.createFullTextQuery(luceneQuery, Dvd.class); List<Dvd> results = ftq.list(); // Assertions.assertThat(results.size()).isEqualTo(3); // Assertions.assertThat(results.get(0).getTitle()).isEqualTo(titles[7]); for (Dvd dvd : results) { log.debug("Title=" + dvd.getTitle()); } } finally { for (Object element : fts.createQuery("from " + Dvd.class.getName()).list()) { fts.delete(element); } } }
From source file:org.codelibs.elasticsearch.index.query.SimpleQueryParser.java
License:Apache License
/** * Dispatches to Lucene's SimpleQueryParser's newFuzzyQuery, optionally * lowercasing the term first//from ww w. ja v a 2s . c o m */ @Override public Query newFuzzyQuery(String text, int fuzziness) { BooleanQuery.Builder bq = new BooleanQuery.Builder(); bq.setDisableCoord(true); for (Map.Entry<String, Float> entry : weights.entrySet()) { final String fieldName = entry.getKey(); try { final BytesRef term = getAnalyzer().normalize(fieldName, text); Query query = new FuzzyQuery(new Term(fieldName, term), fuzziness); bq.add(wrapWithBoost(query, entry.getValue()), BooleanClause.Occur.SHOULD); } catch (RuntimeException e) { rethrowUnlessLenient(e); } } return super.simplify(bq.build()); }