Search in sources :

Example 11 with Builder

use of org.apache.lucene.search.BooleanQuery.Builder in project janusgraph by JanusGraph.

the class LuceneIndex method convertQuery.

private SearchParams convertQuery(Condition<?> condition, final KeyInformation.StoreRetriever information, final LuceneCustomAnalyzer delegatingAnalyzer) {
    final SearchParams params = new SearchParams();
    if (condition instanceof PredicateCondition) {
        final PredicateCondition<String, ?> atom = (PredicateCondition) condition;
        Object value = atom.getValue();
        final String key = atom.getKey();
        KeyInformation ki = information.get(key);
        final JanusGraphPredicate janusgraphPredicate = atom.getPredicate();
        if (value == null && janusgraphPredicate == Cmp.NOT_EQUAL) {
            // some fields like Integer omit norms but have docValues
            params.addQuery(new DocValuesFieldExistsQuery(key), BooleanClause.Occur.SHOULD);
            // some fields like Text have no docValue but have norms
            params.addQuery(new NormsFieldExistsQuery(key), BooleanClause.Occur.SHOULD);
        } else if (value instanceof Number) {
            Preconditions.checkArgument(janusgraphPredicate instanceof Cmp, "Relation not supported on numeric types: %s", janusgraphPredicate);
            params.addQuery(numericQuery(key, (Cmp) janusgraphPredicate, (Number) value));
        } else if (value instanceof String) {
            if (janusgraphPredicate == Cmp.LESS_THAN) {
                params.addQuery(TermRangeQuery.newStringRange(key, null, value.toString(), false, false));
            } else if (janusgraphPredicate == Cmp.LESS_THAN_EQUAL) {
                params.addQuery(TermRangeQuery.newStringRange(key, null, value.toString(), false, true));
            } else if (janusgraphPredicate == Cmp.GREATER_THAN) {
                params.addQuery(TermRangeQuery.newStringRange(key, value.toString(), null, false, false));
            } else if (janusgraphPredicate == Cmp.GREATER_THAN_EQUAL) {
                params.addQuery(TermRangeQuery.newStringRange(key, value.toString(), null, true, false));
            } else {
                final Mapping map = Mapping.getMapping(ki);
                final String stringFieldKey;
                if (Mapping.getMapping(ki) == Mapping.TEXTSTRING) {
                    stringFieldKey = getDualFieldName(key, ki).orElse(key);
                } else {
                    stringFieldKey = key;
                }
                if ((map == Mapping.DEFAULT || map == Mapping.TEXT) && !Text.HAS_CONTAINS.contains(janusgraphPredicate))
                    throw new IllegalArgumentException("Text mapped string values only support CONTAINS queries and not: " + janusgraphPredicate);
                if (map == Mapping.STRING && Text.HAS_CONTAINS.contains(janusgraphPredicate))
                    throw new IllegalArgumentException("String mapped string values do not support CONTAINS queries: " + janusgraphPredicate);
                if (janusgraphPredicate == Text.CONTAINS) {
                    tokenize(params, map, delegatingAnalyzer, ((String) value).toLowerCase(), key, janusgraphPredicate);
                } else if (janusgraphPredicate == Text.CONTAINS_PREFIX) {
                    tokenize(params, map, delegatingAnalyzer, (String) value, key, janusgraphPredicate);
                } else if (janusgraphPredicate == Text.PREFIX) {
                    params.addQuery(new PrefixQuery(new Term(stringFieldKey, (String) value)));
                } else if (janusgraphPredicate == Text.REGEX) {
                    final RegexpQuery rq = new RegexpQuery(new Term(stringFieldKey, (String) value));
                    params.addQuery(rq);
                } else if (janusgraphPredicate == Text.CONTAINS_REGEX) {
                    // This is terrible -- there is probably a better way
                    // putting this to lowercase because Text search is supposed to be case insensitive
                    final RegexpQuery rq = new RegexpQuery(new Term(key, ".*" + (((String) value).toLowerCase()) + ".*"));
                    params.addQuery(rq);
                } else if (janusgraphPredicate == Cmp.EQUAL || janusgraphPredicate == Cmp.NOT_EQUAL) {
                    tokenize(params, map, delegatingAnalyzer, (String) value, stringFieldKey, janusgraphPredicate);
                } else if (janusgraphPredicate == Text.FUZZY) {
                    params.addQuery(new FuzzyQuery(new Term(stringFieldKey, (String) value), Text.getMaxEditDistance((String) value)));
                } else if (janusgraphPredicate == Text.CONTAINS_FUZZY) {
                    value = ((String) value).toLowerCase();
                    final Builder b = new BooleanQuery.Builder();
                    for (final String term : Text.tokenize((String) value)) {
                        b.add(new FuzzyQuery(new Term(key, term), Text.getMaxEditDistance(term)), BooleanClause.Occur.MUST);
                    }
                    params.addQuery(b.build());
                } else
                    throw new IllegalArgumentException("Relation is not supported for string value: " + janusgraphPredicate);
            }
        } else if (value instanceof Geoshape) {
            Preconditions.checkArgument(janusgraphPredicate instanceof Geo, "Relation not supported on geo types: %s", janusgraphPredicate);
            final Shape shape = ((Geoshape) value).getShape();
            final SpatialOperation spatialOp = SPATIAL_PREDICATES.get(janusgraphPredicate);
            final SpatialArgs args = new SpatialArgs(spatialOp, shape);
            params.addQuery(getSpatialStrategy(key, information.get(key)).makeQuery(args));
        } else if (value instanceof Date) {
            Preconditions.checkArgument(janusgraphPredicate instanceof Cmp, "Relation not supported on date types: %s", janusgraphPredicate);
            params.addQuery(numericQuery(key, (Cmp) janusgraphPredicate, ((Date) value).getTime()));
        } else if (value instanceof Instant) {
            Preconditions.checkArgument(janusgraphPredicate instanceof Cmp, "Relation not supported on instant types: %s", janusgraphPredicate);
            params.addQuery(numericQuery(key, (Cmp) janusgraphPredicate, ((Instant) value).toEpochMilli()));
        } else if (value instanceof Boolean) {
            Preconditions.checkArgument(janusgraphPredicate instanceof Cmp, "Relation not supported on boolean types: %s", janusgraphPredicate);
            final int intValue;
            switch((Cmp) janusgraphPredicate) {
                case EQUAL:
                    intValue = ((Boolean) value) ? 1 : 0;
                    params.addQuery(IntPoint.newRangeQuery(key, intValue, intValue));
                    break;
                case NOT_EQUAL:
                    intValue = ((Boolean) value) ? 0 : 1;
                    params.addQuery(IntPoint.newRangeQuery(key, intValue, intValue));
                    break;
                default:
                    throw new IllegalArgumentException("Boolean types only support EQUAL or NOT_EQUAL");
            }
        } else if (value instanceof UUID) {
            Preconditions.checkArgument(janusgraphPredicate instanceof Cmp, "Relation not supported on UUID types: %s", janusgraphPredicate);
            if (janusgraphPredicate == Cmp.EQUAL) {
                params.addQuery(new TermQuery(new Term(key, value.toString())));
            } else if (janusgraphPredicate == Cmp.NOT_EQUAL) {
                final BooleanQuery.Builder q = new BooleanQuery.Builder();
                q.add(new MatchAllDocsQuery(), BooleanClause.Occur.MUST);
                q.add(new TermQuery(new Term(key, value.toString())), BooleanClause.Occur.MUST_NOT);
                params.addQuery(q.build());
            } else {
                throw new IllegalArgumentException("Relation is not supported for UUID type: " + janusgraphPredicate);
            }
        } else {
            throw new IllegalArgumentException("Unsupported type: " + value);
        }
    } else if (condition instanceof Not) {
        final SearchParams childParams = convertQuery(((Not) condition).getChild(), information, delegatingAnalyzer);
        params.addQuery(new MatchAllDocsQuery(), BooleanClause.Occur.MUST);
        params.addParams(childParams, BooleanClause.Occur.MUST_NOT);
    } else if (condition instanceof And) {
        for (final Condition c : condition.getChildren()) {
            final SearchParams childParams = convertQuery(c, information, delegatingAnalyzer);
            params.addParams(childParams, BooleanClause.Occur.MUST);
        }
    } else if (condition instanceof Or) {
        for (final Condition c : condition.getChildren()) {
            final SearchParams childParams = convertQuery(c, information, delegatingAnalyzer);
            params.addParams(childParams, BooleanClause.Occur.SHOULD);
        }
    } else
        throw new IllegalArgumentException("Invalid condition: " + condition);
    return params;
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) Shape(org.locationtech.spatial4j.shape.Shape) Or(org.janusgraph.graphdb.query.condition.Or) Builder(org.apache.lucene.search.BooleanQuery.Builder) DocValuesFieldExistsQuery(org.apache.lucene.search.DocValuesFieldExistsQuery) Mapping(org.janusgraph.core.schema.Mapping) NormsFieldExistsQuery(org.apache.lucene.search.NormsFieldExistsQuery) Builder(org.apache.lucene.search.BooleanQuery.Builder) KeyInformation(org.janusgraph.diskstorage.indexing.KeyInformation) RegexpQuery(org.apache.lucene.search.RegexpQuery) JanusGraphPredicate(org.janusgraph.graphdb.query.JanusGraphPredicate) SpatialOperation(org.apache.lucene.spatial.query.SpatialOperation) UUID(java.util.UUID) PredicateCondition(org.janusgraph.graphdb.query.condition.PredicateCondition) Condition(org.janusgraph.graphdb.query.condition.Condition) TermQuery(org.apache.lucene.search.TermQuery) PredicateCondition(org.janusgraph.graphdb.query.condition.PredicateCondition) SpatialArgs(org.apache.lucene.spatial.query.SpatialArgs) Cmp(org.janusgraph.core.attribute.Cmp) Instant(java.time.Instant) Geoshape(org.janusgraph.core.attribute.Geoshape) Term(org.apache.lucene.index.Term) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) Date(java.util.Date) DoublePoint(org.apache.lucene.document.DoublePoint) LongPoint(org.apache.lucene.document.LongPoint) IntPoint(org.apache.lucene.document.IntPoint) Geo(org.janusgraph.core.attribute.Geo) Not(org.janusgraph.graphdb.query.condition.Not) PrefixQuery(org.apache.lucene.search.PrefixQuery) And(org.janusgraph.graphdb.query.condition.And) FuzzyQuery(org.apache.lucene.search.FuzzyQuery)

Example 12 with Builder

use of org.apache.lucene.search.BooleanQuery.Builder in project SearchServices by Alfresco.

the class Solr4QueryParser method addContentAttributeQuery.

// TODO: correct field names
protected Query addContentAttributeQuery(PropertyDefinition pDef, String queryText, SubQuery subQueryBuilder, AnalysisMode analysisMode, LuceneFunction luceneFunction, String expandedFieldName, List<Locale> expandedLocales) throws ParseException {
    BooleanQuery.Builder booleanQuery = new BooleanQuery.Builder();
    for (Locale locale : expandedLocales) {
        if (locale.toString().length() == 0) {
            IndexedField indexedField = AlfrescoSolrDataModel.getInstance().getQueryableFields(pDef.getName(), null, FieldUse.FTS);
            for (FieldInstance field : indexedField.getFields()) {
                if (!field.isLocalised()) {
                    Query subQuery = subQueryBuilder.getQuery(field.getField(), queryText, analysisMode, luceneFunction);
                    if (subQuery != null) {
                        booleanQuery.add(subQuery, Occur.SHOULD);
                    }
                }
            }
        } else {
            StringBuilder builder = new StringBuilder(queryText.length() + 10);
            builder.append("\u0000").append(locale.toString()).append("\u0000").append(queryText);
            IndexedField indexedField = AlfrescoSolrDataModel.getInstance().getQueryableFields(pDef.getName(), null, FieldUse.FTS);
            for (FieldInstance field : indexedField.getFields()) {
                if (field.isLocalised()) {
                    Query subQuery = subQueryBuilder.getQuery(field.getField(), builder.toString(), analysisMode, luceneFunction);
                    if (subQuery != null) {
                        booleanQuery.add(subQuery, Occur.SHOULD);
                    }
                }
            }
        }
    }
    return getNonEmptyBooleanQuery(booleanQuery.build());
}
Also used : Locale(java.util.Locale) BooleanQuery(org.apache.lucene.search.BooleanQuery) Query(org.apache.lucene.search.Query) RegexpQuery(org.apache.lucene.search.RegexpQuery) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) SpanNearQuery(org.apache.lucene.search.spans.SpanNearQuery) SpanOrQuery(org.apache.lucene.search.spans.SpanOrQuery) MultiTermQuery(org.apache.lucene.search.MultiTermQuery) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) SpanQuery(org.apache.lucene.search.spans.SpanQuery) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) TermRangeQuery(org.apache.lucene.search.TermRangeQuery) IndexedField(org.alfresco.solr.AlfrescoSolrDataModel.IndexedField) Builder(org.apache.lucene.search.BooleanQuery.Builder) FieldInstance(org.alfresco.solr.AlfrescoSolrDataModel.FieldInstance) Builder(org.apache.lucene.search.BooleanQuery.Builder)

Example 13 with Builder

use of org.apache.lucene.search.BooleanQuery.Builder in project SearchServices by Alfresco.

the class Solr4QueryParser method createFingerPrintQuery.

/**
 * @param field
 * @param queryText
 * @param analysisMode
 *
 * @param luceneFunction
 * @return
 * @throws IOException
 * @throws ParseException
 */
private Query createFingerPrintQuery(String field, String queryText, Collection values, AnalysisMode analysisMode, LuceneFunction luceneFunction) throws IOException, ParseException {
    String[] parts = queryText.split("_");
    if (parts.length == 0) {
        return createIsNodeQuery("T");
    }
    if (values != null) {
        int bandSize = 1;
        float fraction = -1;
        float truePositive = 1;
        if (parts.length > 1) {
            fraction = Float.parseFloat(parts[1]);
            if (fraction > 1) {
                fraction /= 100;
            }
        }
        if (parts.length > 2) {
            truePositive = Float.parseFloat(parts[2]);
            if (truePositive > 1) {
                truePositive /= 100;
            }
            bandSize = computeBandSize(values.size(), fraction, truePositive);
        }
        BooleanQuery.Builder builder = new BooleanQuery.Builder();
        BooleanQuery.Builder childBuilder = new BooleanQuery.Builder();
        int rowInBand = 0;
        for (Object token : values) {
            TermQuery tq = new TermQuery(new Term("MINHASH", token.toString()));
            if (bandSize == 1) {
                builder.add(new ConstantScoreQuery(tq), Occur.SHOULD);
            } else {
                childBuilder.add(new ConstantScoreQuery(tq), Occur.MUST);
                rowInBand++;
                if (rowInBand == bandSize) {
                    builder.add(new ConstantScoreQuery(childBuilder.setDisableCoord(true).build()), Occur.SHOULD);
                    childBuilder = new BooleanQuery.Builder();
                    rowInBand = 0;
                }
            }
        }
        // start
        if (childBuilder.build().clauses().size() > 0) {
            for (Object token : values) {
                TermQuery tq = new TermQuery(new Term("MINHASH", token.toString()));
                childBuilder.add(new ConstantScoreQuery(tq), Occur.MUST);
                rowInBand++;
                if (rowInBand == bandSize) {
                    builder.add(new ConstantScoreQuery(childBuilder.setDisableCoord(true).build()), Occur.SHOULD);
                    break;
                }
            }
        }
        builder.setDisableCoord(true);
        if (parts.length == 2) {
            builder.setMinimumNumberShouldMatch((int) (Math.ceil(values.size() * fraction)));
        }
        Query q = builder.build();
        return q;
    } else {
        return getFieldQueryImpl(field, queryText, analysisMode, luceneFunction);
    }
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) MultiTermQuery(org.apache.lucene.search.MultiTermQuery) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) TermQuery(org.apache.lucene.search.TermQuery) Query(org.apache.lucene.search.Query) RegexpQuery(org.apache.lucene.search.RegexpQuery) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) SpanNearQuery(org.apache.lucene.search.spans.SpanNearQuery) SpanOrQuery(org.apache.lucene.search.spans.SpanOrQuery) MultiTermQuery(org.apache.lucene.search.MultiTermQuery) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) SpanQuery(org.apache.lucene.search.spans.SpanQuery) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) TermRangeQuery(org.apache.lucene.search.TermRangeQuery) Builder(org.apache.lucene.search.BooleanQuery.Builder) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) JSONObject(org.json.JSONObject) Term(org.apache.lucene.index.Term) Builder(org.apache.lucene.search.BooleanQuery.Builder)

Example 14 with Builder

use of org.apache.lucene.search.BooleanQuery.Builder in project rubia-forums by flashboss.

the class ForumsSearchModuleImpl method findTopics.

@SuppressWarnings("unchecked")
public ResultPage<Topic> findTopics(SearchCriteria criteria) throws ModuleException {
    if (criteria != null) {
        try {
            EntityManager session = getSession();
            FullTextSession fullTextSession = getFullTextSession((Session) session.getDelegate());
            Builder builder = new Builder();
            String keywords = criteria.getKeywords();
            if (keywords != null && keywords.length() != 0) {
                String[] fields = null;
                Searching searching = Searching.valueOf(criteria.getSearching());
                switch(searching) {
                    case TITLE_MSG:
                        fields = new String[] { "message.text", "topic.subject" };
                        break;
                    case MSG:
                        fields = new String[] { "message.text" };
                        break;
                }
                MultiFieldQueryParser parser = new MultiFieldQueryParser(fields, new StandardAnalyzer());
                builder.add(parser.parse(keywords), MUST);
            }
            String forumId = criteria.getForum();
            if (forumId != null && forumId.length() != 0) {
                builder.add(new TermQuery(new Term("topic.forum.id", forumId)), MUST);
            }
            String categoryId = criteria.getCategory();
            if (categoryId != null && categoryId.length() != 0) {
                builder.add(new TermQuery(new Term("topic.forum.category.id", categoryId)), MUST);
            }
            String userName = criteria.getAuthor();
            if (userName != null && userName.length() != 0) {
                builder.add(new WildcardQuery(new Term("poster.userId", userName)), MUST);
            }
            String timePeriod = criteria.getTimePeriod();
            if (timePeriod != null && timePeriod.length() != 0) {
                addPostTimeQuery(builder, TimePeriod.valueOf(timePeriod));
            }
            FullTextQuery fullTextQuery = fullTextSession.createFullTextQuery(builder.build(), Post.class);
            SortOrder sortOrder = SortOrder.valueOf(criteria.getSortOrder());
            SortBy sortBy = valueOf(criteria.getSortBy());
            fullTextQuery.setSort(getSort(sortBy, sortOrder));
            fullTextQuery.setProjection("topic.id");
            LinkedHashSet<Integer> topicIds = new LinkedHashSet<Integer>();
            LinkedHashSet<Integer> topicToDispIds = new LinkedHashSet<Integer>();
            int start = criteria.getPageSize() * criteria.getPageNumber();
            int end = start + criteria.getPageSize();
            int index = 0;
            for (Object o : fullTextQuery.list()) {
                Integer id = (Integer) ((Object[]) o)[0];
                if (topicIds.add(id)) {
                    if (index >= start && index < end) {
                        topicToDispIds.add(id);
                    }
                    index++;
                }
            }
            List<Topic> topics = null;
            if (topicToDispIds.size() > 0) {
                Query q = session.createQuery("from Topic as t join fetch t.poster where t.id IN ( :topicIds )");
                q.setParameter("topicIds", topicToDispIds);
                List<Topic> results = q.getResultList();
                topics = new LinkedList<Topic>();
                for (Integer id : topicToDispIds) {
                    for (Topic topic : results) {
                        if (id.equals(topic.getId())) {
                            topics.add(topic);
                            break;
                        }
                    }
                }
            }
            ResultPage<Topic> resultPage = new ResultPage<Topic>();
            resultPage.setPage(topics);
            resultPage.setResultSize(topicIds.size());
            return resultPage;
        } catch (ParseException e) {
            return null;
        } catch (Exception e) {
            throw new ModuleException(e.getMessage(), e);
        }
    } else {
        throw new IllegalArgumentException("criteria cannot be null");
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) WildcardQuery(org.apache.lucene.search.WildcardQuery) FullTextSession(org.hibernate.search.FullTextSession) Search.getFullTextSession(org.hibernate.search.Search.getFullTextSession) FullTextQuery(org.hibernate.search.FullTextQuery) WildcardQuery(org.apache.lucene.search.WildcardQuery) Query(javax.persistence.Query) TermQuery(org.apache.lucene.search.TermQuery) SortBy(it.vige.rubia.search.SortBy) Builder(org.apache.lucene.search.BooleanQuery.Builder) ResultPage(it.vige.rubia.search.ResultPage) Topic(it.vige.rubia.model.Topic) ModuleException(it.vige.rubia.ModuleException) TermQuery(org.apache.lucene.search.TermQuery) MultiFieldQueryParser(org.apache.lucene.queryparser.classic.MultiFieldQueryParser) SortOrder(it.vige.rubia.search.SortOrder) Term(org.apache.lucene.index.Term) ParseException(org.apache.lucene.queryparser.classic.ParseException) ModuleException(it.vige.rubia.ModuleException) EntityManager(javax.persistence.EntityManager) Searching(it.vige.rubia.search.Searching) StandardAnalyzer(org.apache.lucene.analysis.standard.StandardAnalyzer) ParseException(org.apache.lucene.queryparser.classic.ParseException) FullTextQuery(org.hibernate.search.FullTextQuery)

Example 15 with Builder

use of org.apache.lucene.search.BooleanQuery.Builder in project rubia-forums by flashboss.

the class ForumsSearchModuleImpl method findTopics.

@SuppressWarnings("unchecked")
public ResultPage<Topic> findTopics(SearchCriteria criteria) throws ModuleException {
    if (criteria != null) {
        try {
            EntityManager session = getSession();
            FullTextSession fullTextSession = getFullTextSession((Session) session.getDelegate());
            Builder builder = new Builder();
            String keywords = criteria.getKeywords();
            if (keywords != null && keywords.length() != 0) {
                String[] fields = null;
                Searching searching = Searching.valueOf(criteria.getSearching());
                switch(searching) {
                    case TITLE_MSG:
                        fields = new String[] { "message.text", "topic.subject" };
                        break;
                    case MSG:
                        fields = new String[] { "message.text" };
                        break;
                }
                MultiFieldQueryParser parser = new MultiFieldQueryParser(fields, new StandardAnalyzer());
                builder.add(parser.parse(keywords), MUST);
            }
            String forumId = criteria.getForum();
            if (forumId != null && forumId.length() != 0) {
                builder.add(new TermQuery(new Term("topic.forum.id", forumId)), MUST);
            }
            String categoryId = criteria.getCategory();
            if (categoryId != null && categoryId.length() != 0) {
                builder.add(new TermQuery(new Term("topic.forum.category.id", categoryId)), MUST);
            }
            String userName = criteria.getAuthor();
            if (userName != null && userName.length() != 0) {
                builder.add(new WildcardQuery(new Term("poster.userId", userName)), MUST);
            }
            String timePeriod = criteria.getTimePeriod();
            if (timePeriod != null && timePeriod.length() != 0) {
                addPostTimeQuery(builder, TimePeriod.valueOf(timePeriod));
            }
            FullTextQuery fullTextQuery = fullTextSession.createFullTextQuery(builder.build(), Post.class);
            SortOrder sortOrder = SortOrder.valueOf(criteria.getSortOrder());
            SortBy sortBy = valueOf(criteria.getSortBy());
            fullTextQuery.setSort(getSort(sortBy, sortOrder));
            fullTextQuery.setProjection("topic.id");
            LinkedHashSet<Integer> topicIds = new LinkedHashSet<Integer>();
            LinkedHashSet<Integer> topicToDispIds = new LinkedHashSet<Integer>();
            int start = criteria.getPageSize() * criteria.getPageNumber();
            int end = start + criteria.getPageSize();
            int index = 0;
            for (Object o : fullTextQuery.list()) {
                Integer id = (Integer) ((Object[]) o)[0];
                if (topicIds.add(id)) {
                    if (index >= start && index < end) {
                        topicToDispIds.add(id);
                    }
                    index++;
                }
            }
            List<Topic> topics = null;
            if (topicToDispIds.size() > 0) {
                Query q = session.createQuery("from Topic as t join fetch t.poster where t.id IN ( :topicIds )");
                q.setParameter("topicIds", topicToDispIds);
                List<Topic> results = q.getResultList();
                topics = new LinkedList<Topic>();
                for (Integer id : topicToDispIds) {
                    for (Topic topic : results) {
                        if (id.equals(topic.getId())) {
                            topics.add(topic);
                            break;
                        }
                    }
                }
            }
            ResultPage<Topic> resultPage = new ResultPage<Topic>();
            resultPage.setPage(topics);
            resultPage.setResultSize(topicIds.size());
            return resultPage;
        } catch (ParseException e) {
            return null;
        } catch (Exception e) {
            throw new ModuleException(e.getMessage(), e);
        }
    } else {
        throw new IllegalArgumentException("criteria cannot be null");
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) WildcardQuery(org.apache.lucene.search.WildcardQuery) FullTextSession(org.hibernate.search.FullTextSession) Search.getFullTextSession(org.hibernate.search.Search.getFullTextSession) FullTextQuery(org.hibernate.search.FullTextQuery) WildcardQuery(org.apache.lucene.search.WildcardQuery) Query(javax.persistence.Query) TermQuery(org.apache.lucene.search.TermQuery) SortBy(it.vige.rubia.search.SortBy) Builder(org.apache.lucene.search.BooleanQuery.Builder) ResultPage(it.vige.rubia.search.ResultPage) Topic(it.vige.rubia.model.Topic) ModuleException(it.vige.rubia.ModuleException) TermQuery(org.apache.lucene.search.TermQuery) MultiFieldQueryParser(org.apache.lucene.queryparser.classic.MultiFieldQueryParser) SortOrder(it.vige.rubia.search.SortOrder) Term(org.apache.lucene.index.Term) ParseException(org.apache.lucene.queryparser.classic.ParseException) ModuleException(it.vige.rubia.ModuleException) EntityManager(javax.persistence.EntityManager) Searching(it.vige.rubia.search.Searching) StandardAnalyzer(org.apache.lucene.analysis.standard.StandardAnalyzer) ParseException(org.apache.lucene.queryparser.classic.ParseException) FullTextQuery(org.hibernate.search.FullTextQuery)

Aggregations

Builder (org.apache.lucene.search.BooleanQuery.Builder)19 Term (org.apache.lucene.index.Term)14 TermQuery (org.apache.lucene.search.TermQuery)13 ModuleException (it.vige.rubia.ModuleException)8 ResultPage (it.vige.rubia.search.ResultPage)8 Searching (it.vige.rubia.search.Searching)8 SortBy (it.vige.rubia.search.SortBy)8 SortOrder (it.vige.rubia.search.SortOrder)8 EntityManager (javax.persistence.EntityManager)8 StandardAnalyzer (org.apache.lucene.analysis.standard.StandardAnalyzer)8 MultiFieldQueryParser (org.apache.lucene.queryparser.classic.MultiFieldQueryParser)8 ParseException (org.apache.lucene.queryparser.classic.ParseException)8 BooleanQuery (org.apache.lucene.search.BooleanQuery)8 WildcardQuery (org.apache.lucene.search.WildcardQuery)8 FullTextQuery (org.hibernate.search.FullTextQuery)8 FullTextSession (org.hibernate.search.FullTextSession)8 Search.getFullTextSession (org.hibernate.search.Search.getFullTextSession)8 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)6 PrefixQuery (org.apache.lucene.search.PrefixQuery)5 Post (it.vige.rubia.model.Post)4