42
42
import java .io .IOException ;
43
43
import java .util .*;
44
44
import java .util .stream .Collectors ;
45
+ import org .apache .lucene .search .join .ScoreMode ;
45
46
import org .elasticsearch .index .query .BoolQueryBuilder ;
46
47
import org .elasticsearch .index .query .QueryBuilder ;
47
48
import org .elasticsearch .index .query .QueryBuilders ;
@@ -64,7 +65,6 @@ public class ElasticSearchEntityTagsProvider implements EntityTagsProvider {
64
65
65
66
private final String activeElasticSearchIndex ;
66
67
67
- private final boolean singleMappingType ;
68
68
private final String mappingTypeName ;
69
69
70
70
@ Autowired
@@ -81,7 +81,6 @@ public ElasticSearchEntityTagsProvider(
81
81
this .front50Service = front50Service ;
82
82
this .jestClient = jestClient ;
83
83
this .activeElasticSearchIndex = elasticSearchConfigProperties .getActiveIndex ();
84
- this .singleMappingType = elasticSearchConfigProperties .isSingleMappingType ();
85
84
this .mappingTypeName = elasticSearchConfigProperties .getMappingTypeName ();
86
85
}
87
86
@@ -131,6 +130,11 @@ public Collection<EntityTags> getAll(
131
130
queryBuilder = queryBuilder .must (QueryBuilders .wildcardQuery ("id" , idPrefix ));
132
131
}
133
132
133
+ if (entityType != null ) {
134
+ queryBuilder =
135
+ queryBuilder .must (QueryBuilders .wildcardQuery ("entityRef.entityType" , entityType ));
136
+ }
137
+
134
138
if (tags != null ) {
135
139
for (Map .Entry <String , Object > entry : tags .entrySet ()) {
136
140
// each key/value pair maps to a distinct nested `tags` object and must be a unique query
@@ -150,7 +154,7 @@ public Collection<EntityTags> getAll(
150
154
queryBuilder = queryBuilder .must (applyTagsToBuilder (namespace , Collections .emptyMap ()));
151
155
}
152
156
153
- return search (entityType , queryBuilder , maxResults );
157
+ return search (queryBuilder , maxResults );
154
158
}
155
159
156
160
@ Override
@@ -173,7 +177,7 @@ public Optional<EntityTags> get(String id, Map<String, Object> tags) {
173
177
}
174
178
}
175
179
176
- List <EntityTags > entityTags = search (null , queryBuilder , 1 );
180
+ List <EntityTags > entityTags = search (queryBuilder , 1 );
177
181
return entityTags .isEmpty () ? Optional .empty () : Optional .of (entityTags .get (0 ));
178
182
}
179
183
@@ -184,7 +188,7 @@ public void index(EntityTags entityTags) {
184
188
new Index .Builder (
185
189
objectMapper .convertValue (prepareForWrite (objectMapper , entityTags ), Map .class ))
186
190
.index (activeElasticSearchIndex )
187
- .type (getDocumentType ( entityTags ) )
191
+ .type (mappingTypeName )
188
192
.id (entityTags .getId ())
189
193
.build ();
190
194
@@ -209,13 +213,13 @@ public void bulkIndex(Collection<EntityTags> multipleEntityTags) {
209
213
Bulk .Builder builder = new Bulk .Builder ().defaultIndex (activeElasticSearchIndex );
210
214
211
215
for (EntityTags entityTags : tags ) {
216
+ Map tag =
217
+ objectMapper .convertValue (prepareForWrite (objectMapper , entityTags ), Map .class );
212
218
builder =
213
219
builder .addAction (
214
- new Index .Builder (
215
- objectMapper .convertValue (
216
- prepareForWrite (objectMapper , entityTags ), Map .class ))
220
+ new Index .Builder (tag )
217
221
.index (activeElasticSearchIndex )
218
- .type (getDocumentType ( entityTags ) )
222
+ .type (mappingTypeName )
219
223
.id (entityTags .getId ())
220
224
.build ());
221
225
}
@@ -255,10 +259,7 @@ public void delete(String id) {
255
259
}
256
260
257
261
Delete action =
258
- new Delete .Builder (id )
259
- .index (activeElasticSearchIndex )
260
- .type (getDocumentType (entityTags ))
261
- .build ();
262
+ new Delete .Builder (id ).index (activeElasticSearchIndex ).type (mappingTypeName ).build ();
262
263
263
264
JestResult jestResult = jestClient .execute (action );
264
265
if (!jestResult .isSucceeded ()) {
@@ -281,9 +282,7 @@ public void bulkDelete(Collection<EntityTags> multipleEntityTags) {
281
282
for (EntityTags entityTags : tags ) {
282
283
builder =
283
284
builder .addAction (
284
- new Delete .Builder (entityTags .getId ())
285
- .type (getDocumentType (entityTags ))
286
- .build ());
285
+ new Delete .Builder (entityTags .getId ()).type (mappingTypeName ).build ());
287
286
}
288
287
289
288
Bulk bulk = builder .build ();
@@ -352,9 +351,14 @@ public Map delta() {
352
351
entityTagsByEntityTypeFront50
353
352
.keySet ()
354
353
.forEach (
355
- entityType ->
356
- entityTagsByEntityTypeElasticsearch .put (
357
- entityType , fetchAll (QueryBuilders .matchAllQuery (), entityType , 5000 , "2m" )));
354
+ entityType -> {
355
+ BoolQueryBuilder queryBuilder = QueryBuilders .boolQuery ();
356
+ queryBuilder =
357
+ queryBuilder .must (QueryBuilders .termQuery ("entityRef.entityType" , entityType ));
358
+
359
+ entityTagsByEntityTypeElasticsearch .put (
360
+ entityType , fetchAll (queryBuilder , 5000 , "2m" ));
361
+ });
358
362
359
363
Map <String , Map > metadata = new HashMap <>();
360
364
@@ -566,7 +570,7 @@ private List<EntityTags> getAllMatchingEntityTags(String namespace, String tag)
566
570
QueryBuilders .boolQuery ()
567
571
.must (applyTagsToBuilder (null , Collections .singletonMap (tag , "*" )));
568
572
569
- fetchAll (queryBuilder , null , 5000 , "2m" )
573
+ fetchAll (queryBuilder , 5000 , "2m" )
570
574
.forEach (
571
575
entityTags -> {
572
576
if (!entityTagsIdentifiers .contains (entityTags .getId ())) {
@@ -580,7 +584,7 @@ private List<EntityTags> getAllMatchingEntityTags(String namespace, String tag)
580
584
BoolQueryBuilder queryBuilder =
581
585
QueryBuilders .boolQuery ().must (applyTagsToBuilder (namespace , Collections .emptyMap ()));
582
586
583
- fetchAll (queryBuilder , null , 5000 , "2m" )
587
+ fetchAll (queryBuilder , 5000 , "2m" )
584
588
.forEach (
585
589
entityTags -> {
586
590
if (!entityTagsIdentifiers .contains (entityTags .getId ())) {
@@ -610,7 +614,7 @@ private QueryBuilder applyTagsToBuilder(String namespace, Map<String, Object> ta
610
614
boolQueryBuilder .must (QueryBuilders .termQuery ("tags.namespace" , namespace ));
611
615
}
612
616
613
- return QueryBuilders .nestedQuery ("tags" , boolQueryBuilder );
617
+ return QueryBuilders .nestedQuery ("tags" , boolQueryBuilder , ScoreMode . Avg );
614
618
}
615
619
616
620
/** Elasticsearch requires that all search criteria be flattened (vs. nested) */
@@ -628,17 +632,13 @@ private Map<String, Object> flatten(
628
632
return accumulator ;
629
633
}
630
634
631
- private List <EntityTags > search (String type , QueryBuilder queryBuilder , int maxResults ) {
635
+ private List <EntityTags > search (QueryBuilder queryBuilder , int maxResults ) {
632
636
SearchSourceBuilder searchSourceBuilder =
633
637
new SearchSourceBuilder ().query (queryBuilder ).size (maxResults );
634
638
String searchQuery = searchSourceBuilder .toString ();
635
639
636
640
Search .Builder searchBuilder =
637
641
new Search .Builder (searchQuery ).addIndex (activeElasticSearchIndex );
638
- if (type != null ) {
639
- // restrict to a specific index type (optional)
640
- searchBuilder .addType (type .toLowerCase ());
641
- }
642
642
643
643
try {
644
644
SearchResult searchResult = jestClient .execute (searchBuilder .build ());
@@ -651,18 +651,13 @@ private List<EntityTags> search(String type, QueryBuilder queryBuilder, int maxR
651
651
}
652
652
}
653
653
654
- private List <EntityTags > fetchAll (
655
- QueryBuilder queryBuilder , String type , int scrollSize , String scrollTime ) {
654
+ private List <EntityTags > fetchAll (QueryBuilder queryBuilder , int scrollSize , String scrollTime ) {
656
655
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder ();
657
656
searchSourceBuilder .query (queryBuilder );
658
657
659
658
Search .Builder builder =
660
659
new Search .Builder (searchSourceBuilder .toString ()).addIndex (activeElasticSearchIndex );
661
660
662
- if (type != null ) {
663
- builder .addType (type );
664
- }
665
-
666
661
Search search =
667
662
builder
668
663
.setParameter (Parameters .SIZE , scrollSize )
@@ -684,10 +679,7 @@ private List<EntityTags> fetchAll(
684
679
685
680
try {
686
681
while (entityTags .size () > 0 ) {
687
- SearchScroll scroll =
688
- new SearchScroll .Builder (scrollId , scrollTime )
689
- .setParameter (Parameters .SIZE , scrollSize )
690
- .build ();
682
+ SearchScroll scroll = new SearchScroll .Builder (scrollId , scrollTime ).build ();
691
683
692
684
try {
693
685
result = jestClient .execute (scroll );
@@ -748,12 +740,4 @@ private static EntityTags prepareForRead(ObjectMapper objectMapper, Map indexedE
748
740
749
741
return entityTags ;
750
742
}
751
-
752
- private String getDocumentType (EntityTags entityTags ) {
753
- if (singleMappingType ) {
754
- return mappingTypeName ;
755
- } else {
756
- return entityTags .getEntityRef ().getEntityType ();
757
- }
758
- }
759
743
}
0 commit comments