Project

General

Profile

1
package eu.dnetlib.functionality.index.query;
2

    
3
import java.util.Collection;
4
import java.util.List;
5

    
6
import com.google.common.collect.BiMap;
7
import com.google.common.collect.Iterables;
8

    
9
import eu.dnetlib.data.provision.index.rmi.BrowsingRow;
10
import eu.dnetlib.functionality.index.model.document.IndexDocument;
11
import eu.dnetlib.functionality.index.utils.IndexFieldUtility;
12
import eu.dnetlib.miscutils.functional.UnaryFunction;
13

    
14
/**
15
 * The Class QueryResponseParser.
16
 */
17
public abstract class QueryResponseParser {
18

    
19
	/** The highlight utils. */
20
	protected final UnaryFunction<String, String> highlightUtils;
21

    
22
	/** The aliases. */
23
	protected final BiMap<String, String> aliases;
24

    
25
	/** The return empty fields. */
26
	protected final boolean returnEmptyFields;
27

    
28
	/** The include ranking. */
29
	protected final boolean includeRanking;
30

    
31
	/** The wrapper rank. */
32
	protected final UnaryFunction<String, IndexDocument> wrapperRank = new UnaryFunction<String, IndexDocument>() {
33

    
34
		@Override
35
		public String evaluate(final IndexDocument doc) {
36
			return addRanking(getSingleField(doc, IndexFieldUtility.RESULT), getSingleField(doc, IndexFieldUtility.SCORE_FIELD));
37
		}
38
	};
39

    
40
	/** The wrapper no rank. */
41
	protected final UnaryFunction<String, IndexDocument> wrapperNoRank = new UnaryFunction<String, IndexDocument>() {
42

    
43
		@Override
44
		public String evaluate(final IndexDocument doc) {
45
			return wrap(getSingleField(doc, IndexFieldUtility.RESULT));
46
		}
47
	};
48

    
49
	/**
50
	 * Gets the single field.
51
	 *
52
	 * @param doc
53
	 *            the doc
54
	 * @param fieldName
55
	 *            the field name
56
	 * @return the single field
57
	 */
58
	@SuppressWarnings("unchecked")
59
	private String getSingleField(final IndexDocument doc, final String fieldName) {
60
		Object value = doc.getFieldValue(fieldName);
61
		if (value instanceof Collection) return Iterables.getOnlyElement((Iterable<String>) value);
62
		return String.valueOf(value);
63
	}
64

    
65
	/**
66
	 * Instantiates a new query response parser.
67
	 *
68
	 * @param queryRsp
69
	 *            the query rsp
70
	 * @param highlightUtils
71
	 *            the highlight utils
72
	 * @param aliases
73
	 *            the aliases
74
	 * @param returnEmptyFields
75
	 *            the return empty fields
76
	 * @param includeRanking
77
	 *            the include ranking
78
	 */
79
	public QueryResponseParser(final UnaryFunction<String, String> highlightUtils, final BiMap<String, String> aliases, final boolean returnEmptyFields,
80
			final boolean includeRanking) {
81

    
82
		this.highlightUtils = highlightUtils;
83
		this.aliases = aliases;
84
		this.returnEmptyFields = returnEmptyFields;
85
		this.includeRanking = includeRanking;
86
	}
87

    
88
	/**
89
	 * Converts a String document to
90
	 *
91
	 * <record rank="score"> [document] </record>.
92
	 *
93
	 * @param doc
94
	 *            the doc
95
	 * @param score
96
	 *            the score
97
	 * @return the string
98
	 */
99
	private String addRanking(final String doc, final String score) {
100
		return new String("<record rank=\"" + score + "\">" + doc + "</record>");
101
	}
102

    
103
	/**
104
	 * Wraps the given document as <record> [document] </record>.
105
	 *
106
	 * @param doc
107
	 *            the doc
108
	 * @return the string
109
	 */
110
	private String wrap(final String doc) {
111
		return new String("<record>" + doc + "</record>");
112
	}
113

    
114
	/**
115
	 * Gets the num found.
116
	 *
117
	 * @return the num found
118
	 */
119
	public abstract long getNumFound();
120

    
121
	/**
122
	 * Gets the query time.
123
	 *
124
	 * @return the query time
125
	 */
126
	public abstract int getQueryTime();
127

    
128
	/**
129
	 * Gets the elapsed time.
130
	 *
131
	 * @return the elapsed time
132
	 */
133
	public abstract long getElapsedTime();
134

    
135
	/**
136
	 * Gets the status.
137
	 *
138
	 * @return the status
139
	 */
140
	public abstract String getStatus();
141

    
142
	/**
143
	 * Gets the start.
144
	 *
145
	 * @return the start
146
	 */
147
	public abstract long getStart();
148

    
149
	/**
150
	 * Gets the current size.
151
	 *
152
	 * @return the current number of documents.
153
	 */
154
	public abstract int getCurrentSize();
155

    
156
	/**
157
	 * Gets the results.
158
	 *
159
	 * @return query results as a List<String>
160
	 */
161
	public abstract List<String> getResults();
162

    
163
	/**
164
	 * method counts the number of facet fields resulting from the performed query.
165
	 *
166
	 * @return the number of browsing results
167
	 */
168
	public abstract Long getNumberOfBrowsingResults();
169

    
170
	/**
171
	 * Gets the browsing results.
172
	 *
173
	 * @return the browsing results
174
	 */
175
	public abstract List<BrowsingRow> getBrowsingResults();
176

    
177
	public BiMap<String, String> getAliases() {
178
		return aliases;
179
	}
180

    
181
}
(8-8/9)