1
|
package eu.dnetlib.data.search.utils.solr;
|
2
|
|
3
|
|
4
|
import eu.dnetlib.functionality.cql.CqlTranslator;
|
5
|
import eu.dnetlib.functionality.cql.CqlTranslatorImpl;
|
6
|
import eu.dnetlib.functionality.cql.lucene.TranslatedQuery;
|
7
|
import org.apache.log4j.Logger;
|
8
|
import org.apache.solr.common.util.NamedList;
|
9
|
import org.z3950.zing.cql.CQLParseException;
|
10
|
|
11
|
import java.io.IOException;
|
12
|
import java.util.Arrays;
|
13
|
import java.util.Collections;
|
14
|
|
15
|
/**
|
16
|
* Created by kiatrop on 21/11/2016.
|
17
|
*/
|
18
|
public class SolrResultSetOptionsUtil {
|
19
|
|
20
|
private static final Logger logger = Logger.getLogger(SolrResultSetOptionsUtil.class);
|
21
|
|
22
|
public static NamedList<String> extractQueryOptions(String eprQuery) throws CQLParseException, IOException {
|
23
|
CqlTranslator translator = new CqlTranslatorImpl();
|
24
|
NamedList<String> queryOpts = new NamedList<String>();
|
25
|
String[] queryParts = eprQuery.split("&groupby=");
|
26
|
|
27
|
logger.debug("QUERY: " + queryParts[0].replace("query=",""));
|
28
|
TranslatedQuery translatedQuery = translator.getTranslatedQuery(queryParts[0].replace("query=",""));
|
29
|
queryOpts.add("q", translatedQuery.asLucene());
|
30
|
queryOpts.add("fl", "__result");
|
31
|
queryOpts.add("shards.tolerant","true");
|
32
|
|
33
|
if (translatedQuery.getOptions() != null && translatedQuery.getOptions().getSort()!= null ) {
|
34
|
queryOpts.add("sort", translatedQuery.getOptions().getSort().getField() + " " + translatedQuery.getOptions().getSort().getMode());
|
35
|
}
|
36
|
|
37
|
if (queryParts.length > 1) {
|
38
|
|
39
|
//facetParts groupby + sf
|
40
|
String[] facetParts = queryParts[1].split("&fq=");
|
41
|
|
42
|
logger.debug("facet parts" + Arrays.asList(facetParts));
|
43
|
|
44
|
String[] refineParts = null;
|
45
|
String[] refineFields = null;
|
46
|
String[] specialFacets = null;
|
47
|
String[] facetQueries = null;
|
48
|
|
49
|
if (facetParts != null && facetParts.length > 0) {
|
50
|
if (!facetParts[0].isEmpty()) {
|
51
|
refineParts = facetParts[0].split("&sf=");
|
52
|
logger.debug("refine parts " + Arrays.asList(refineParts));
|
53
|
|
54
|
if (refineParts != null && refineParts.length > 0) {
|
55
|
refineFields = refineParts[0].split(",(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)", -1);
|
56
|
}
|
57
|
|
58
|
if(refineParts.length > 1){
|
59
|
refineParts[1].replace("&sf=","");
|
60
|
specialFacets = refineParts[1].split(",(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)", -1);
|
61
|
}
|
62
|
}
|
63
|
|
64
|
if (facetParts.length > 1){
|
65
|
facetQueries = facetParts[1].split(",(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)", -1);
|
66
|
}
|
67
|
|
68
|
}
|
69
|
|
70
|
/* logger.debug("refine fields " + Arrays.asList(refineFields));
|
71
|
logger.debug("special facets " + Arrays.asList(specialFacets));
|
72
|
logger.debug("facet queries " + Arrays.asList(facetParts));*/
|
73
|
|
74
|
if (refineFields != null && refineFields.length > 0) {
|
75
|
queryOpts.add("facet", "true");
|
76
|
queryOpts.add("facet.mincount", "1");
|
77
|
queryOpts.add("facet.threads", refineFields.length + "");
|
78
|
for (String field : refineFields) {
|
79
|
queryOpts.add("facet.field", field);
|
80
|
}
|
81
|
}
|
82
|
|
83
|
if (specialFacets != null && specialFacets.length > 0) {
|
84
|
for (String field : specialFacets) {
|
85
|
queryOpts.add("f."+field+".facet.limit", "-1");
|
86
|
}
|
87
|
}
|
88
|
|
89
|
|
90
|
if (facetQueries!=null && facetQueries.length > 0) {
|
91
|
//queryOpts.add("q.op", "AND"); TODO is it correct?
|
92
|
|
93
|
for (String facetPart : facetQueries) {
|
94
|
logger.debug("facet Part " + facetPart);
|
95
|
queryOpts.add("fq", translator.toLucene(facetPart));
|
96
|
}
|
97
|
}
|
98
|
|
99
|
}
|
100
|
|
101
|
return queryOpts;
|
102
|
}
|
103
|
}
|