public void testBoostedFieldDesc() throws Exception {
    FullTextSession fullTextSession = Search.getFullTextSession(openSession());
    buildBoostedFieldIndex(fullTextSession);

    fullTextSession.clear();
    Transaction tx = fullTextSession.beginTransaction();

    QueryParser authorParser =
        new QueryParser(
            TestConstants.getTargetLuceneVersion(), "author", TestConstants.standardAnalyzer);
    QueryParser descParser =
        new QueryParser(
            TestConstants.getTargetLuceneVersion(), "description", TestConstants.standardAnalyzer);
    Query author = authorParser.parse("Wells");
    Query desc = descParser.parse("martians");

    BooleanQuery query = new BooleanQuery();
    query.add(author, BooleanClause.Occur.SHOULD);
    query.add(desc, BooleanClause.Occur.SHOULD);
    log.debug(query.toString());

    org.hibernate.search.FullTextQuery hibQuery =
        fullTextSession.createFullTextQuery(query, BoostedFieldDescriptionLibrary.class);
    List results = hibQuery.list();

    assertTrue(
        "incorrect document boost",
        ((BoostedFieldDescriptionLibrary) results.get(0)).getDescription().startsWith("Martians"));

    log.debug(hibQuery.explain(0).toString());
    log.debug(hibQuery.explain(1).toString());

    // cleanup
    for (Object element :
        fullTextSession
            .createQuery("from " + BoostedFieldDescriptionLibrary.class.getName())
            .list()) {
      fullTextSession.delete(element);
    }
    tx.commit();
    fullTextSession.close();
  }
Beispiel #2
0
 private void outputQueryAndResults(
     boolean outputLogs, Coffee originalInstance, Query mltQuery, List<Object[]> results) {
   // set to true to display results
   if (outputLogs) {
     StringBuilder builder =
         new StringBuilder("Initial coffee: ")
             .append(originalInstance)
             .append("\n\n")
             .append("Query: ")
             .append(mltQuery.toString())
             .append("\n\n")
             .append("Matching coffees")
             .append("\n");
     for (Object[] entry : results) {
       builder.append("    Score: ").append(entry[1]);
       builder.append(" | Coffee: ").append(entry[0]).append("\n");
     }
     log.debug(builder.toString());
   }
 }
 private Version getLuceneMatchVersion(SearchConfiguration cfg) {
   final Version version;
   String tmp = cfg.getProperty(Environment.LUCENE_MATCH_VERSION);
   if (StringHelper.isEmpty(tmp)) {
     log.recommendConfiguringLuceneVersion();
     version = Environment.DEFAULT_LUCENE_MATCH_VERSION;
   } else {
     try {
       version = Version.parseLeniently(tmp);
       if (log.isDebugEnabled()) {
         log.debug("Setting Lucene compatibility to Version " + version);
       }
     } catch (IllegalArgumentException e) {
       throw log.illegalLuceneVersionFormat(tmp, e.getMessage());
     } catch (ParseException e) {
       throw log.illegalLuceneVersionFormat(tmp, e.getMessage());
     }
   }
   return version;
 }