您的位置:首页 > 其它

DDL Lucene Search Demo

2018-01-02 00:00 399 查看

DDL Lucene Search Demo

/**
3     * Executes a full-text search using Liferay search engine (currently Apache Lucene) and its index. You must use at
4     * least one Search Term (custom attributes, attributes, or content)
5     *
6     * @param customAttributes
7     *            - A map containing custom attributes to search for
8     * @param contentString
9     *            - A List of Strings to be searched inside the content
10     * @param attributes
11     *            - A map containing the attribute name as the key, and the array of values to search for as the value
12     * @param userId
13     *            - the portal ID of the current user
14     * @param exactTerm
15     *            - true to search for the exact terms, false to use 'like'
16     * @param allTerms
17     *            - true to obligatory find all terms in each result
18     * @param quotableId
19     *            - the exact ID of the Quotable object related to the document (optional, can be null)
20     * @param quotableType
21     *            - the type of quotable ID to search (required when quotableId is filled)
22     * @param fileTypeId
23     *            - the identifier (aka. ID or IDT) of the document type you want to find (optional, can be null)
24     * @param startDate
25     *            - the date to start the search (optional, can be null)
26     * @param endDate
27     *            - the date do end the search (optional, can be null)
28     * @return search hits containing an array of Document if any found
29     * @throws Exception
30     *             - in case of an error during the operation. The error is already being logged.
31     */
32    public static Hits doFullTextSearch(Map<String, String[]> customAttributes, Map<String, String[]> attributes, long userId,
33            ArrayList<String> contentString, boolean exactTerm, boolean allTerms, Long quotableId, Class quotableType, Long fileTypeId,
34            Date startDate, Date endDate) throws Exception {
35        SearchContext searchContext = new SearchContext();
36
37        BooleanClauseOccur termOccur = allTerms ? BooleanClauseOccur.MUST : BooleanClauseOccur.SHOULD;
38
39        try {
40            Indexer indexer = IndexerRegistryUtil.getIndexer(DLFileEntryConstants.getClassName());
41
42            searchContext.setCompanyId(getCompanyId());
43            Long[] arrayLong = new Long[1];
44            searchContext.setGroupIds(ArrayUtil.toArray(getUserGroupsIds(userId).toArray(arrayLong)));
45
46            BooleanQuery mainQuery = indexer.getFacetQuery(DLFileEntry.class.getName(), searchContext);
47
48            if (MapUtils.isNotEmpty(customAttributes)) {
49                BooleanQuery customAttrQuery = BooleanQueryFactoryUtil.create(searchContext);
50                for (Entry<String, String[]> entry : customAttributes.entrySet()) {
51                    for (String value : entry.getValue()) {
52                        customAttrQuery.addTerm(ExpandoBridgeIndexerUtil.encodeFieldName(entry.getKey()), value, !exactTerm, termOccur);
53                    }
54                }
55                mainQuery.add(customAttrQuery, BooleanClauseOccur.SHOULD);
56            }
57            if (MapUtils.isNotEmpty(attributes)) {
58                BooleanQuery attrQuery = BooleanQueryFactoryUtil.create(searchContext);
59                for (Entry<String, String[]> entry : attributes.entrySet()) {
60                    for (String value : entry.getValue()) {
61                        attrQuery.addTerm(entry.getKey(), value, !exactTerm, termOccur);
62                    }
63                }
64                mainQuery.add(attrQuery, BooleanClauseOccur.SHOULD);
65            }
66            if (CollectionUtils.isNotEmpty(contentString)) {
67                BooleanQuery contentQuery = BooleanQueryFactoryUtil.create(searchContext);
68                for (String value : contentString) {
69                    contentQuery.addTerm(Field.CONTENT, value, !exactTerm, termOccur);
70                }
71                mainQuery.add(contentQuery, BooleanClauseOccur.SHOULD);
72            }
73
74            if ((quotableId != null) && (quotableType != null)) {
75                mainQuery.addTerm(ExpandoBridgeIndexerUtil.encodeFieldName(FIELD_QUOTABLE_ID), "" + quotableId.longValue(), false,
76                        BooleanClauseOccur.MUST);
77                mainQuery.addTerm(ExpandoBridgeIndexerUtil.encodeFieldName(FIELD_QUOTABLE_TYPE), quotableType.getSimpleName(), false,
78                        BooleanClauseOccur.MUST);
79            }
80
81            if (fileTypeId != null) {
82                mainQuery.addTerm(ExpandoBridgeIndexerUtil.encodeFieldName(FIELD_FILE_TYPE_ID), "" + fileTypeId.longValue(), false,
83                        BooleanClauseOccur.MUST);
84            }
85
86            if ((startDate != null) || (endDate != null)) {
87                BooleanQuery dateQuery = BooleanQueryFactoryUtil.create(searchContext);
88                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
89
90                String start = null;
91                String end = null;
92
93                if (startDate == null) {
94                    start = "19700101000000";
95                    end = dateFormat.format(endDate) + "000000";
96                } else if (endDate == null) {
97                    Calendar tomorrow = Calendar.getInstance();
98                    tomorrow.add(Calendar.DAY_OF_MONTH, 10);
99                    start = dateFormat.format(startDate) + "000000";
100                    end = dateFormat.format(tomorrow.getTime()) + "000000";
101                } else {
102                    start = dateFormat.format(startDate) + "000000";
103                    end = dateFormat.format(endDate) + "000000";
104                }
105
106                dateQuery.addRangeTerm(Field.CREATE_DATE, start, end);
107                mainQuery.add(dateQuery, BooleanClauseOccur.MUST);
108            }
109
110            return SearchEngineUtil.search(indexer.getSearchEngineId(), getCompanyId(), mainQuery, -1, -1);
111        } catch (Exception ex) {
112            log.log(Level.SEVERE, ex.getMessage(), ex);
113            throw ex;
114        }
115    }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: