/
BaseIndexer.java
1577 lines (1198 loc) · 41.5 KB
/
BaseIndexer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* SPDX-FileCopyrightText: (c) 2000 Liferay, Inc. https://liferay.com
* SPDX-License-Identifier: LGPL-2.1-or-later OR LicenseRef-Liferay-DXP-EULA-2.0.0-2023-06
*/
package com.liferay.portal.kernel.search;
import com.liferay.asset.kernel.AssetRendererFactoryRegistryUtil;
import com.liferay.asset.kernel.model.AssetEntry;
import com.liferay.asset.kernel.model.AssetRendererFactory;
import com.liferay.expando.kernel.model.ExpandoBridge;
import com.liferay.osgi.service.tracker.collections.list.ServiceTrackerList;
import com.liferay.osgi.service.tracker.collections.list.ServiceTrackerListFactory;
import com.liferay.petra.lang.HashUtil;
import com.liferay.petra.string.StringBundler;
import com.liferay.petra.string.StringPool;
import com.liferay.portal.kernel.change.tracking.CTCollectionThreadLocal;
import com.liferay.portal.kernel.exception.NoSuchCountryException;
import com.liferay.portal.kernel.exception.NoSuchModelException;
import com.liferay.portal.kernel.exception.NoSuchRegionException;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.language.LanguageUtil;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.model.Address;
import com.liferay.portal.kernel.model.BaseModel;
import com.liferay.portal.kernel.model.Country;
import com.liferay.portal.kernel.model.Group;
import com.liferay.portal.kernel.model.Region;
import com.liferay.portal.kernel.model.ResourcedModel;
import com.liferay.portal.kernel.model.WorkflowedModel;
import com.liferay.portal.kernel.model.change.tracking.CTModel;
import com.liferay.portal.kernel.module.service.Snapshot;
import com.liferay.portal.kernel.module.util.SystemBundleUtil;
import com.liferay.portal.kernel.search.facet.Facet;
import com.liferay.portal.kernel.search.facet.MultiValueFacet;
import com.liferay.portal.kernel.search.filter.BooleanFilter;
import com.liferay.portal.kernel.search.filter.Filter;
import com.liferay.portal.kernel.search.filter.QueryFilter;
import com.liferay.portal.kernel.search.filter.TermsFilter;
import com.liferay.portal.kernel.search.generic.BooleanQueryImpl;
import com.liferay.portal.kernel.search.hits.HitsProcessorRegistryUtil;
import com.liferay.portal.kernel.security.auth.CompanyThreadLocal;
import com.liferay.portal.kernel.security.permission.PermissionChecker;
import com.liferay.portal.kernel.security.permission.PermissionThreadLocal;
import com.liferay.portal.kernel.service.CountryServiceUtil;
import com.liferay.portal.kernel.service.GroupLocalServiceUtil;
import com.liferay.portal.kernel.service.RegionServiceUtil;
import com.liferay.portal.kernel.util.ArrayUtil;
import com.liferay.portal.kernel.util.GetterUtil;
import com.liferay.portal.kernel.util.LocaleUtil;
import com.liferay.portal.kernel.util.LocalizationUtil;
import com.liferay.portal.kernel.util.PropsKeys;
import com.liferay.portal.kernel.util.PropsUtil;
import com.liferay.portal.kernel.util.SetUtil;
import com.liferay.portal.kernel.util.StringUtil;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.portal.kernel.workflow.WorkflowConstants;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import javax.portlet.PortletRequest;
import javax.portlet.PortletResponse;
/**
* @author Brian Wing Shun Chan
* @author Hugo Huijser
* @author Ryan Park
* @author Raymond Augé
*/
public abstract class BaseIndexer<T> implements Indexer<T> {
@Override
public void delete(long companyId, String uid) throws SearchException {
try {
IndexWriterHelperUtil.deleteDocument(
companyId, uid, _commitImmediately);
}
catch (SearchException searchException) {
throw searchException;
}
catch (Exception exception) {
throw new SearchException(exception);
}
}
@Override
public void delete(T object) throws SearchException {
if (object == null) {
return;
}
if (object instanceof CTModel<?>) {
CTModel<?> ctModel = (CTModel<?>)object;
if ((ctModel.getCtCollectionId() == 0) &&
!CTCollectionThreadLocal.isProductionMode()) {
return;
}
}
try {
doDelete(object);
}
catch (SearchException searchException) {
throw searchException;
}
catch (Exception exception) {
throw new SearchException(exception);
}
}
@Override
public boolean equals(Object object) {
if (object == this) {
return true;
}
if (!(object instanceof Indexer<?>)) {
return false;
}
Indexer<?> indexer = (Indexer<?>)object;
return Objects.equals(getClassName(), indexer.getClassName());
}
@Override
public Document getDocument(T object) throws SearchException {
try {
Document document = doGetDocument(object);
for (IndexerPostProcessor indexerPostProcessor :
IndexerRegistryUtil.getIndexerPostProcessors(this)) {
indexerPostProcessor.postProcessDocument(document, object);
}
if (document == null) {
return null;
}
Map<String, Field> fields = document.getFields();
Field groupIdField = fields.get(Field.GROUP_ID);
if (groupIdField != null) {
long groupId = GetterUtil.getLong(groupIdField.getValue());
addStagingGroupKeyword(document, groupId);
}
return document;
}
catch (SearchException searchException) {
throw searchException;
}
catch (Exception exception) {
throw new SearchException(exception);
}
}
@Override
public BooleanFilter getFacetBooleanFilter(
String className, SearchContext searchContext)
throws Exception {
return null;
}
@Override
public BooleanQuery getFullQuery(SearchContext searchContext)
throws SearchException {
try {
resetFullQuery(searchContext);
String[] fullQueryEntryClassNames =
searchContext.getFullQueryEntryClassNames();
if (ArrayUtil.isNotEmpty(fullQueryEntryClassNames)) {
searchContext.setAttribute(
"relatedEntryClassNames", getSearchClassNames());
}
String[] entryClassNames = ArrayUtil.append(
getSearchClassNames(), fullQueryEntryClassNames);
searchContext.setEntryClassNames(entryClassNames);
BooleanFilter fullQueryBooleanFilter = new BooleanFilter();
addSearchAssetCategoryIds(fullQueryBooleanFilter, searchContext);
addSearchAssetTagNames(fullQueryBooleanFilter, searchContext);
addSearchFolderId(fullQueryBooleanFilter, searchContext);
addSearchLayout(fullQueryBooleanFilter, searchContext);
addSearchUserId(fullQueryBooleanFilter, searchContext);
_addPreFilters(
fullQueryBooleanFilter,
_getEntryClassNameIndexerMap(entryClassNames), searchContext);
BooleanQuery fullQuery = createFullQuery(
fullQueryBooleanFilter, searchContext);
fullQuery.setQueryConfig(searchContext.getQueryConfig());
return fullQuery;
}
catch (SearchException searchException) {
throw searchException;
}
catch (Exception exception) {
throw new SearchException(exception);
}
}
@Override
public IndexerPostProcessor[] getIndexerPostProcessors() {
List<IndexerPostProcessor> indexerPostProcessors =
IndexerRegistryUtil.getIndexerPostProcessors(this);
return indexerPostProcessors.toArray(new IndexerPostProcessor[0]);
}
@Override
public String[] getSearchClassNames() {
return new String[] {getClassName()};
}
/**
* @deprecated As of Judson (7.1.x), replaced by {@link
* com.liferay.portal.search.sort.SortFieldBuilder
* #getSortField}
*/
@Deprecated
@Override
public String getSortField(String orderByCol) {
String sortField = doGetSortField(orderByCol);
if (_document.isDocumentSortableTextField(sortField)) {
return Field.getSortableFieldName(sortField);
}
return sortField;
}
@Override
public Summary getSummary(
Document document, String snippet, PortletRequest portletRequest,
PortletResponse portletResponse)
throws SearchException {
try {
Locale locale = getLocale(portletRequest);
Summary summary = doGetSummary(
document, locale, snippet, portletRequest, portletResponse);
for (IndexerPostProcessor indexerPostProcessor :
IndexerRegistryUtil.getIndexerPostProcessors(this)) {
indexerPostProcessor.postProcessSummary(
summary, document, locale, snippet);
}
return summary;
}
catch (SearchException searchException) {
throw searchException;
}
catch (Exception exception) {
throw new SearchException(exception);
}
}
@Override
public int hashCode() {
return HashUtil.hash(0, getClassName());
}
@Override
public boolean hasPermission(
PermissionChecker permissionChecker, String entryClassName,
long entryClassPK, String actionId)
throws Exception {
return true;
}
@Override
public boolean isCommitImmediately() {
return _commitImmediately;
}
@Override
public boolean isFilterSearch() {
return _filterSearch;
}
@Override
public boolean isIndexerEnabled() {
if (_indexerEnabled == null) {
String indexerEnabled = PropsUtil.get(
PropsKeys.INDEXER_ENABLED,
new com.liferay.portal.kernel.configuration.Filter(
getClassName()));
_indexerEnabled = GetterUtil.getBoolean(indexerEnabled, true);
return _indexerEnabled;
}
return _indexerEnabled;
}
@Override
public boolean isPermissionAware() {
return _permissionAware;
}
public boolean isSelectAllLocales() {
return _selectAllLocales;
}
@Override
public boolean isStagingAware() {
return _stagingAware;
}
@Override
public boolean isVisible(long classPK, int status) throws Exception {
return true;
}
@Override
public void postProcessContextBooleanFilter(
BooleanFilter contextBooleanFilter, SearchContext searchContext)
throws Exception {
}
/**
* @deprecated As of Wilberforce (7.0.x), replaced by {@link
* #postProcessContextBooleanFilter(BooleanFilter,
* SearchContext)}
*/
@Deprecated
@Override
public void postProcessContextQuery(
BooleanQuery contextQuery, SearchContext searchContext)
throws Exception {
}
@Override
public void postProcessSearchQuery(
BooleanQuery searchQuery, BooleanFilter fullQueryBooleanFilter,
SearchContext searchContext)
throws Exception {
if (Validator.isNull(searchContext.getKeywords())) {
addSearchTerm(searchQuery, searchContext, Field.DESCRIPTION, false);
addSearchTerm(searchQuery, searchContext, Field.TITLE, false);
addSearchTerm(searchQuery, searchContext, Field.USER_NAME, false);
}
}
/**
* @deprecated As of Wilberforce (7.0.x), replaced by {@link
* #postProcessSearchQuery(BooleanQuery, BooleanFilter,
* SearchContext)}
*/
@Deprecated
@Override
public void postProcessSearchQuery(
BooleanQuery searchQuery, SearchContext searchContext)
throws Exception {
}
@Override
public void reindex(Collection<T> collection) {
if (IndexWriterHelperUtil.isIndexReadOnly() ||
IndexWriterHelperUtil.isIndexReadOnly(getClassName()) ||
!isIndexerEnabled() || collection.isEmpty()) {
return;
}
for (T element : collection) {
try {
reindex(element);
}
catch (SearchException searchException) {
_log.error(
"Unable to index object: " + element, searchException);
}
}
}
@Override
public void reindex(String className, long classPK) throws SearchException {
try {
if (IndexWriterHelperUtil.isIndexReadOnly() ||
IndexWriterHelperUtil.isIndexReadOnly(getClassName()) ||
!isIndexerEnabled() || (classPK <= 0)) {
return;
}
doReindex(className, classPK);
}
catch (NoSuchModelException noSuchModelException) {
if (_log.isWarnEnabled()) {
_log.warn(
StringBundler.concat(
"Unable to index ", className, " ", classPK),
noSuchModelException);
}
}
catch (SearchException searchException) {
throw searchException;
}
catch (Exception exception) {
throw new SearchException(exception);
}
}
@Override
public void reindex(String[] ids) throws SearchException {
long companyThreadLocalCompanyId = CompanyThreadLocal.getCompanyId();
try {
if (IndexWriterHelperUtil.isIndexReadOnly() ||
IndexWriterHelperUtil.isIndexReadOnly(getClassName()) ||
!isIndexerEnabled()) {
return;
}
if (ids.length > 0) {
CompanyThreadLocal.setCompanyId(GetterUtil.getLong(ids[0]));
}
doReindex(ids);
}
catch (SearchException searchException) {
throw searchException;
}
catch (Exception exception) {
throw new SearchException(exception);
}
finally {
CompanyThreadLocal.setCompanyId(companyThreadLocalCompanyId);
}
}
@Override
public void reindex(T object) throws SearchException {
try {
if (IndexWriterHelperUtil.isIndexReadOnly() ||
IndexWriterHelperUtil.isIndexReadOnly(getClassName()) ||
!isIndexerEnabled() || (object == null)) {
return;
}
doReindex(object);
}
catch (SearchException searchException) {
throw searchException;
}
catch (Exception exception) {
throw new SearchException(exception);
}
}
@Override
public Hits search(SearchContext searchContext) throws SearchException {
try {
QueryConfig queryConfig = searchContext.getQueryConfig();
addDefaultHighlightFieldNames(queryConfig);
if (ArrayUtil.isEmpty(queryConfig.getSelectedFieldNames())) {
addDefaultSelectedFieldNames(searchContext);
}
addFacetSelectedFieldNames(searchContext, queryConfig);
Hits hits = _search(searchContext);
processHits(searchContext, hits);
return hits;
}
catch (SearchException searchException) {
throw searchException;
}
catch (Exception exception) {
throw new SearchException(exception);
}
}
@Override
public Hits search(
SearchContext searchContext, String... selectedFieldNames)
throws SearchException {
QueryConfig queryConfig = searchContext.getQueryConfig();
queryConfig.setSelectedFieldNames(selectedFieldNames);
return search(searchContext);
}
@Override
public long searchCount(SearchContext searchContext)
throws SearchException {
PermissionChecker permissionChecker =
PermissionThreadLocal.getPermissionChecker();
if ((permissionChecker != null) &&
isUseSearchResultPermissionFilter(searchContext)) {
Hits hits = search(searchContext);
return hits.getLength();
}
QueryConfig queryConfig = searchContext.getQueryConfig();
queryConfig.setHighlightEnabled(false);
queryConfig.setHitsProcessingEnabled(false);
queryConfig.setScoreEnabled(false);
queryConfig.setQueryIndexingEnabled(false);
queryConfig.setQuerySuggestionEnabled(false);
BooleanQuery fullQuery = getFullQuery(searchContext);
fullQuery.setQueryConfig(queryConfig);
return IndexSearcherHelperUtil.searchCount(searchContext, fullQuery);
}
public void setCommitImmediately(boolean commitImmediately) {
_commitImmediately = commitImmediately;
}
@Override
public void setIndexerEnabled(boolean indexerEnabled) {
_indexerEnabled = indexerEnabled;
}
public void setSelectAllLocales(boolean selectAllLocales) {
_selectAllLocales = selectAllLocales;
}
protected void addDefaultHighlightFieldNames(QueryConfig queryConfig) {
queryConfig.addHighlightFieldNames(Field.ASSET_CATEGORY_TITLES);
if (queryConfig.isHighlightEnabled()) {
queryConfig.addHighlightFieldNames(
Field.CONTENT, Field.DESCRIPTION, Field.TITLE);
}
}
protected void addDefaultSelectedFieldNames(SearchContext searchContext) {
QueryConfig queryConfig = searchContext.getQueryConfig();
Set<String> selectedFieldNames = null;
if (!ArrayUtil.isEmpty(getDefaultSelectedFieldNames())) {
selectedFieldNames = SetUtil.fromArray(
getDefaultSelectedFieldNames());
if (searchContext.isIncludeAttachments() ||
searchContext.isIncludeDiscussions()) {
selectedFieldNames.add(Field.CLASS_NAME_ID);
selectedFieldNames.add(Field.CLASS_PK);
}
}
if (!ArrayUtil.isEmpty(getDefaultSelectedLocalizedFieldNames())) {
if (selectedFieldNames == null) {
selectedFieldNames = new HashSet<>();
}
if (isSelectAllLocales()) {
addSelectedLocalizedFieldNames(
selectedFieldNames,
LocaleUtil.toLanguageIds(
LanguageUtil.getAvailableLocales()));
}
else {
addSelectedLocalizedFieldNames(
selectedFieldNames,
LocaleUtil.toLanguageId(queryConfig.getLocale()));
}
}
if ((selectedFieldNames != null) && !selectedFieldNames.isEmpty()) {
queryConfig.setSelectedFieldNames(
selectedFieldNames.toArray(new String[0]));
}
}
/**
* @deprecated As of Wilberforce (7.0.x)
*/
@Deprecated
protected void addFacetClause(
SearchContext searchContext, BooleanFilter facetBooleanFilter,
Collection<Facet> facets)
throws ParseException {
BooleanQuery facetBooleanQuery = new BooleanQueryImpl();
for (Facet facet : facets) {
BooleanClause<Query> facetBooleanClause = facet.getFacetClause();
if (facetBooleanClause != null) {
facetBooleanQuery.add(
facetBooleanClause.getClause(),
facetBooleanClause.getBooleanClauseOccur());
}
}
if (!facetBooleanQuery.hasClauses()) {
return;
}
QueryFilter queryFilter = new QueryFilter(facetBooleanQuery);
facetBooleanFilter.add(queryFilter, BooleanClauseOccur.MUST);
}
protected void addFacetSelectedFieldNames(
SearchContext searchContext, QueryConfig queryConfig) {
String[] selectedFieldNames = queryConfig.getSelectedFieldNames();
if (ArrayUtil.isEmpty(selectedFieldNames) ||
((selectedFieldNames.length == 1) &&
selectedFieldNames[0].equals(Field.ANY))) {
return;
}
Set<String> selectedFieldNameSet = SetUtil.fromArray(
selectedFieldNames);
Map<String, Facet> facets = searchContext.getFacets();
selectedFieldNameSet.addAll(facets.keySet());
selectedFieldNames = selectedFieldNameSet.toArray(new String[0]);
queryConfig.setSelectedFieldNames(selectedFieldNames);
}
protected void addLocalizedField(
Document document, String field, Locale siteDefaultLocale,
Map<Locale, String> map) {
for (Map.Entry<Locale, String> entry : map.entrySet()) {
Locale locale = entry.getKey();
if (locale.equals(siteDefaultLocale)) {
document.addText(field, entry.getValue());
}
String languageId = LocaleUtil.toLanguageId(locale);
document.addText(
LocalizationUtil.getLocalizedName(field, languageId),
entry.getValue());
}
}
protected void addSearchAssetCategoryIds(
BooleanFilter queryBooleanFilter, SearchContext searchContext)
throws Exception {
MultiValueFacet multiValueFacet = new MultiValueFacet(searchContext);
multiValueFacet.setFieldName(Field.ASSET_CATEGORY_IDS);
multiValueFacet.setStatic(true);
multiValueFacet.setValues(searchContext.getAssetCategoryIds());
searchContext.addFacet(multiValueFacet);
}
protected void addSearchAssetTagNames(
BooleanFilter queryBooleanFilter, SearchContext searchContext)
throws Exception {
MultiValueFacet multiValueFacet = new MultiValueFacet(searchContext);
multiValueFacet.setFieldName(Field.ASSET_TAG_NAMES);
multiValueFacet.setStatic(true);
multiValueFacet.setValues(searchContext.getAssetTagNames());
searchContext.addFacet(multiValueFacet);
}
protected Filter addSearchClassTypeIds(
BooleanFilter contextBooleanFilter, SearchContext searchContext)
throws Exception {
long[] classTypeIds = searchContext.getClassTypeIds();
if (ArrayUtil.isEmpty(classTypeIds)) {
return null;
}
TermsFilter classTypeIdsTermsFilter = new TermsFilter(
Field.CLASS_TYPE_ID);
classTypeIdsTermsFilter.addValues(
ArrayUtil.toStringArray(classTypeIds));
return contextBooleanFilter.add(
classTypeIdsTermsFilter, BooleanClauseOccur.MUST);
}
protected void addSearchEntryClassNames(
BooleanFilter queryBooleanFilter, SearchContext searchContext)
throws Exception {
}
protected Map<String, Query> addSearchExpando(
BooleanQuery searchQuery, SearchContext searchContext,
String keywords)
throws Exception {
ExpandoQueryContributor expandoQueryContributor =
_expandoQueryContributorSnapshot.get();
expandoQueryContributor.contribute(
keywords, searchQuery, getSearchClassNames(), searchContext);
return new HashMap<>();
}
protected void addSearchFolderId(
BooleanFilter queryBooleanFilter, SearchContext searchContext)
throws Exception {
MultiValueFacet multiValueFacet = new MultiValueFacet(searchContext);
multiValueFacet.setFieldName(Field.TREE_PATH);
multiValueFacet.setStatic(true);
long[] folderIds = searchContext.getFolderIds();
if (ArrayUtil.isNotEmpty(folderIds)) {
folderIds = ArrayUtil.remove(folderIds, _DEFAULT_FOLDER_ID);
multiValueFacet.setValues(folderIds);
}
searchContext.addFacet(multiValueFacet);
}
protected Map<String, Query> addSearchKeywords(
BooleanQuery searchQuery, SearchContext searchContext)
throws Exception {
AddSearchKeywordsQueryContributor addSearchKeywordsQueryContributor =
_addSearchKeywordsQueryContributorSnapshot.get();
addSearchKeywordsQueryContributor.contribute(
searchQuery, searchContext);
return addSearchExpando(
searchQuery, searchContext, searchContext.getKeywords());
}
protected void addSearchLayout(
BooleanFilter queryBooleanFilter, SearchContext searchContext)
throws Exception {
MultiValueFacet multiValueFacet = new MultiValueFacet(searchContext);
multiValueFacet.setFieldName(Field.LAYOUT_UUID);
multiValueFacet.setStatic(true);
searchContext.addFacet(multiValueFacet);
}
protected Map<String, Query> addSearchLocalizedTerm(
BooleanQuery searchQuery, SearchContext searchContext, String field,
boolean like)
throws Exception {
Map<String, Query> queries = new HashMap<>();
queries.put(
field, addSearchTerm(searchQuery, searchContext, field, like));
String localizedFieldName = Field.getLocalizedName(
searchContext.getLocale(), field);
Query localizedQuery = addSearchTerm(
searchQuery, searchContext, localizedFieldName, like);
queries.put(localizedFieldName, localizedQuery);
return queries;
}
protected Query addSearchTerm(
BooleanQuery searchQuery, SearchContext searchContext, String field,
boolean like)
throws Exception {
if (Validator.isNull(field)) {
return null;
}
String value = null;
Serializable serializable = searchContext.getAttribute(field);
if (serializable != null) {
Class<?> clazz = serializable.getClass();
if (clazz.isArray()) {
value = StringUtil.merge((Object[])serializable);
}
else {
value = GetterUtil.getString(serializable);
}
}
else {
value = GetterUtil.getString(serializable);
}
if (Validator.isNotNull(value) &&
(searchContext.getFacet(field) != null)) {
return null;
}
if (Validator.isNull(value)) {
value = searchContext.getKeywords();
}
if (Validator.isNull(value)) {
return null;
}
Query query = null;
if (searchContext.isAndSearch()) {
query = searchQuery.addRequiredTerm(field, value, like);
}
else {
query = searchQuery.addTerm(field, value, like);
}
return query;
}
protected void addSearchUserId(
BooleanFilter queryBooleanFilter, SearchContext searchContext)
throws Exception {
MultiValueFacet multiValueFacet = new MultiValueFacet(searchContext);
multiValueFacet.setFieldName(Field.USER_ID);
multiValueFacet.setStatic(true);
long userId = GetterUtil.getLong(
searchContext.getAttribute(Field.USER_ID));
if (userId > 0) {
multiValueFacet.setValues(new long[] {userId});
}
searchContext.addFacet(multiValueFacet);
}
protected void addSelectedLocalizedFieldNames(
Set<String> selectedFieldNames, String... languageIds) {
for (String defaultLocalizedSelectedFieldName :
getDefaultSelectedLocalizedFieldNames()) {
selectedFieldNames.add(defaultLocalizedSelectedFieldName);
for (String languageId : languageIds) {
String localizedFieldName = LocalizationUtil.getLocalizedName(
defaultLocalizedSelectedFieldName, languageId);
selectedFieldNames.add(localizedFieldName);
}
}
}
protected void addStagingGroupKeyword(Document document, long groupId) {
if (!isStagingAware()) {
return;
}
document.addKeyword(Field.STAGING_GROUP, isStagingGroup(groupId));
}
protected void addStatus(
BooleanFilter contextBooleanFilter, SearchContext searchContext)
throws Exception {
int[] statuses = GetterUtil.getIntegerValues(
searchContext.getAttribute(Field.STATUS), null);
if (ArrayUtil.isEmpty(statuses)) {
int status = GetterUtil.getInteger(
searchContext.getAttribute(Field.STATUS),
WorkflowConstants.STATUS_APPROVED);
statuses = new int[] {status};
}
if (!ArrayUtil.contains(statuses, WorkflowConstants.STATUS_ANY)) {
TermsFilter statusesTermsFilter = new TermsFilter(Field.STATUS);
statusesTermsFilter.addValues(ArrayUtil.toStringArray(statuses));
contextBooleanFilter.add(
statusesTermsFilter, BooleanClauseOccur.MUST);
}
else {
contextBooleanFilter.addTerm(
Field.STATUS, String.valueOf(WorkflowConstants.STATUS_IN_TRASH),
BooleanClauseOccur.MUST_NOT);
}
}
protected BooleanQuery createFullQuery(
BooleanFilter fullQueryBooleanFilter, SearchContext searchContext)
throws Exception {
BooleanQuery searchQuery = new BooleanQueryImpl();
addSearchKeywords(searchQuery, searchContext);
_postProcessSearchQuery(
searchQuery, fullQueryBooleanFilter, Arrays.asList(this),
searchContext);
doPostProcessSearchQuery(this, searchQuery, searchContext);
Map<String, Facet> facets = searchContext.getFacets();
BooleanFilter facetBooleanFilter = new BooleanFilter();
addFacetClause(searchContext, facetBooleanFilter, facets.values());
if (facetBooleanFilter.hasClauses()) {
fullQueryBooleanFilter.add(
facetBooleanFilter, BooleanClauseOccur.MUST);
}
BooleanQuery fullBooleanQuery = new BooleanQueryImpl();
if (fullQueryBooleanFilter.hasClauses()) {
fullBooleanQuery.setPreBooleanFilter(fullQueryBooleanFilter);
}
if (searchQuery.hasClauses()) {
fullBooleanQuery.add(searchQuery, BooleanClauseOccur.MUST);
}
BooleanClause<Query>[] booleanClauses =
searchContext.getBooleanClauses();
if (booleanClauses != null) {
for (BooleanClause<Query> booleanClause : booleanClauses) {
fullBooleanQuery.add(
booleanClause.getClause(),
booleanClause.getBooleanClauseOccur());
}
}