Exemplo n.º 1
0
  public void testModified() throws Exception {
    prepareTestData();
    assertQueryInterceptorPresent(cache2);

    queryParser = createQueryParser("blurb");
    luceneQuery = queryParser.parse("playing");
    cacheQuery = Search.getSearchManager(cache2).getQuery(luceneQuery);

    List<Object> found = cacheQuery.list();

    assert found.size() == 1 : "Expected list of size 1, was of size " + found.size();
    assert found.get(0).equals(person1);

    person1.setBlurb("Likes pizza");
    cache1.put("Navin", person1);

    queryParser = createQueryParser("blurb");
    luceneQuery = queryParser.parse("pizza");
    cacheQuery = Search.getSearchManager(cache2).getQuery(luceneQuery);

    found = cacheQuery.list();

    assert found.size() == 1;
    assert found.get(0).equals(person1);
  }
Exemplo n.º 2
0
 /**
  * Given the search text, searcher object, and query analyzer generate an appropriate Lucene
  * search query.
  */
 protected Query createSearchQuery(
     IndexSearcher searcher, StandardAnalyzer analyzer, String text, List<Integer> namespaces)
     throws IOException, ParseException {
   BooleanQuery fullQuery = new BooleanQuery();
   QueryParser qp;
   // build the namespace portion the query
   if (namespaces != null && !namespaces.isEmpty()) {
     qp = new QueryParser(USE_LUCENE_VERSION, FIELD_TOPIC_NAMESPACE, analyzer);
     StringBuilder namespaceText = new StringBuilder();
     for (Integer namespaceId : namespaces) {
       if (namespaceText.length() != 0) {
         namespaceText.append(" ").append(QueryParser.Operator.OR).append(" ");
       }
       namespaceText.append(namespaceId);
     }
     fullQuery.add(qp.parse(namespaceText.toString()), Occur.MUST);
   }
   // create a sub-query for topic name & topic text
   BooleanQuery nameAndContentQuery = new BooleanQuery();
   // topic name
   qp = new QueryParser(USE_LUCENE_VERSION, FIELD_TOPIC_NAME_ANALYZED, analyzer);
   nameAndContentQuery.add(qp.parse(text), Occur.SHOULD);
   // topic content
   qp = new QueryParser(USE_LUCENE_VERSION, FIELD_TOPIC_CONTENT, analyzer);
   nameAndContentQuery.add(qp.parse(text), Occur.SHOULD);
   // rewrite the sub-query to expand it - required for wildcards to work with highlighter
   Query subQuery = searcher.rewrite(nameAndContentQuery);
   // add the sub-query to the main query
   fullQuery.add(subQuery, Occur.MUST);
   return fullQuery;
 }
  private Query buildLuceneQuery(Long categoryId, Integer page, String q) throws ParseException {
    StringBuilder query = new StringBuilder();

    QueryParser queryParser =
        new org.apache.lucene.queryParser.QueryParser(
            Version.LUCENE_31, "title", new StandardAnalyzer((Version.LUCENE_31)));
    if (StringUtils.isNotBlank(q)) {
      query.append(buildQuery(q));
    }

    if (categoryId != null) {
      List<Long> childCategories =
          DAOFactory.getInstance().getCategoryDAO().childCategories(categoryId);
      if (StringUtils.isNotBlank(q)) {
        query.append("AND ( " + buildQuery(childCategories) + ")");
      } else {
        query.append(buildQuery(childCategories));
      }

      buildQuery(childCategories);
    }
    if (!StringUtils.isNotBlank(query)) {
      query.append("id:*");
    }
    queryParser.setAllowLeadingWildcard(true);
    return queryParser.parse(query.toString());
  }
  public void startSearch(String searchString) throws IOException {

    /*analyze(searchString);*/

    try {
      Directory directory = FSDirectory.open(new File(".//Index")); // где находится индекс
      IndexSearcher is = new IndexSearcher(directory); // объект поиска
      QueryParser parser =
          new QueryParser(
              Version.LUCENE_31,
              "name",
              new RussianAnalyzer(Version.LUCENE_31)); // поле поиска + анализатор
      /* String str1 = "фотоаппарат";
      String str2 = "телевизор";
      String str3 = "SONY";
      String total = "(" + str1 + " OR " + str2 + ")" + " AND " + str3;
      System.out.println(total);*/
      Query query = parser.parse(searchString); // что ищем
      TopDocs results =
          is.search(
              query, null,
              10); // включаем поиск ограничиваемся 10 документами, results содержит ...
      System.out.println(
          "getMaxScore()="
              + results.getMaxScore()
              + " totalHits="
              + results
                  .totalHits); // MaxScore - наилучший результат(приоритет), totalHits - количество
      // найденных документов

      /*proposalController.getProposalList().clear();*/

      for (ScoreDoc hits : results.scoreDocs) { // получаем подсказки
        Document doc = is.doc(hits.doc); // получаем документ по спец сылке doc

        for (Proposal proposal :
            proposalFacade.findPropolsalsByProduct(Long.valueOf(doc.get("recid")))) {

          proposalController.getProposalList().add(proposal);
          _log.info(
              "Предложение найдено:"
                  + proposal.getRecid().toString()
                  + ",Товар: "
                  + doc.get("recid")
                  + ", "
                  + doc.get("name"));
        }

        /*System.out.println("doc="+hits.doc+" score="+hits.score);//выводим спец сылку doc + приоритет
        addMessage(doc.get("id") + " | " + doc.get("recid") + " | " + doc.get("name"));//выводим поля найденного документа*/
      }

      directory.close();
    } catch (ParseException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
    addMessage("Поиск выполнен");
  }
Exemplo n.º 5
0
  public void testDemo() throws IOException, ParseException {

    Analyzer analyzer = new StandardAnalyzer(TEST_VERSION_CURRENT);

    // Store the index in memory:
    Directory directory = new RAMDirectory();
    // To store an index on disk, use this instead:
    // Directory directory = FSDirectory.open("/tmp/testindex");
    IndexWriter iwriter =
        new IndexWriter(directory, analyzer, true, new IndexWriter.MaxFieldLength(25000));
    Document doc = new Document();
    String text = "This is the text to be indexed.";
    doc.add(new Field("fieldname", text, Field.Store.YES, Field.Index.ANALYZED));
    iwriter.addDocument(doc);
    iwriter.close();

    // Now search the index:
    IndexSearcher isearcher = new IndexSearcher(directory, true); // read-only=true
    // Parse a simple query that searches for "text":
    QueryParser parser = new QueryParser(TEST_VERSION_CURRENT, "fieldname", analyzer);
    Query query = parser.parse("text");
    ScoreDoc[] hits = isearcher.search(query, null, 1000).scoreDocs;
    assertEquals(1, hits.length);
    // Iterate through the results:
    for (int i = 0; i < hits.length; i++) {
      Document hitDoc = isearcher.doc(hits[i].doc);
      assertEquals("This is the text to be indexed.", hitDoc.get("fieldname"));
    }
    isearcher.close();
    directory.close();
  }
 @Override
 protected String decorate(String text, MatchAlgorithmReference matchAlgorithm) {
   if (matchAlgorithm
       .getContent()
       .equals(
           StandardMatchAlgorithmReference.CONTAINS
               .getMatchAlgorithmReference()
               .getContent())) {
     text = QueryParser.escape(text);
     StringBuilder sb = new StringBuilder();
     sb.append("(");
     for (String token : text.split("\\s+")) {
       sb.append("description:");
       sb.append(token);
       sb.append("* ");
     }
     sb.append(")");
     sb.append(" OR description:\"" + text + "\"");
     sb.append(" OR exactDescription:\"" + QueryParser.escape(text) + "\"");
     return sb.toString().trim();
   } else if (matchAlgorithm
       .getContent()
       .equals(
           StandardMatchAlgorithmReference.EXACT_MATCH
               .getMatchAlgorithmReference()
               .getContent())) {
     return "exactDescription:\"" + QueryParser.escape(text) + "\"";
   } else if (matchAlgorithm.getContent().equals(LUCENE_QUERY)) {
     return text;
   } else {
     throw new IllegalStateException();
   }
 }
Exemplo n.º 7
0
  public WOActionResults searchAction() {
    String searchString = (String) this.request().formValueForKey("searchString");

    String searchTerm = null;
    if (null != searchString
        && 1 <= searchString.length()
        && searchString.lastIndexOf("~") != searchString.length() - 1) {
      searchTerm = searchString + "~";
    }

    ScoreDoc[] foundScoreDocs = null;
    ERIndex indicatorIndex = ERIndex.indexNamed(Indicator.ENTITY_NAME);
    if (null != searchString && 0 < searchString.length()) {
      try {
        QueryParser contentQueryParser =
            new QueryParser(
                Version.LUCENE_29, "community", new StandardAnalyzer(Version.LUCENE_29));
        Query q = contentQueryParser.parse(searchTerm);
        foundScoreDocs = indicatorIndex.findScoreDocs(q, 10);
      } catch (Exception e) {
        ERXApplication.log.error(
            "Failed to query lucene with " + searchString + " " + e.getMessage());
        e.printStackTrace();
      }
    }

    Search searchPage = (Search) pageWithName(Search.class.getName());
    searchPage.setSearchString(searchString);
    searchPage.setFoundScoreDocs(foundScoreDocs);
    searchPage.setIndicatorIndex(indicatorIndex);
    return searchPage;
  }
Exemplo n.º 8
0
 private final Table values(final String s) throws IOException, ParseException {
   final QueryParser parser = new QueryParser(s, new StandardAnalyzer());
   parser.setAllowLeadingWildcard(true);
   final Query query = parser.parse("*");
   final Set<String> values = new TreeSet<String>();
   try {
     state.search(
         query,
         new HitCollector() {
           @Override
           public final void collect(int doc, float score) {
             try {
               final Document d = state.doc(doc);
               final Field[] fs = d.getFields(s);
               for (int i = 0; i < fs.length; ++i) values.add(fs[i].stringValue());
             } catch (final IOException e) {
               throw new WrapException(e);
             }
           }
         });
   } catch (final WrapException e) {
     throw (IOException) e.getCause();
   }
   return new ArrayTable(
       TableType.FIELD_VALUE, "value", values.toArray(new String[values.size()]));
 }
Exemplo n.º 9
0
  public void testAdded() throws Exception {
    prepareTestData();
    queryParser = createQueryParser("blurb");

    luceneQuery = queryParser.parse("eats");
    cacheQuery = Search.getSearchManager(cache2).getQuery(luceneQuery);
    List<Object> found = cacheQuery.list();

    AssertJUnit.assertEquals(2, found.size());
    assert found.contains(person2);
    assert found.contains(person3);
    assert !found.contains(person4) : "This should not contain object person4";

    person4 = new Person();
    person4.setName("Mighty Goat");
    person4.setBlurb("Also eats grass");

    cache1.put("mighty", person4);

    luceneQuery = queryParser.parse("eats");
    cacheQuery = Search.getSearchManager(cache2).getQuery(luceneQuery);
    found = cacheQuery.list();

    AssertJUnit.assertEquals(3, found.size());
    assert found.contains(person2);
    assert found.contains(person3);
    assert found.contains(person4) : "This should now contain object person4";
  }
Exemplo n.º 10
0
  public TopDocCollectorSearchResult searchByScore(
      String queryStr, int startFrom, String operator) {

    try {
      queryStr = queryStr.trim();
      QueryParser parser = new QueryParser("contents", analyzer);
      if (QueryParser.AND_OPERATOR.toString().equalsIgnoreCase(operator)) {
        parser.setDefaultOperator(QueryParser.AND_OPERATOR);
      } else {
        parser.setDefaultOperator(QueryParser.OR_OPERATOR);
      }
      Query query;
      query = parser.parse(queryStr);
      TopDocCollector collector = doPagingSearch(query, startFrom);
      TopDocCollectorSearchResult result = new TopDocCollectorSearchResult(collector, searcher);
      return result;
    } catch (ParseException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }

    return null;

    // System.out.println("Searching for: " + query.toString("contents"));

    // doPagingSearch(in, searcher, query, hitsPerPage, raw, queries ==
    // null);

    // }
    // reader.close();
  }
Exemplo n.º 11
0
  public TopFieldDocsSearchResult searchBySession(String queryStr, int startFrom, String operator) {

    try {
      queryStr = queryStr.trim();
      QueryParser parser = new QueryParser("contents", analyzer);
      Operator op = QueryParser.AND_OPERATOR;
      if (QueryParser.AND_OPERATOR.toString().equalsIgnoreCase(operator)) {
        parser.setDefaultOperator(QueryParser.AND_OPERATOR);
      } else {
        parser.setDefaultOperator(QueryParser.OR_OPERATOR);
      }

      Query query;
      query = parser.parse(queryStr);
      Sort sort = new Sort("summary", true);

      TopFieldDocs tfd = searcher.search(query, null, startFrom + 10, sort);
      TopFieldDocsSearchResult result = new TopFieldDocsSearchResult(tfd, searcher);
      return result;
    } catch (ParseException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }

    return null;
  }
Exemplo n.º 12
0
 public static CacheQuery createCacheQuery(Cache m_cache, String fieldName, String searchString)
     throws ParseException {
   QueryParser qp = createQueryParser(fieldName);
   Query parsedQuery = qp.parse(searchString);
   SearchManager queryFactory = Search.getSearchManager(m_cache);
   CacheQuery cacheQuery = queryFactory.getQuery(parsedQuery);
   return cacheQuery;
 }
Exemplo n.º 13
0
 public List<Item> search(String query, String indexDir) throws Exception {
   open(indexDir);
   String[] fields = {"title", "content"};
   QueryParser qp = new MultiFieldQueryParser(Version.LUCENE_36, fields, analyzer);
   Query q = qp.parse(query);
   List<Item> result = search(q, 100);
   close();
   return result;
 }
Exemplo n.º 14
0
 /* (non-Javadoc)
  * @see org.apache.lucene.benchmark.quality.QualityQueryParser#parse(org.apache.lucene.benchmark.quality.QualityQuery)
  */
 public Query parse(QualityQuery qq) throws ParseException {
   QueryParser qp = queryParser.get();
   if (qp == null) {
     qp =
         new QueryParser(
             Version.LUCENE_CURRENT, indexField, new StandardAnalyzer(Version.LUCENE_CURRENT));
     queryParser.set(qp);
   }
   return qp.parse(qq.getValue(qqName));
 }
Exemplo n.º 15
0
  protected Hits query(String db, String defaultField, String queryString)
      throws IOException, CorruptIndexException, ParseException {
    Directory directory = FSDirectory.getDirectory(indexPath(db));
    IndexReader reader = IndexReader.open(directory);

    Searcher searcher = new IndexSearcher(reader);
    Analyzer analyzer = new StandardAnalyzer();
    QueryParser qp = new QueryParser(defaultField, analyzer);
    Query query = qp.parse(queryString);
    return searcher.search(query);
  }
Exemplo n.º 16
0
  public void testBatchSize() throws Exception {
    FullTextSession s = Search.getFullTextSession(openSession());
    Transaction tx = s.beginTransaction();
    final int loop = 14;
    s.doWork(
        new Work() {
          @Override
          public void execute(Connection connection) throws SQLException {
            for (int i = 0; i < loop; i++) {
              Statement statmt = connection.createStatement();
              statmt.executeUpdate(
                  "insert into Domain(id, name) values( + " + (i + 1) + ", 'sponge" + i + "')");
              statmt.executeUpdate(
                  "insert into Email(id, title, body, header, domain_id) values( + "
                      + (i + 1)
                      + ", 'Bob Sponge', 'Meet the guys who create the software', 'nope', "
                      + (i + 1)
                      + ")");
              statmt.close();
            }
          }
        });
    tx.commit();
    s.close();

    // check non created object does get found!!1
    s = Search.getFullTextSession(openSession());
    tx = s.beginTransaction();
    ScrollableResults results = s.createCriteria(Email.class).scroll(ScrollMode.FORWARD_ONLY);
    int index = 0;
    while (results.next()) {
      index++;
      s.index(results.get(0));
      if (index % 5 == 0) {
        s.clear();
      }
    }
    tx
        .commit(); // if you get a LazyInitializationException, that's because we clear() the
                   // session in the loop.. it only works with a batch size of 5 (the point of the
                   // test)
    s.clear();
    tx = s.beginTransaction();
    QueryParser parser =
        new QueryParser(TestConstants.getTargetLuceneVersion(), "id", TestConstants.stopAnalyzer);
    List result = s.createFullTextQuery(parser.parse("body:create")).list();
    assertEquals(14, result.size());
    for (Object object : result) {
      s.delete(object);
    }
    tx.commit();
    s.close();
  }
Exemplo n.º 17
0
  public static void wildTest() throws Exception {
    Directory dir = FSDirectory.open(new File(tempDir));
    IndexSearcher searcher = new IndexSearcher(dir); // 来对索引进行检索

    // WildcardQuery query = new WildcardQuery(new Term("path", ("www." + "si\u003fna" + "*" )));
    QueryParser p = new QueryParser(Version.LUCENE_36, "aa", new KeywordAnalyzer());
    Query query = p.parse("path:www." + "s*i\\?na.com");
    System.out.println(query.toString() + "=" + query.getClass());
    TopDocs top = searcher.search(query, 1000);
    ScoreDoc[] sdoc = top.scoreDocs;
    System.out.println(sdoc.length);
    printResult(sdoc, "path", searcher);
  }
Exemplo n.º 18
0
  private int doSearch(String searchPhrase, int maxNumberOfHits, Version luenceVersion)
      throws NullPointerException, ParseException, IOException {
    LOG.trace("*** Search Phrase: {} ***", searchPhrase);

    QueryParser parser = new QueryParser(luenceVersion, "contents", analyzer);
    Query query = parser.parse(searchPhrase);
    TopScoreDocCollector collector = TopScoreDocCollector.create(maxNumberOfHits, true);
    indexSearcher.search(query, collector);
    hits = collector.topDocs().scoreDocs;

    LOG.trace("*** Search generated {} hits ***", hits.length);
    return hits.length;
  }
Exemplo n.º 19
0
  @SuppressWarnings("unchecked")
  public SearchResult<Subject> searchSubjectsBasic(int resultsPerPage, int page, String text) {

    int firstResult = page * resultsPerPage;

    StringBuilder queryBuilder = new StringBuilder();

    if (!StringUtils.isBlank(text)) {
      queryBuilder.append("+(");
      addTokenizedSearchCriteria(queryBuilder, "name", text, false);
      addTokenizedSearchCriteria(queryBuilder, "educationType.name", text, false);
      queryBuilder.append(")");
    }

    EntityManager entityManager = getEntityManager();
    FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(entityManager);

    try {
      String queryString = queryBuilder.toString();
      Query luceneQuery;
      QueryParser parser =
          new QueryParser(Version.LUCENE_29, "", new StandardAnalyzer(Version.LUCENE_29));
      if (StringUtils.isBlank(queryString)) {
        luceneQuery = new MatchAllDocsQuery();
      } else {
        luceneQuery = parser.parse(queryString);
      }

      FullTextQuery query =
          (FullTextQuery)
              fullTextEntityManager
                  .createFullTextQuery(luceneQuery, Subject.class)
                  .setFirstResult(firstResult)
                  .setMaxResults(resultsPerPage);
      query.enableFullTextFilter("ArchivedSubject").setParameter("archived", Boolean.FALSE);

      int hits = query.getResultSize();
      int pages = hits / resultsPerPage;
      if (hits % resultsPerPage > 0) {
        pages++;
      }

      int lastResult = Math.min(firstResult + resultsPerPage, hits) - 1;

      return new SearchResult<Subject>(
          page, pages, hits, firstResult, lastResult, query.getResultList());

    } catch (ParseException e) {
      throw new PersistenceException(e);
    }
  }
Exemplo n.º 20
0
  @Override
  public QueryResponse query(SearchQuery searchQuery) {
    try {
      if (logger.isDebugEnabled()) {
        logger.debug("searching query...");
      }
      long start = System.currentTimeMillis();
      QueryParser queryParser =
          new QueryParser(
              LuceneConfig.LUCENE_VERSION, schema.getDefaultSearchField(), schema.getAnalyzer());
      Query query = queryParser.parse(searchQuery.getQuery());
      int pageNo = searchQuery.getPageNo();
      int pageSize = searchQuery.getPageSize();
      int fullPageCount = pageNo * pageSize;
      int pageStartIndex = pageNo < 1 ? 0 : ((pageNo - 1) * pageSize);
      Sort sort = getSort(searchQuery);
      Filter filter = getFilter(searchQuery);
      TopFieldDocs topFieldDocs = indexSearcher.search(query, filter, fullPageCount, sort);
      ScoreDoc[] scoreDocs = topFieldDocs.scoreDocs;
      int scoreDocsLength = scoreDocs.length;
      List<OutputDocument> outputDocuments;

      if (scoreDocsLength <= pageStartIndex) {
        // 当前页没有数据了
        outputDocuments = CollectionUtil.newArrayList(0);
      } else {
        // 只获取最后一页的数据
        outputDocuments = CollectionUtil.newArrayList(scoreDocs.length - pageStartIndex);
        for (int i = pageStartIndex; i < scoreDocs.length; ++i) {
          Document doc = indexSearcher.doc(scoreDocs[i].doc);
          OutputDocument outputDocument = DocumentTransformUtil.toOutputDocument(doc, schema);
          outputDocuments.add(outputDocument);
        }
      }
      QueryResponse queryResponse = new QueryResponse();

      queryResponse.setOutputDocuments(outputDocuments);
      queryResponse.setTotalHits(topFieldDocs.totalHits);

      if (logger.isDebugEnabled()) {
        logger.debug("search query finish.");
      }
      long end = System.currentTimeMillis();
      long timeEscape = end - start;
      queryResponse.setTimeEscape(timeEscape);
      return queryResponse;
    } catch (Exception e) {
      logger.error("search query error", e);
      return new QueryResponse(e.getMessage(), ResultCodes.COMMON_ERROR);
    }
  }
  /* (non-Javadoc)
   * @see org.apache.lucene.benchmark.quality.QualityQueryParser#parse(org.apache.lucene.benchmark.quality.QualityQuery)
   */
  public Query parse(QualityQuery qq) throws ParseException {
    QueryParser qp = queryParser.get();
    if (qp == null) {
      qp =
          new QueryParser(
              Version.LUCENE_CURRENT, indexField, new StandardAnalyzer(Version.LUCENE_CURRENT));
      queryParser.set(qp);
    }
    BooleanQuery bq = new BooleanQuery();
    for (int i = 0; i < qqNames.length; i++)
      bq.add(qp.parse(QueryParser.escape(qq.getValue(qqNames[i]))), BooleanClause.Occur.SHOULD);

    return bq;
  }
Exemplo n.º 22
0
  @Override
  public void addTerm(
      BooleanQuery booleanQuery,
      String field,
      String value,
      boolean like,
      BooleanClauseOccur booleanClauseOccur) {

    if (Validator.isNull(value)) {
      return;
    }

    Analyzer analyzer = getAnalyzer();

    if (analyzer instanceof PerFieldAnalyzer) {
      PerFieldAnalyzer perFieldAnalyzer = (PerFieldAnalyzer) analyzer;

      Analyzer fieldAnalyzer = perFieldAnalyzer.getAnalyzer(field);

      if (fieldAnalyzer instanceof LikeKeywordAnalyzer) {
        like = true;
      }
    }

    if (like) {
      value = StringUtil.replace(value, StringPool.PERCENT, StringPool.BLANK);
    }

    try {
      QueryParser queryParser = new QueryParser(getVersion(), field, analyzer);

      Query query = queryParser.parse(value);

      BooleanClause.Occur occur = null;

      if (booleanClauseOccur.equals(BooleanClauseOccur.MUST)) {
        occur = BooleanClause.Occur.MUST;
      } else if (booleanClauseOccur.equals(BooleanClauseOccur.MUST_NOT)) {
        occur = BooleanClause.Occur.MUST_NOT;
      } else {
        occur = BooleanClause.Occur.SHOULD;
      }

      _includeIfUnique(booleanQuery, like, queryParser, query, occur);
    } catch (Exception e) {
      if (_log.isWarnEnabled()) {
        _log.warn(e, e);
      }
    }
  }
Exemplo n.º 23
0
 /**
  * 获取全文查询对象
  *
  * @param q 查询关键字
  * @param fields 查询字段
  * @return 全文查询对象
  */
 public BooleanQuery getFullTextQuery(String q, String... fields) {
   Analyzer analyzer = new IKAnalyzer();
   BooleanQuery query = new BooleanQuery();
   try {
     if (StringUtils.isNotBlank(q)) {
       for (String field : fields) {
         QueryParser parser = new QueryParser(Version.LUCENE_36, field, analyzer);
         query.add(parser.parse(q), Occur.SHOULD);
       }
     }
   } catch (ParseException e) {
     e.printStackTrace();
   }
   return query;
 }
  private int executeQuery(String field, String queryString, Directory dir)
      throws CorruptIndexException, IOException, ParseException {

    IndexReader reader = IndexReader.open(dir, true);
    IndexSearcher searcher = new IndexSearcher(reader);
    QueryParser parser =
        new QueryParser(Version.LUCENE_35, field, new StandardWithACIIFoldingFilter());
    Query q1 = parser.parse(queryString);
    TopDocs hits = searcher.search(q1, 1000);
    int hitCount = hits.totalHits;

    searcher.close();

    return hitCount;
  }
 @SuppressWarnings("static-access")
 private QueryParser getQueryParser(Analyzer analyzer) {
   // defaultSearchField indicates which field search against when there is no term
   // indicated in the query string.
   // The analyzer is needed so that we use the same analyzer on the search queries as
   // was used on the text that was indexed.
   QueryParser qp = new QueryParser(defaultSearchField, analyzer);
   // this sets the query parser to AND all of the query terms it finds.
   qp.setDefaultOperator(QueryParser.AND_OPERATOR);
   // set up the map of stemmed field names -> unstemmed field names
   //        HashMap<String,String> map = new HashMap<String, String>();
   //        map.put(Entity2LuceneDoc.term.ALLTEXT,Entity2LuceneDoc.term.ALLTEXTUNSTEMMED);
   //        qp.setStemmedToUnstemmed(map);
   return qp;
 }
Exemplo n.º 26
0
  /**
   * Creates a new instance; Passes the query directly on to the Lucene parser.
   *
   * @param values
   * @param query
   * @param analyzer
   */
  public FullText(SearchValues values, String query, Class<? extends Analyzer> analyzer) {
    super(values);
    Assert.notNull(analyzer, "Analyzer required");
    this.analyzer = analyzer;

    if (values.onlyTypes == null || values.onlyTypes.size() != 1) {
      throw new ApiUsageException(
          "Searches by full text are currently limited to a single type.\n"
              + "Plese use Search.onlyType()");
    }

    if (query == null || query.length() < 1) {
      throw new IllegalArgumentException("Query string must be non-empty");
    }

    if ((query.startsWith("*") || query.startsWith("?")) && !values.leadingWildcard) {
      throw new ApiUsageException(
          "Searches starting with a leading "
              + "wildcard (*,?) can be slow.\nPlease use "
              + "setAllowLeadingWildcard() to permit this usage.");
    }

    if (query.equals("*")) {
      throw new ApiUsageException(
          "Wildcard searches (*) must contain more than a single wildcard. ");
    }

    this.queryStr = query;
    try {
      final Analyzer a = analyzer.newInstance();
      final QueryParser parser = new /*Analyzing*/ QueryParser("combined_fields", a);
      parser.setAllowLeadingWildcard(values.leadingWildcard);
      q = parser.parse(queryStr);
    } catch (ParseException pe) {
      final String msg = queryStr + " caused a parse exception: " + pe.getMessage();
      // No longer logging these, since it's a simple user error
      ApiUsageException aue = new ApiUsageException(msg);
      throw aue;
    } catch (InstantiationException e) {
      ApiUsageException aue =
          new ApiUsageException(analyzer.getName() + " cannot be instantiated.");
      throw aue;
    } catch (IllegalAccessException e) {
      ApiUsageException aue =
          new ApiUsageException(analyzer.getName() + " cannot be instantiated.");
      throw aue;
    }
  }
Exemplo n.º 27
0
  @Transactional(propagation = Propagation.REQUIRED)
  public int searchCount(String[] f, String luquery, Class<?>... entities) throws Exception {

    // create FullTextEntityManager ----------------------------
    FullTextEntityManager fullTextEntityManager =
        org.hibernate.search.jpa.Search.getFullTextEntityManager(entityManager);
    // ---------------------------------------------------------

    QueryParser parser =
        new MultiFieldQueryParser(Version.LUCENE_31, f, new StandardAnalyzer(Version.LUCENE_31));

    org.apache.lucene.search.Query query = parser.parse(luquery.trim());

    // wrap Lucene query in a javax.persistence.Query
    return fullTextEntityManager.createFullTextQuery(query, entities).getResultSize();
  }
 private void search(Analyzer analyzer, Directory directory, String queryStr)
     throws CorruptIndexException, IOException, ParseException {
   IndexSearcher isearcher;
   // 查询索引
   isearcher = new IndexSearcher(directory);
   QueryParser tq = new QueryParser(Version.LUCENE_32, "text", ansjHeightAnalyzer);
   Query query = tq.parse(queryStr);
   System.out.println(query);
   TopDocs hits = isearcher.search(query, 5);
   System.out.println(queryStr + ":共找到" + hits.totalHits + "条记录!");
   for (int i = 0; i < hits.scoreDocs.length; i++) {
     int docId = hits.scoreDocs[i].doc;
     Document document = isearcher.doc(docId);
     System.out.println(toHighlighter(ansjHeightAnalyzer, query, document));
   }
 }
Exemplo n.º 29
0
 private String[] convertEscaped(
     Path<?> leftHandSide, Expression<?> rightHandSide, QueryMetadata metadata) {
   String[] str = convert(leftHandSide, rightHandSide, metadata);
   for (int i = 0; i < str.length; i++) {
     str[i] = QueryParser.escape(str[i]);
   }
   return str;
 }
Exemplo n.º 30
0
  /**
   * Performs a search using Lucene
   *
   * @param index with the documents to search
   * @param query to search
   * @param fieldName field where search inside the documents
   * @return the search result
   */
  public static LuceneSearchResult search(LuceneIndex index, String query, String fieldName) {
    try {

      Searcher searcher = new IndexSearcher(index.getDirectory());
      Analyzer analyzer = new StandardAnalyzer();
      QueryParser parser = new QueryParser(fieldName, analyzer);
      Query q = parser.parse(query);
      Hits hits = searcher.search(q);
      LuceneSearchResult lsr = new LuceneSearchResult(hits, index);
      searcher.close();
      return lsr;

    } catch (Exception e) {
      org.apache.commons.logging.LogFactory.getLog(LuceneSearcher.class).error(e);
    }
    return null;
  }