public QueryParser getParser(Analyzer a) throws Exception {
   if (a == null) a = new MockAnalyzer(random(), MockTokenizer.SIMPLE, true);
   QueryParser qp = new QueryParser(getDefaultField(), a);
   qp.setDefaultOperator(QueryParserBase.OR_OPERATOR);
   qp.setSplitOnWhitespace(splitOnWhitespace);
   return qp;
 }
Example #2
0
 public SearchIndex() throws Exception {
   //        parser = new QueryParser(Version.LUCENE_36, QUERY_NAME, analyzer);
   parser = new QueryParser(Version.LUCENE_36, QUERY_NAME, analyzer);
   parser.setDefaultOperator(QueryParser.AND_OPERATOR);
   //        luceneService = new NRT("test1");
   //        luceneService = new NRTLuceneService("test1");
   luceneService = new NRTLuceneService();
 }
 /** simple CJK synonym test */
 public void testCJKSynonym() throws Exception {
   Query expected = new SynonymQuery(new Term("field", "国"), new Term("field", "國"));
   QueryParser qp = new QueryParser("field", new MockCJKSynonymAnalyzer());
   assertEquals(expected, qp.parse("国"));
   qp.setDefaultOperator(Operator.AND);
   assertEquals(expected, qp.parse("国"));
   expected = new BoostQuery(expected, 2f);
   assertEquals(expected, qp.parse("国^2"));
 }
 /** synonyms with default AND operator */
 public void testCJKSynonymsAND() throws Exception {
   BooleanQuery.Builder expectedB = new BooleanQuery.Builder();
   expectedB.add(new TermQuery(new Term("field", "中")), BooleanClause.Occur.MUST);
   Query inner = new SynonymQuery(new Term("field", "国"), new Term("field", "國"));
   expectedB.add(inner, BooleanClause.Occur.MUST);
   Query expected = expectedB.build();
   QueryParser qp = new QueryParser("field", new MockCJKSynonymAnalyzer());
   qp.setDefaultOperator(Operator.AND);
   assertEquals(expected, qp.parse("中国"));
   expected = new BoostQuery(expected, 2f);
   assertEquals(expected, qp.parse("中国^2"));
 }
 /** simple synonyms test */
 public void testSynonyms() throws Exception {
   Query expected = new SynonymQuery(new Term("field", "dogs"), new Term("field", "dog"));
   QueryParser qp = new QueryParser("field", new MockSynonymAnalyzer());
   assertEquals(expected, qp.parse("dogs"));
   assertEquals(expected, qp.parse("\"dogs\""));
   qp.setDefaultOperator(Operator.AND);
   assertEquals(expected, qp.parse("dogs"));
   assertEquals(expected, qp.parse("\"dogs\""));
   expected = new BoostQuery(expected, 2f);
   assertEquals(expected, qp.parse("dogs^2"));
   assertEquals(expected, qp.parse("\"dogs\"^2"));
 }
 /** forms multiphrase query */
 public void testCJKSynonymsPhrase() throws Exception {
   MultiPhraseQuery.Builder expectedQBuilder = new MultiPhraseQuery.Builder();
   expectedQBuilder.add(new Term("field", "中"));
   expectedQBuilder.add(new Term[] {new Term("field", "国"), new Term("field", "國")});
   QueryParser qp = new QueryParser("field", new MockCJKSynonymAnalyzer());
   qp.setDefaultOperator(Operator.AND);
   assertEquals(expectedQBuilder.build(), qp.parse("\"中国\""));
   Query expected = new BoostQuery(expectedQBuilder.build(), 2f);
   assertEquals(expected, qp.parse("\"中国\"^2"));
   expectedQBuilder.setSlop(3);
   expected = new BoostQuery(expectedQBuilder.build(), 2f);
   assertEquals(expected, qp.parse("\"中国\"~3^2"));
 }
 public LuceneSearch_queryparser() throws IOException {
   if (searcher == null) {
     Directory dir =
         FSDirectory.open(
             new File("/Users/chanvain/Documents/workspace/NGPODCollector/index")); // 打开索引文件
     IndexReader reader = DirectoryReader.open(dir);
     System.out.println(reader.getDocCount("title")); // 统计数量
     searcher = new IndexSearcher(reader);
     String[] fields = {"title", "description"}; // 定义要搜索的两个field
     parser = new MultiFieldQueryParser(fields, new StandardAnalyzer()); // 定义parser
     parser.setDefaultOperator(QueryParserBase.AND_OPERATOR); // 定义两个field出现的情况,这里是都要出现
   }
 }
 /** forms multiphrase query */
 public void testSynonymsPhrase() throws Exception {
   MultiPhraseQuery.Builder expectedQBuilder = new MultiPhraseQuery.Builder();
   expectedQBuilder.add(new Term(FIELD, "old"));
   expectedQBuilder.add(new Term[] {new Term(FIELD, "dogs"), new Term(FIELD, "dog")});
   QueryParser qp = new QueryParser(FIELD, new MockSynonymAnalyzer());
   assertEquals(expectedQBuilder.build(), qp.parse("\"old dogs\""));
   qp.setDefaultOperator(Operator.AND);
   assertEquals(expectedQBuilder.build(), qp.parse("\"old dogs\""));
   BoostQuery expected = new BoostQuery(expectedQBuilder.build(), 2f);
   assertEquals(expected, qp.parse("\"old dogs\"^2"));
   expectedQBuilder.setSlop(3);
   expected = new BoostQuery(expectedQBuilder.build(), 2f);
   assertEquals(expected, qp.parse("\"old dogs\"~3^2"));
 }
 private List<T> search(
     final String queryString,
     final int pageNumber,
     final int pageSize,
     final PagingStrategy paging) {
   final QueryParser parser = new QueryParser(DEFAULT_FIELD, new StandardAnalyzer());
   parser.setAllowLeadingWildcard(true);
   parser.setDefaultOperator(Operator.AND);
   final FullTextSession searchSession = getSearchSession();
   try {
     final FullTextQuery query =
         searchSession.createFullTextQuery(parser.parse(queryString), this.type);
     paging.setup(query, pageNumber, pageSize);
     return query.list();
   } catch (final ParseException e) {
     throw new EGOVRuntimeException("invalid.search.string", e);
   }
 }
 @Override
 public void setDefaultOperatorAND(CommonQueryParserConfiguration cqpC) {
   assert (cqpC instanceof QueryParser);
   QueryParser qp = (QueryParser) cqpC;
   qp.setDefaultOperator(Operator.AND);
 }