예제 #1
0
 // NEW
 // SELECT ...,
 // TS_RANK_CD(NX_TO_TSVECTOR(fulltext), TO_TSQUERY('french', ?), 32) as
 // nxscore
 // FROM ...
 // LEFT JOIN fulltext ON fulltext.id = hierarchy.id
 // WHERE ...
 // AND TO_TSQUERY('french', ?) @@ NX_TO_TSVECTOR(fulltext)
 // AND fulltext LIKE '% foo bar %' -- when phrase search
 // ORDER BY nxscore DESC
 @Override
 public FulltextMatchInfo getFulltextScoredMatchInfo(
     String fulltextQuery,
     String indexName,
     int nthMatch,
     Column mainColumn,
     Model model,
     Database database) {
   String indexSuffix = model.getFulltextIndexSuffix(indexName);
   Table ft = database.getTable(model.FULLTEXT_TABLE_NAME);
   Column ftMain = ft.getColumn(model.MAIN_KEY);
   Column ftColumn = ft.getColumn(model.FULLTEXT_FULLTEXT_KEY + indexSuffix);
   String ftColumnName = ftColumn.getFullQuotedName();
   String nthSuffix = nthMatch == 1 ? "" : String.valueOf(nthMatch);
   FulltextMatchInfo info = new FulltextMatchInfo();
   info.joins = new ArrayList<Join>();
   if (nthMatch == 1) {
     // Need only one JOIN involving the fulltext table
     info.joins.add(
         new Join(
             Join.INNER,
             ft.getQuotedName(),
             null,
             null,
             ftMain.getFullQuotedName(),
             mainColumn.getFullQuotedName()));
   }
   /*
    * for phrase search, fulltextQuery may contain a LIKE part
    */
   String like;
   if (fulltextQuery.contains(FT_LIKE_SEP)) {
     String[] tmp = fulltextQuery.split(FT_LIKE_SEP, 2);
     fulltextQuery = tmp[0];
     like = tmp[1].replace(FT_LIKE_COL, ftColumnName);
   } else {
     like = null;
   }
   String tsquery = String.format("TO_TSQUERY('%s', ?)", fulltextAnalyzer);
   String tsvector;
   if (compatibilityFulltextTable) {
     tsvector = ftColumnName;
   } else {
     tsvector = String.format("NX_TO_TSVECTOR(%s)", ftColumnName);
   }
   String where = String.format("(%s @@ %s)", tsquery, tsvector);
   if (like != null) {
     where += " AND (" + like + ")";
   }
   info.whereExpr = where;
   info.whereExprParam = fulltextQuery;
   info.scoreExpr = String.format("TS_RANK_CD(%s, %s, 32)", tsvector, tsquery);
   info.scoreExprParam = fulltextQuery;
   info.scoreAlias = "_nxscore" + nthSuffix;
   info.scoreCol = new Column(mainColumn.getTable(), null, ColumnType.DOUBLE, null);
   return info;
 }
예제 #2
0
 @Override
 public Map<String, Serializable> getSQLStatementsProperties(Model model, Database database) {
   Map<String, Serializable> properties = new HashMap<String, Serializable>();
   switch (idType) {
     case VARCHAR:
       properties.put("idType", "varchar(36)");
       properties.put("idTypeParam", "varchar");
       properties.put("idNotPresent", "'-'");
       properties.put("sequenceEnabled", Boolean.FALSE);
       break;
     case UUID:
       properties.put("idType", "uuid");
       properties.put("idTypeParam", "uuid");
       properties.put("idNotPresent", "'00000000-FFFF-FFFF-FFFF-FFFF00000000'");
       properties.put("sequenceEnabled", Boolean.FALSE);
       break;
     case SEQUENCE:
       properties.put("idType", "int8");
       properties.put("idTypeParam", "int8");
       properties.put("idNotPresent", "-1");
       properties.put("sequenceEnabled", Boolean.TRUE);
       properties.put("idSequenceName", idSequenceName);
   }
   properties.put("aclOptimizationsEnabled", Boolean.valueOf(aclOptimizationsEnabled));
   properties.put("pathOptimizationsEnabled", Boolean.valueOf(pathOptimizationsEnabled));
   properties.put("fulltextAnalyzer", fulltextAnalyzer);
   properties.put("fulltextEnabled", Boolean.valueOf(!fulltextSearchDisabled));
   properties.put("clusteringEnabled", Boolean.valueOf(clusteringEnabled));
   properties.put("proxiesEnabled", Boolean.valueOf(proxiesEnabled));
   properties.put("softDeleteEnabled", Boolean.valueOf(softDeleteEnabled));
   if (!fulltextDisabled) {
     Table ft = database.getTable(model.FULLTEXT_TABLE_NAME);
     properties.put("fulltextTable", ft.getQuotedName());
     FulltextConfiguration fti = model.getFulltextConfiguration();
     List<String> lines = new ArrayList<String>(fti.indexNames.size());
     for (String indexName : fti.indexNames) {
       String suffix = model.getFulltextIndexSuffix(indexName);
       Column ftft = ft.getColumn(model.FULLTEXT_FULLTEXT_KEY + suffix);
       Column ftst = ft.getColumn(model.FULLTEXT_SIMPLETEXT_KEY + suffix);
       Column ftbt = ft.getColumn(model.FULLTEXT_BINARYTEXT_KEY + suffix);
       String concat;
       if (compatibilityFulltextTable) {
         // tsvector
         concat = "  NEW.%s := COALESCE(NEW.%s, ''::TSVECTOR) || COALESCE(NEW.%s, ''::TSVECTOR);";
       } else {
         // text with space at beginning and end
         concat = "  NEW.%s := ' ' || COALESCE(NEW.%s, '') || ' ' || COALESCE(NEW.%s, '') || ' ';";
       }
       String line =
           String.format(concat, ftft.getQuotedName(), ftst.getQuotedName(), ftbt.getQuotedName());
       lines.add(line);
     }
     properties.put("fulltextTriggerStatements", StringUtils.join(lines, "\n"));
   }
   String[] permissions =
       NXCore.getSecurityService().getPermissionsToCheck(SecurityConstants.BROWSE);
   List<String> permsList = new LinkedList<String>();
   for (String perm : permissions) {
     permsList.add("('" + perm + "')");
   }
   properties.put("readPermissions", StringUtils.join(permsList, ", "));
   properties.put("usersSeparator", getUsersSeparator());
   properties.put("everyone", SecurityConstants.EVERYONE);
   properties.put("readAclMaxSize", Integer.toString(readAclMaxSize));
   properties.put("unlogged", unloggedKeyword);
   return properties;
 }