blob: 833c1251a7bfaf113b36108600e0ced8878740ed (
plain) (
blame)
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
|
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.search.yql;
import java.util.HashSet;
import java.util.Set;
import com.yahoo.component.chain.dependencies.After;
import static com.yahoo.prelude.fastsearch.VespaBackEndSearcher.SORTABLE_ATTRIBUTES_SUMMARY_CLASS;
import com.yahoo.processing.request.CompoundName;
import com.yahoo.search.Query;
import com.yahoo.search.Result;
import com.yahoo.search.Searcher;
import com.yahoo.search.query.Presentation;
import com.yahoo.search.schema.DocumentSummary;
import com.yahoo.search.schema.Schema;
import com.yahoo.search.schema.SchemaInfo;
import com.yahoo.search.searchchain.Execution;
/**
* Ensure the fields specified in {@link Presentation#getSummaryFields()} are available after filling phase.
*
* @author stiankri
* @author Steinar Knutsen
*/
@After(MinimalQueryInserter.EXTERNAL_YQL)
public class FieldFiller extends Searcher {
private final Set<String> intersectionOfAttributes;
private final SchemaInfo schemaInfo;
public static final CompoundName FIELD_FILLER_DISABLE = new CompoundName("FieldFiller.disable");
public FieldFiller(SchemaInfo schemaInfo) {
this.schemaInfo = schemaInfo;
intersectionOfAttributes = new HashSet<>();
boolean first = true;
for (Schema schema : schemaInfo.schemas().values()) {
for (DocumentSummary summary : schema.documentSummaries().values()) {
Set<String> attributes;
if (SORTABLE_ATTRIBUTES_SUMMARY_CLASS.equals(summary.name())) {
attributes = new HashSet<>(summary.fields().size());
for (DocumentSummary.Field f : summary.fields().values()) {
attributes.add(f.name());
}
if (first) {
first = false;
intersectionOfAttributes.addAll(attributes);
} else {
intersectionOfAttributes.retainAll(attributes);
}
}
}
}
}
@Override
public Result search(Query query, Execution execution) {
return execution.search(query);
}
@Override
public void fill(Result result, String summaryClass, Execution execution) {
Set<String> summaryFields = result.getQuery().getPresentation().getSummaryFields();
if (summaryFields.isEmpty() || result.getQuery().properties().getBoolean(FIELD_FILLER_DISABLE)) {
// no special handling:
execution.fill(result, summaryClass);
return;
}
if (summaryClass != null) {
// always fill requested class:
execution.fill(result, summaryClass);
if (hasAll(summaryFields, summaryClass, result.getQuery().getModel().getRestrict())) {
// no more was needed:
return;
}
}
// we need more:
if (intersectionOfAttributes.containsAll(summaryFields)) {
// only attributes needed:
execution.fill(result, SORTABLE_ATTRIBUTES_SUMMARY_CLASS);
} else {
// fetch all summary fields:
execution.fill(result, null);
}
}
private boolean hasAll(Set<String> requested, String summaryName, Set<String> restrict) {
Set<String> intersection = null;
for (String schemaName : restrict.isEmpty() ? schemaInfo.schemas().keySet() : restrict) {
Schema schema = schemaInfo.schemas().get(schemaName);
if (schema == null) continue;
DocumentSummary summary = schema.documentSummaries().get(summaryName);
if (summary == null) {
intersection = null;
break;
}
if (intersection == null) {
intersection = new HashSet<>(summary.fields().size());
intersection.addAll(summary.fields().keySet());
} else {
intersection.retainAll(summary.fields().keySet());
}
}
return intersection != null && intersection.containsAll(requested);
}
}
|