Search in sources :

Example 1 with SearchEngineFilter

use of org.entando.entando.aps.system.services.searchengine.SearchEngineFilter in project entando-core by entando.

the class SearcherDAO method createQuery.

private Query createQuery(SearchEngineFilter filter) {
    BooleanQuery fieldQuery = new BooleanQuery();
    String key = filter.getKey();
    String attachmentKey = key + IIndexerDAO.ATTACHMENT_FIELD_SUFFIX;
    Object value = filter.getValue();
    if (null != value) {
        if (value instanceof String) {
            SearchEngineFilter.TextSearchOption option = filter.getTextSearchOption();
            if (null == option) {
                option = SearchEngineFilter.TextSearchOption.AT_LEAST_ONE_WORD;
            }
            String stringValue = value.toString();
            String[] values = stringValue.split("\\s+");
            if (!option.equals(SearchEngineFilter.TextSearchOption.EXACT)) {
                BooleanClause.Occur bc = BooleanClause.Occur.SHOULD;
                if (option.equals(SearchEngineFilter.TextSearchOption.ALL_WORDS)) {
                    bc = BooleanClause.Occur.MUST;
                } else if (option.equals(SearchEngineFilter.TextSearchOption.ANY_WORD)) {
                    bc = BooleanClause.Occur.MUST_NOT;
                }
                for (int i = 0; i < values.length; i++) {
                    TermQuery term = new TermQuery(new Term(key, values[i].toLowerCase()));
                    // NOTE: search lower case....
                    if (filter.isIncludeAttachments()) {
                        BooleanQuery compositeQuery = new BooleanQuery();
                        compositeQuery.add(term, BooleanClause.Occur.SHOULD);
                        TermQuery termAttachment = new TermQuery(new Term(attachmentKey, values[i].toLowerCase()));
                        compositeQuery.add(termAttachment, BooleanClause.Occur.SHOULD);
                        fieldQuery.add(compositeQuery, bc);
                    } else {
                        fieldQuery.add(term, bc);
                    }
                }
            } else {
                PhraseQuery phraseQuery = new PhraseQuery();
                for (int i = 0; i < values.length; i++) {
                    // NOTE: search lower case....
                    phraseQuery.add(new Term(key, values[i].toLowerCase()));
                }
                if (filter.isIncludeAttachments()) {
                    fieldQuery.add(phraseQuery, BooleanClause.Occur.SHOULD);
                    PhraseQuery phraseQuery2 = new PhraseQuery();
                    for (int i = 0; i < values.length; i++) {
                        // NOTE: search lower case....
                        phraseQuery2.add(new Term(attachmentKey, values[i].toLowerCase()));
                    }
                    fieldQuery.add(phraseQuery2, BooleanClause.Occur.SHOULD);
                } else {
                    return phraseQuery;
                }
            }
        } else if (value instanceof Date) {
            String toString = DateTools.timeToString(((Date) value).getTime(), DateTools.Resolution.MINUTE);
            TermQuery term = new TermQuery(new Term(filter.getKey(), toString));
            fieldQuery.add(term, BooleanClause.Occur.MUST);
        } else if (value instanceof Number) {
            TermQuery term = new TermQuery(new Term(filter.getKey(), value.toString()));
            fieldQuery.add(term, BooleanClause.Occur.MUST);
        }
    } else {
        if (filter.getStart() instanceof Number || filter.getEnd() instanceof Number) {
        // .............................. TODO
        } else {
            String start = null;
            String end = null;
            if (filter.getStart() instanceof Date || filter.getEnd() instanceof Date) {
                if (null != filter.getStart()) {
                    start = DateTools.timeToString(((Date) filter.getStart()).getTime(), DateTools.Resolution.MINUTE);
                }
                if (null != filter.getEnd()) {
                    end = DateTools.timeToString(((Date) filter.getEnd()).getTime(), DateTools.Resolution.MINUTE);
                }
            } else {
                start = (null != filter.getStart()) ? filter.getStart().toString().toLowerCase() : null;
                end = (null != filter.getEnd()) ? filter.getEnd().toString().toLowerCase() : null;
            }
            BytesRef byteStart = (null != start) ? new BytesRef(start.getBytes()) : null;
            BytesRef byteEnd = (null != end) ? new BytesRef(end.getBytes()) : null;
            TermRangeQuery range = new TermRangeQuery(filter.getKey(), byteStart, byteEnd, true, true);
            fieldQuery.add(range, BooleanClause.Occur.MUST);
        }
    }
    return fieldQuery;
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) TermQuery(org.apache.lucene.search.TermQuery) PhraseQuery(org.apache.lucene.search.PhraseQuery) TermRangeQuery(org.apache.lucene.search.TermRangeQuery) Term(org.apache.lucene.index.Term) Date(java.util.Date) BooleanClause(org.apache.lucene.search.BooleanClause) SearchEngineFilter(org.entando.entando.aps.system.services.searchengine.SearchEngineFilter) BytesRef(org.apache.lucene.util.BytesRef)

Example 2 with SearchEngineFilter

use of org.entando.entando.aps.system.services.searchengine.SearchEngineFilter in project entando-core by entando.

the class TestSearchEngineManager method testSearchContentsId_4.

public void testSearchContentsId_4() throws Throwable {
    try {
        Thread thread = this._searchEngineManager.startReloadContentsReferences();
        thread.join();
        SearchEngineManager sem = (SearchEngineManager) this._searchEngineManager;
        SearchEngineFilter filterByType = new SearchEngineFilter(IIndexerDAO.CONTENT_TYPE_FIELD_NAME, "ART");
        SearchEngineFilter[] filters = { filterByType };
        List<String> allowedGroup = new ArrayList<String>();
        allowedGroup.add(Group.FREE_GROUP_NAME);
        List<String> contentsId = sem.searchEntityId(filters, null, allowedGroup);
        assertNotNull(contentsId);
        String[] expected1 = { "ART180", "ART1", "ART187", "ART121" };
        this.verify(contentsId, expected1);
        Category cat1 = this._categoryManager.getCategory("cat1");
        List<ITreeNode> categories = new ArrayList<ITreeNode>();
        categories.add(cat1);
        contentsId = sem.searchEntityId(filters, categories, allowedGroup);
        assertNotNull(contentsId);
        String[] expected2 = { "ART180" };
        this.verify(contentsId, expected2);
    } catch (Throwable t) {
        throw t;
    }
}
Also used : SearchEngineFilter(org.entando.entando.aps.system.services.searchengine.SearchEngineFilter) Category(com.agiletec.aps.system.services.category.Category) ITreeNode(com.agiletec.aps.system.common.tree.ITreeNode) ArrayList(java.util.ArrayList)

Example 3 with SearchEngineFilter

use of org.entando.entando.aps.system.services.searchengine.SearchEngineFilter in project entando-core by entando.

the class TestSearchEngineManager method testFacetedAllContents.

public void testFacetedAllContents() throws Throwable {
    try {
        Thread thread = this._searchEngineManager.startReloadContentsReferences();
        thread.join();
        SearchEngineManager sem = (SearchEngineManager) this._searchEngineManager;
        Set<String> allowedGroup = new HashSet<String>();
        allowedGroup.add(Group.ADMINS_GROUP_NAME);
        SearchEngineFilter[] filters = {};
        FacetedContentsResult result = sem.searchFacetedEntities(filters, null, allowedGroup);
        assertNotNull(result);
        assertNotNull(result.getContentsId());
        assertNotNull(result.getOccurrences());
        assertTrue(result.getContentsId().size() > 0);
        assertTrue(result.getOccurrences().size() > 0);
    } catch (Throwable t) {
        throw t;
    }
}
Also used : SearchEngineFilter(org.entando.entando.aps.system.services.searchengine.SearchEngineFilter) FacetedContentsResult(org.entando.entando.aps.system.services.searchengine.FacetedContentsResult) HashSet(java.util.HashSet)

Example 4 with SearchEngineFilter

use of org.entando.entando.aps.system.services.searchengine.SearchEngineFilter in project entando-core by entando.

the class TestSearchEngineManager method testSearchContentsId_7.

public void testSearchContentsId_7() throws Throwable {
    try {
        Content content_1 = this.createContent_1();
        this._searchEngineManager.deleteIndexedEntity(content_1.getId());
        this._searchEngineManager.addEntityToIndex(content_1);
        Content content_2 = this.createContent_2();
        this._searchEngineManager.deleteIndexedEntity(content_2.getId());
        this._searchEngineManager.addEntityToIndex(content_2);
        Content content_3 = this.createContent_3();
        this._searchEngineManager.deleteIndexedEntity(content_3.getId());
        this._searchEngineManager.addEntityToIndex(content_3);
        // San Pietroburgo è una città meravigliosa W3C-WAI
        // 100
        // Il turismo ha incrementato più del 20 per cento nel 2011-2013, quando la Croazia ha aderito all'Unione europea. Consegienda di questo aumento è una serie di modernizzazione di alloggi di recente costruzione, tra cui circa tre dozzine di ostelli.
        // 101
        // La vita è una cosa meravigliosa
        // 103
        SearchEngineManager sem = (SearchEngineManager) this._searchEngineManager;
        List<String> allowedGroup = new ArrayList<String>();
        allowedGroup.add(Group.FREE_GROUP_NAME);
        SearchEngineFilter filter1 = new SearchEngineFilter("it", "San meravigliosa", SearchEngineFilter.TextSearchOption.ALL_WORDS);
        SearchEngineFilter[] filters1 = { filter1 };
        List<String> contentsId = sem.searchEntityId(filters1, null, allowedGroup);
        assertNotNull(contentsId);
        assertEquals(1, contentsId.size());
        assertTrue(contentsId.contains(content_1.getId()));
        SearchEngineFilter filter2 = new SearchEngineFilter("it", "San meravigliosa", SearchEngineFilter.TextSearchOption.AT_LEAST_ONE_WORD);
        SearchEngineFilter[] filters2 = { filter2 };
        contentsId = sem.searchEntityId(filters2, null, allowedGroup);
        assertNotNull(contentsId);
        assertEquals(2, contentsId.size());
        assertTrue(contentsId.contains(content_1.getId()));
        assertTrue(contentsId.contains(content_3.getId()));
        SearchEngineFilter filter3 = new SearchEngineFilter("it", "San meravigliosa", SearchEngineFilter.TextSearchOption.EXACT);
        SearchEngineFilter[] filters3 = { filter3 };
        contentsId = sem.searchEntityId(filters3, null, allowedGroup);
        assertNotNull(contentsId);
        assertEquals(0, contentsId.size());
        SearchEngineFilter filter4 = new SearchEngineFilter("it", "una cosa meravigliosa", SearchEngineFilter.TextSearchOption.EXACT);
        SearchEngineFilter[] filters4 = { filter4 };
        contentsId = sem.searchEntityId(filters4, null, allowedGroup);
        assertNotNull(contentsId);
        assertEquals(1, contentsId.size());
        assertTrue(contentsId.contains(content_3.getId()));
    } catch (Throwable t) {
        throw t;
    }
}
Also used : SearchEngineFilter(org.entando.entando.aps.system.services.searchengine.SearchEngineFilter) Content(com.agiletec.plugins.jacms.aps.system.services.content.model.Content) ArrayList(java.util.ArrayList)

Example 5 with SearchEngineFilter

use of org.entando.entando.aps.system.services.searchengine.SearchEngineFilter in project entando-core by entando.

the class TestSearchEngineManager method testSearchContentsId_4.

public void testSearchContentsId_4() throws Throwable {
    try {
        Thread thread = this.dataObjectSearchEngineManager.startReloadDataObjectsReferences();
        thread.join();
        SearchEngineManager sem = (SearchEngineManager) this.dataObjectSearchEngineManager;
        SearchEngineFilter filterByType = new SearchEngineFilter(IIndexerDAO.DATAOBJECT_TYPE_FIELD_NAME, "ART");
        SearchEngineFilter[] filters = { filterByType };
        List<String> allowedGroup = new ArrayList<String>();
        allowedGroup.add(Group.FREE_GROUP_NAME);
        List<String> contentsId = sem.searchEntityId(filters, null, allowedGroup);
        assertNotNull(contentsId);
        String[] expected1 = { "ART180", "ART1", "ART187", "ART121" };
        this.verify(contentsId, expected1);
        Category cat1 = this._categoryManager.getCategory("cat1");
        List<ITreeNode> categories = new ArrayList<ITreeNode>();
        categories.add(cat1);
        contentsId = sem.searchEntityId(filters, categories, allowedGroup);
        assertNotNull(contentsId);
        String[] expected2 = { "ART180" };
        this.verify(contentsId, expected2);
    } catch (Throwable t) {
        throw t;
    }
}
Also used : SearchEngineFilter(org.entando.entando.aps.system.services.searchengine.SearchEngineFilter) Category(com.agiletec.aps.system.services.category.Category) ITreeNode(com.agiletec.aps.system.common.tree.ITreeNode) IDataObjectSearchEngineManager(org.entando.entando.aps.system.services.dataobjectsearchengine.IDataObjectSearchEngineManager) SearchEngineManager(org.entando.entando.aps.system.services.dataobjectsearchengine.SearchEngineManager) ArrayList(java.util.ArrayList)

Aggregations

SearchEngineFilter (org.entando.entando.aps.system.services.searchengine.SearchEngineFilter)15 ITreeNode (com.agiletec.aps.system.common.tree.ITreeNode)4 ArrayList (java.util.ArrayList)4 Term (org.apache.lucene.index.Term)4 IDataObjectSearchEngineManager (org.entando.entando.aps.system.services.dataobjectsearchengine.IDataObjectSearchEngineManager)4 SearchEngineManager (org.entando.entando.aps.system.services.dataobjectsearchengine.SearchEngineManager)4 Date (java.util.Date)3 HashSet (java.util.HashSet)3 AttributeInterface (com.agiletec.aps.system.common.entity.model.attribute.AttributeInterface)2 BooleanAttribute (com.agiletec.aps.system.common.entity.model.attribute.BooleanAttribute)2 DateAttribute (com.agiletec.aps.system.common.entity.model.attribute.DateAttribute)2 ITextAttribute (com.agiletec.aps.system.common.entity.model.attribute.ITextAttribute)2 NumberAttribute (com.agiletec.aps.system.common.entity.model.attribute.NumberAttribute)2 ApsSystemException (com.agiletec.aps.system.exception.ApsSystemException)2 Category (com.agiletec.aps.system.services.category.Category)2 BigDecimal (java.math.BigDecimal)2 BooleanQuery (org.apache.lucene.search.BooleanQuery)2 PhraseQuery (org.apache.lucene.search.PhraseQuery)2 TermQuery (org.apache.lucene.search.TermQuery)2 TermRangeQuery (org.apache.lucene.search.TermRangeQuery)2