Revision 44742
Added by Katerina Iatropoulou over 7 years ago
modules/uoa-search/branches/newAPI/src/test/java/eu/dnetlib/data/search/app/plan/web/api/SolrUtilsTest.java | ||
---|---|---|
1 |
package eu.dnetlib.data.search.app.plan.web.api; |
|
2 |
|
|
3 |
import eu.dnetlib.data.search.app.SearchServiceImpl; |
|
4 |
import eu.dnetlib.data.search.solr.SolrResultSet; |
|
5 |
import eu.dnetlib.data.search.utils.SolrResultSetOptionsUtil; |
|
6 |
import eu.dnetlib.data.search.web.api.SearchApiService; |
|
7 |
import eu.dnetlib.data.search.web.utils.RequestResponseHandler; |
|
8 |
import eu.dnetlib.functionality.index.cql.CqlTranslator; |
|
9 |
import eu.dnetlib.functionality.index.cql.CqlTranslatorImpl; |
|
10 |
import org.apache.log4j.BasicConfigurator; |
|
11 |
import org.apache.solr.client.solrj.impl.CloudSolrServer; |
|
12 |
import org.apache.solr.common.util.NamedList; |
|
13 |
import org.junit.Assert; |
|
14 |
import org.junit.Before; |
|
15 |
import org.junit.Test; |
|
16 |
import org.mockito.Mockito; |
|
17 |
import org.z3950.zing.cql.CQLParseException; |
|
18 |
|
|
19 |
import javax.servlet.http.HttpServletRequest; |
|
20 |
import java.io.IOException; |
|
21 |
import java.util.ArrayList; |
|
22 |
import java.util.Arrays; |
|
23 |
import java.util.List; |
|
24 |
|
|
25 |
import static org.mockito.Matchers.anyString; |
|
26 |
import static org.mockito.Mockito.doNothing; |
|
27 |
|
|
28 |
|
|
29 |
/** |
|
30 |
* Created by kiatrop on 9/11/2016. |
|
31 |
*/ |
|
32 |
public class SolrUtilsTest { |
|
33 |
|
|
34 |
private org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(SearchApiService.class); |
|
35 |
|
|
36 |
HttpServletRequest requestMock; |
|
37 |
RequestResponseHandler.Entity entity; |
|
38 |
List<String> refineFields = new ArrayList<String>(); |
|
39 |
List<String> fieldQueries = new ArrayList<String>(); |
|
40 |
CloudSolrServer mockSolrClient; |
|
41 |
SolrResultSet mockRS; |
|
42 |
|
|
43 |
@Before |
|
44 |
public void before() { |
|
45 |
BasicConfigurator.configure(); |
|
46 |
|
|
47 |
/* requestMock = Mockito.mock(HttpServletRequest.class); |
|
48 |
Map<String, String[]> mockParameterMap = new HashMap<String, String[]>(); |
|
49 |
mockParameterMap.put("fq", new String[]{"(relfunderid exact ec___::EC) and (relfunderid exact wt___::WT)", |
|
50 |
"relfundinglevel0_id exact ec___::EC::SP1", "(relfundinglevel1_id exact ec___::EC::SP1::VALUE1) or (relfundinglevel1_id exact ec___::EC::SP1::VALUE2)"}); |
|
51 |
when(requestMock.getParameterMap()).thenReturn(mockParameterMap); |
|
52 |
when(requestMock.getParameterValues("fq")).thenReturn(new String[]{"(relfunderid exact ec___::EC) and (relfunderid exact wt___::WT)", |
|
53 |
"relfundinglevel0_id exact ec___::EC::SP1" , "(relfundinglevel1_id exact ec___::EC::SP1::VALUE1) or (relfundinglevel1_id exact ec___::EC::SP1::VALUE2)"}); |
|
54 |
*/ |
|
55 |
mockSolrClient = Mockito.mock(CloudSolrServer.class); |
|
56 |
doNothing().when(mockSolrClient).setDefaultCollection(anyString()); |
|
57 |
|
|
58 |
mockRS = Mockito.mock(SolrResultSet.class); |
|
59 |
|
|
60 |
refineFields = Arrays.asList(new String[]{"relfunderid", "relfundinglevel0_id", "relfundinglevel1_id", "relfundinglevel2_id"}); |
|
61 |
|
|
62 |
fieldQueries = Arrays.asList(new String[]{"(relfunderid exact ec___::EC) and (relfunderid exact wt___::WT)", |
|
63 |
"relfundinglevel0_id exact ec___::EC::SP1" , "(relfundinglevel1_id exact ec___::EC::SP1::VALUE1) or (relfundinglevel1_id exact ec___::EC::SP1::VALUE2)"}); |
|
64 |
} |
|
65 |
|
|
66 |
|
|
67 |
@Test |
|
68 |
public void createEprQuery() { |
|
69 |
String query = SearchServiceImpl.createEprQuery("(oaftype=result)", refineFields, fieldQueries); |
|
70 |
Assert.assertEquals("query=(oaftype=result)" + |
|
71 |
"&groupby=relfunderid,relfundinglevel0_id,relfundinglevel1_id,relfundinglevel2_id" + |
|
72 |
"&fq=(relfunderid exact ec___::EC) and (relfunderid exact wt___::WT),relfundinglevel0_id exact ec___::EC::SP1," + |
|
73 |
"(relfundinglevel1_id exact ec___::EC::SP1::VALUE1) or (relfundinglevel1_id exact ec___::EC::SP1::VALUE2)", query); |
|
74 |
|
|
75 |
//empty refine, empty fq |
|
76 |
query = SearchServiceImpl.createEprQuery("(oaftype=result)", new ArrayList<String>(), new ArrayList<String>()); |
|
77 |
Assert.assertEquals("query=(oaftype=result)" + |
|
78 |
"&groupby=" + |
|
79 |
"&fq=", query); |
|
80 |
|
|
81 |
//empty fq |
|
82 |
query = SearchServiceImpl.createEprQuery("(oaftype=result)", refineFields, new ArrayList<String>()); |
|
83 |
Assert.assertEquals("query=(oaftype=result)" + |
|
84 |
"&groupby=relfunderid,relfundinglevel0_id,relfundinglevel1_id,relfundinglevel2_id" + |
|
85 |
"&fq=", query); |
|
86 |
|
|
87 |
//empty refine |
|
88 |
query = SearchServiceImpl.createEprQuery("(oaftype=result)", new ArrayList<String>(), fieldQueries); |
|
89 |
Assert.assertEquals("query=(oaftype=result)" + |
|
90 |
"&groupby=" + |
|
91 |
"&fq=(relfunderid exact ec___::EC) and (relfunderid exact wt___::WT),relfundinglevel0_id exact ec___::EC::SP1," + |
|
92 |
"(relfundinglevel1_id exact ec___::EC::SP1::VALUE1) or (relfundinglevel1_id exact ec___::EC::SP1::VALUE2)", query); |
|
93 |
|
|
94 |
//empty query |
|
95 |
query = SearchServiceImpl.createEprQuery("", new ArrayList<String>(), new ArrayList<String>()); |
|
96 |
Assert.assertEquals("query=" + |
|
97 |
"&groupby=" + |
|
98 |
"&fq=", query); |
|
99 |
|
|
100 |
//null values |
|
101 |
query = SearchServiceImpl.createEprQuery(null, null, null); |
|
102 |
Assert.assertEquals("query=" + |
|
103 |
"&groupby=" + |
|
104 |
"&fq=", query); |
|
105 |
|
|
106 |
} |
|
107 |
|
|
108 |
@Test |
|
109 |
public void extractQueryOptions() throws IOException, CQLParseException { |
|
110 |
CqlTranslator cqlTranslator = new CqlTranslatorImpl(); |
|
111 |
|
|
112 |
NamedList<String> actual = SolrResultSetOptionsUtil.extractQueryOptions(SearchServiceImpl.createEprQuery("(oaftype=result)", refineFields, fieldQueries)); |
|
113 |
Assert.assertEquals(cqlTranslator.toLucene("(oaftype=result)"), actual.get("q")); |
|
114 |
Assert.assertEquals("__result", actual.get("fl")); |
|
115 |
Assert.assertEquals("AND", actual.get("q.op")); |
|
116 |
Assert.assertEquals("true", actual.get("shards.tolerant")); |
|
117 |
Assert.assertEquals("true", actual.get("facet")); |
|
118 |
Assert.assertEquals("1", actual.get("facet.mincount")); |
|
119 |
Assert.assertEquals(refineFields.size() + "", actual.get("facet.threads")); |
|
120 |
Assert.assertEquals(refineFields.size(), actual.getAll("facet.field").size()); |
|
121 |
Assert.assertTrue(actual.getAll("facet.field").contains("relfunderid")); |
|
122 |
Assert.assertTrue(actual.getAll("facet.field").contains("relfundinglevel1_id")); |
|
123 |
Assert.assertTrue(actual.getAll("facet.field").contains("relfundinglevel2_id")); |
|
124 |
Assert.assertFalse(actual.getAll("facet.field").contains("relfundinglevel3_id")); |
|
125 |
Assert.assertEquals(fieldQueries.size(), actual.getAll("fq").size()); |
|
126 |
Assert.assertTrue(actual.getAll("fq").contains(cqlTranslator.toLucene("(relfunderid exact ec___::EC) and (relfunderid exact wt___::WT)"))); |
|
127 |
Assert.assertTrue(actual.getAll("fq").contains(cqlTranslator.toLucene("relfundinglevel0_id exact ec___::EC::SP1"))); |
|
128 |
Assert.assertTrue(actual.getAll("fq").contains(cqlTranslator.toLucene("(relfundinglevel1_id exact ec___::EC::SP1::VALUE1) or (relfundinglevel1_id exact ec___::EC::SP1::VALUE2)"))); |
|
129 |
|
|
130 |
//empty refine, empty fq |
|
131 |
actual = SolrResultSetOptionsUtil.extractQueryOptions(SearchServiceImpl.createEprQuery("(oaftype=result)", new ArrayList<String>(), new ArrayList<String>())); |
|
132 |
Assert.assertEquals(cqlTranslator.toLucene("(oaftype=result)"), actual.get("q")); |
|
133 |
Assert.assertEquals("__result", actual.get("fl")); |
|
134 |
Assert.assertNull(actual.get("q.op")); |
|
135 |
Assert.assertEquals("true", actual.get("shards.tolerant")); |
|
136 |
Assert.assertNull(actual.get("facet")); |
|
137 |
Assert.assertNull(actual.get("facet.mincount")); |
|
138 |
Assert.assertNull(actual.get("facet.threads")); |
|
139 |
Assert.assertNull(actual.get("facet.field")); |
|
140 |
Assert.assertTrue(actual.getAll("facet.field").isEmpty()); |
|
141 |
Assert.assertNull(actual.get("fq")); |
|
142 |
Assert.assertTrue(actual.getAll("fq").isEmpty()); |
|
143 |
|
|
144 |
//empty fq |
|
145 |
actual = SolrResultSetOptionsUtil.extractQueryOptions(SearchServiceImpl.createEprQuery("(oaftype=result)", refineFields, new ArrayList<String>())); |
|
146 |
Assert.assertEquals(cqlTranslator.toLucene("(oaftype=result)"), actual.get("q")); |
|
147 |
Assert.assertEquals("__result", actual.get("fl")); |
|
148 |
Assert.assertNull(actual.get("q.op")); |
|
149 |
Assert.assertEquals("true", actual.get("shards.tolerant")); |
|
150 |
Assert.assertEquals("true", actual.get("facet")); |
|
151 |
Assert.assertEquals("1", actual.get("facet.mincount")); |
|
152 |
Assert.assertEquals(refineFields.size() + "", actual.get("facet.threads")); |
|
153 |
Assert.assertEquals(refineFields.size(), actual.getAll("facet.field").size()); |
|
154 |
Assert.assertTrue(actual.getAll("facet.field").contains("relfunderid")); |
|
155 |
Assert.assertTrue(actual.getAll("facet.field").contains("relfundinglevel1_id")); |
|
156 |
Assert.assertTrue(actual.getAll("facet.field").contains("relfundinglevel2_id")); |
|
157 |
Assert.assertFalse(actual.getAll("facet.field").contains("relfundinglevel3_id")); |
|
158 |
Assert.assertNull(actual.get("fq")); |
|
159 |
Assert.assertTrue(actual.getAll("fq").isEmpty()); |
|
160 |
|
|
161 |
//null fq |
|
162 |
actual = SolrResultSetOptionsUtil.extractQueryOptions(SearchServiceImpl.createEprQuery("(oaftype=result)", refineFields, null)); |
|
163 |
Assert.assertEquals(cqlTranslator.toLucene("(oaftype=result)"), actual.get("q")); |
|
164 |
Assert.assertEquals("__result", actual.get("fl")); |
|
165 |
Assert.assertNull(actual.get("q.op")); |
|
166 |
Assert.assertEquals("true", actual.get("shards.tolerant")); |
|
167 |
Assert.assertEquals("true", actual.get("facet")); |
|
168 |
Assert.assertEquals("1", actual.get("facet.mincount")); |
|
169 |
Assert.assertEquals(refineFields.size() + "", actual.get("facet.threads")); |
|
170 |
Assert.assertEquals(refineFields.size(), actual.getAll("facet.field").size()); |
|
171 |
Assert.assertTrue(actual.getAll("facet.field").contains("relfunderid")); |
|
172 |
Assert.assertTrue(actual.getAll("facet.field").contains("relfundinglevel1_id")); |
|
173 |
Assert.assertTrue(actual.getAll("facet.field").contains("relfundinglevel2_id")); |
|
174 |
Assert.assertFalse(actual.getAll("facet.field").contains("relfundinglevel3_id")); |
|
175 |
Assert.assertNull(actual.get("fq")); |
|
176 |
Assert.assertTrue(actual.getAll("fq").isEmpty()); |
|
177 |
|
|
178 |
//empty refine |
|
179 |
actual = SolrResultSetOptionsUtil.extractQueryOptions(SearchServiceImpl.createEprQuery("(oaftype=result)", new ArrayList<String>(), fieldQueries)); |
|
180 |
Assert.assertEquals(cqlTranslator.toLucene("(oaftype=result)"), actual.get("q")); |
|
181 |
Assert.assertEquals("__result", actual.get("fl")); |
|
182 |
Assert.assertEquals("AND", actual.get("q.op")); |
|
183 |
Assert.assertEquals("true", actual.get("shards.tolerant")); |
|
184 |
Assert.assertNull(actual.get("facet")); |
|
185 |
Assert.assertNull(actual.get("facet.mincount")); |
|
186 |
Assert.assertNull(actual.get("facet.threads")); |
|
187 |
Assert.assertNull(actual.get("facet.field")); |
|
188 |
Assert.assertTrue(actual.getAll("facet.field").isEmpty()); |
|
189 |
Assert.assertEquals(fieldQueries.size(), actual.getAll("fq").size()); |
|
190 |
Assert.assertTrue(actual.getAll("fq").contains(cqlTranslator.toLucene("(relfunderid exact ec___::EC) and (relfunderid exact wt___::WT)"))); |
|
191 |
Assert.assertTrue(actual.getAll("fq").contains(cqlTranslator.toLucene("relfundinglevel0_id exact ec___::EC::SP1"))); |
|
192 |
Assert.assertTrue(actual.getAll("fq").contains(cqlTranslator.toLucene("(relfundinglevel1_id exact ec___::EC::SP1::VALUE1) or (relfundinglevel1_id exact ec___::EC::SP1::VALUE2)"))); |
|
193 |
|
|
194 |
//null refine |
|
195 |
actual = SolrResultSetOptionsUtil.extractQueryOptions(SearchServiceImpl.createEprQuery("(oaftype=result)", null, fieldQueries)); |
|
196 |
Assert.assertEquals(cqlTranslator.toLucene("(oaftype=result)"), actual.get("q")); |
|
197 |
Assert.assertEquals("__result", actual.get("fl")); |
|
198 |
Assert.assertEquals("AND", actual.get("q.op")); |
|
199 |
Assert.assertEquals("true", actual.get("shards.tolerant")); |
|
200 |
Assert.assertNull(actual.get("facet")); |
|
201 |
Assert.assertNull(actual.get("facet.mincount")); |
|
202 |
Assert.assertNull(actual.get("facet.threads")); |
|
203 |
Assert.assertNull(actual.get("facet.field")); |
|
204 |
Assert.assertTrue(actual.getAll("facet.field").isEmpty()); |
|
205 |
Assert.assertEquals(fieldQueries.size(), actual.getAll("fq").size()); |
|
206 |
Assert.assertTrue(actual.getAll("fq").contains(cqlTranslator.toLucene(("(relfunderid exact ec___::EC) and (relfunderid exact wt___::WT)")))); |
|
207 |
Assert.assertTrue(actual.getAll("fq").contains(cqlTranslator.toLucene("relfundinglevel0_id exact ec___::EC::SP1"))); |
|
208 |
Assert.assertTrue(actual.getAll("fq").contains(cqlTranslator.toLucene("(relfundinglevel1_id exact ec___::EC::SP1::VALUE1) or (relfundinglevel1_id exact ec___::EC::SP1::VALUE2)"))); |
|
209 |
|
|
210 |
//null refine, null fq |
|
211 |
actual = SolrResultSetOptionsUtil.extractQueryOptions(SearchServiceImpl.createEprQuery("(oaftype=result)", null, null)); |
|
212 |
Assert.assertEquals(cqlTranslator.toLucene("(oaftype=result)"), actual.get("q")); |
|
213 |
Assert.assertEquals("__result", actual.get("fl")); |
|
214 |
Assert.assertNull(actual.get("q.op")); |
|
215 |
Assert.assertEquals("true", actual.get("shards.tolerant")); |
|
216 |
Assert.assertNull(actual.get("facet")); |
|
217 |
Assert.assertNull(actual.get("facet.mincount")); |
|
218 |
Assert.assertNull(actual.get("facet.threads")); |
|
219 |
Assert.assertNull(actual.get("facet.field")); |
|
220 |
Assert.assertTrue(actual.getAll("facet.field").isEmpty()); |
|
221 |
Assert.assertNull(actual.get("fq")); |
|
222 |
Assert.assertTrue(actual.getAll("fq").isEmpty()); |
|
223 |
} |
|
224 |
|
|
225 |
@Test(expected=CQLParseException.class) |
|
226 |
public void extractQueryOptionsEmptyQuery() throws IOException, CQLParseException { |
|
227 |
NamedList<String> actual = SolrResultSetOptionsUtil.extractQueryOptions(SearchServiceImpl.createEprQuery(null, null, null)); |
|
228 |
} |
|
229 |
|
|
230 |
} |
modules/uoa-search/branches/newAPI/src/main/java/eu/dnetlib/data/search/utils/SolrResultsFormatter.java | ||
---|---|---|
1 |
package eu.dnetlib.data.search.utils; |
|
2 |
|
|
3 |
import org.apache.log4j.Logger; |
|
4 |
import org.json.XML; |
|
5 |
|
|
6 |
import java.util.Iterator; |
|
7 |
import java.util.List; |
|
8 |
|
|
9 |
/** |
|
10 |
* Created by kiatrop on 16/11/2016. |
|
11 |
*/ |
|
12 |
public class SolrResultsFormatter { |
|
13 |
|
|
14 |
private static final Logger logger = Logger.getLogger(SolrResultsFormatter.class); |
|
15 |
|
|
16 |
public static String jsonList2Json(List<String> jsons) { |
|
17 |
StringBuilder entitesBuilder = new StringBuilder(); |
|
18 |
|
|
19 |
for(Iterator<String> jsonIterator = jsons.iterator(); jsonIterator.hasNext();){ |
|
20 |
entitesBuilder.append(jsonIterator.next()); |
|
21 |
if(jsonIterator.hasNext()){ |
|
22 |
entitesBuilder.append(","); |
|
23 |
} |
|
24 |
} |
|
25 |
return entitesBuilder.toString(); |
|
26 |
} |
|
27 |
|
|
28 |
public static String xml2Json(String resultXml) { |
|
29 |
StringBuilder entitesBuilder = new StringBuilder(); |
|
30 |
|
|
31 |
if (resultXml!= null) { |
|
32 |
return XML.toJSONObject(resultXml).toString(); |
|
33 |
} |
|
34 |
|
|
35 |
return ""; |
|
36 |
} |
|
37 |
} |
modules/uoa-search/branches/newAPI/src/main/java/eu/dnetlib/data/search/utils/SolrResultSetOptionsUtil.java | ||
---|---|---|
1 |
package eu.dnetlib.data.search.utils; |
|
2 |
|
|
3 |
import eu.dnetlib.functionality.index.cql.CqlTranslator; |
|
4 |
import eu.dnetlib.functionality.index.cql.CqlTranslatorImpl; |
|
5 |
import eu.dnetlib.functionality.index.cql.TranslatedQuery; |
|
6 |
import org.apache.log4j.Logger; |
|
7 |
import org.apache.solr.common.util.NamedList; |
|
8 |
import org.z3950.zing.cql.CQLParseException; |
|
9 |
|
|
10 |
import java.io.IOException; |
|
11 |
|
|
12 |
/** |
|
13 |
* Created by kiatrop on 21/11/2016. |
|
14 |
*/ |
|
15 |
public class SolrResultSetOptionsUtil { |
|
16 |
|
|
17 |
private static final Logger logger = Logger.getLogger(SolrResultSetOptionsUtil.class); |
|
18 |
|
|
19 |
public static NamedList<String> extractQueryOptions(String eprQuery) throws CQLParseException, IOException { |
|
20 |
CqlTranslator translator = new CqlTranslatorImpl(); |
|
21 |
NamedList<String> queryOpts = new NamedList<String>(); |
|
22 |
String[] queryParts = eprQuery.split("&groupby="); |
|
23 |
|
|
24 |
TranslatedQuery translatedQuery = translator.getTranslatedQuery(queryParts[0].replace("query=","")); |
|
25 |
queryOpts.add("q", translatedQuery.asLucene()); |
|
26 |
queryOpts.add("fl", "__result"); |
|
27 |
queryOpts.add("shards.tolerant","true"); |
|
28 |
|
|
29 |
if (translatedQuery.getOptions() != null && translatedQuery.getOptions().getSort()!= null ) { |
|
30 |
queryOpts.add("sort", translatedQuery.getOptions().getSort().getField() + " " + translatedQuery.getOptions().getSort().getMode()); |
|
31 |
} |
|
32 |
|
|
33 |
if (queryParts.length > 1) { |
|
34 |
String[] facetParts = queryParts[1].split("&fq="); |
|
35 |
|
|
36 |
String[] refineParts = null; |
|
37 |
String[] facetQueries = null; |
|
38 |
|
|
39 |
if (facetParts != null && facetParts.length > 0) { |
|
40 |
if (!facetParts[0].isEmpty()) { |
|
41 |
refineParts = facetParts[0].split(","); |
|
42 |
} |
|
43 |
|
|
44 |
if (facetParts.length > 1){ |
|
45 |
facetQueries = facetParts[1].split(","); |
|
46 |
} |
|
47 |
|
|
48 |
if (facetParts[0].isEmpty() && facetParts.length > 1) { |
|
49 |
facetQueries = facetParts[1].split(","); |
|
50 |
} |
|
51 |
} |
|
52 |
|
|
53 |
if (refineParts != null && refineParts.length > 0) { |
|
54 |
queryOpts.add("facet", "true"); |
|
55 |
queryOpts.add("facet.mincount", "1"); |
|
56 |
queryOpts.add("facet.threads", refineParts.length + ""); |
|
57 |
for (String field : refineParts) { |
|
58 |
queryOpts.add("facet.field", field); |
|
59 |
} |
|
60 |
} |
|
61 |
|
|
62 |
if (facetQueries!=null && facetQueries.length > 0) { |
|
63 |
queryOpts.add("q.op", "AND"); |
|
64 |
for (String facetPart : facetQueries) { |
|
65 |
queryOpts.add("fq", translator.toLucene(facetPart)); |
|
66 |
} |
|
67 |
} |
|
68 |
|
|
69 |
} |
|
70 |
|
|
71 |
return queryOpts; |
|
72 |
} |
|
73 |
|
|
74 |
} |
modules/uoa-search/branches/newAPI/src/main/java/eu/dnetlib/data/search/web/servlet/SearchServlet.java | ||
---|---|---|
1 |
package eu.dnetlib.data.search.web.servlet; |
|
2 |
|
|
3 |
import eu.dnetlib.api.data.SearchService; |
|
4 |
import eu.dnetlib.data.search.app.SearchServiceImpl; |
|
5 |
import eu.dnetlib.domain.data.FormattedSearchResult; |
|
6 |
import org.apache.commons.lang.IncompleteArgumentException; |
|
7 |
import org.apache.commons.lang.StringEscapeUtils; |
|
8 |
import org.apache.log4j.Logger; |
|
9 |
import org.springframework.context.ApplicationContext; |
|
10 |
import org.springframework.web.context.support.WebApplicationContextUtils; |
|
11 |
|
|
12 |
import javax.servlet.ServletConfig; |
|
13 |
import javax.servlet.ServletException; |
|
14 |
import javax.servlet.http.HttpServlet; |
|
15 |
import javax.servlet.http.HttpServletRequest; |
|
16 |
import javax.servlet.http.HttpServletResponse; |
|
17 |
import java.io.PrintWriter; |
|
18 |
import java.util.ArrayList; |
|
19 |
import java.util.Collection; |
|
20 |
|
|
21 |
@SuppressWarnings("serial") |
|
22 |
public class SearchServlet extends HttpServlet{ |
|
23 |
|
|
24 |
private SearchServiceImpl searchService = null; |
|
25 |
private ApplicationContext context = null; |
|
26 |
private static Logger logger = Logger.getLogger(SearchService.class); |
|
27 |
|
|
28 |
public void init(ServletConfig config) throws ServletException { |
|
29 |
super.init(config); |
|
30 |
context = WebApplicationContextUtils.getWebApplicationContext(getServletContext()); |
|
31 |
searchService = (SearchServiceImpl) context.getBean("searchService"); |
|
32 |
} |
|
33 |
|
|
34 |
@Override |
|
35 |
public void doGet(HttpServletRequest request, HttpServletResponse response) { |
|
36 |
PrintWriter writer = null; |
|
37 |
FormattedSearchResult formattedSearchResult = null; |
|
38 |
|
|
39 |
String action = request.getParameter("action"); |
|
40 |
if (action == null) { |
|
41 |
throw new UnsupportedOperationException("Undefined action."); |
|
42 |
} |
|
43 |
|
|
44 |
String query = request.getParameter("query"); |
|
45 |
if (query == null) { |
|
46 |
throw new IncompleteArgumentException("Undefined query. Search request"); |
|
47 |
} |
|
48 |
|
|
49 |
String format = (request.getParameter("format") != null) ? |
|
50 |
request.getParameter("format") : "xml"; |
|
51 |
|
|
52 |
String locale = request.getParameter("locale"); |
|
53 |
|
|
54 |
int page = readParameter(request, "page", 1); |
|
55 |
int size = readParameter(request, "size", 10); |
|
56 |
|
|
57 |
String sTransformer = request.getParameter("sTransformer"); |
|
58 |
String rTransformer = request.getParameter("rTransformer"); |
|
59 |
|
|
60 |
Collection<String> fields = readParameter(request, "fields"); |
|
61 |
|
|
62 |
response.setContentType("text/xml;charset=UTF-8"); |
|
63 |
|
|
64 |
try { |
|
65 |
writer = response.getWriter(); |
|
66 |
if(action.equals("search")) { |
|
67 |
if (sTransformer == null) { |
|
68 |
throw new IncompleteArgumentException("Undefined search transformer. Search request"); |
|
69 |
} |
|
70 |
|
|
71 |
formattedSearchResult = searchService.search(query, sTransformer, format, locale, page, size); |
|
72 |
|
|
73 |
} else if(action.equals("refine")) { |
|
74 |
if(rTransformer == null) { |
|
75 |
throw new IncompleteArgumentException("Undefined refine transformer. Refine request"); |
|
76 |
} |
|
77 |
|
|
78 |
if(fields == null) { |
|
79 |
throw new IncompleteArgumentException("Undefined refine fields. Refine request"); |
|
80 |
} |
|
81 |
|
|
82 |
formattedSearchResult = searchService.refine(query, rTransformer, format, locale, fields); |
|
83 |
|
|
84 |
} else if(action.equals("searchNrefine")) { |
|
85 |
|
|
86 |
if(sTransformer == null) { |
|
87 |
throw new IncompleteArgumentException("Undefined search transformer. Search and Refine request"); |
|
88 |
} |
|
89 |
|
|
90 |
if(rTransformer == null) { |
|
91 |
throw new IncompleteArgumentException("Undefined refine transformer. Search and Refine request"); |
|
92 |
} |
|
93 |
|
|
94 |
if(fields == null) { |
|
95 |
throw new IncompleteArgumentException("Undefined refine fields. Search and Refine request"); |
|
96 |
} |
|
97 |
|
|
98 |
formattedSearchResult = searchService.searchNrefine(query, sTransformer, rTransformer, format, locale, page, size, fields); |
|
99 |
|
|
100 |
} else { |
|
101 |
throw new UnsupportedOperationException("The action " + action + " is not supported."); |
|
102 |
} |
|
103 |
|
|
104 |
writer.append(formattedSearchResult.getFormattedResult()); |
|
105 |
|
|
106 |
} catch (Exception e) { |
|
107 |
writer.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); |
|
108 |
writer.append("<error>"); |
|
109 |
writer.append("<exception>").append(StringEscapeUtils.escapeXml(e.getClass().getName())).append("</exception>"); |
|
110 |
if (e.getMessage() != null) { |
|
111 |
writer.append("<message>").append(StringEscapeUtils.escapeXml(e.getMessage())).append("</message>"); |
|
112 |
} |
|
113 |
if (e.getCause() != null) { |
|
114 |
writer.append("<cause>").append(StringEscapeUtils.escapeXml(e.getCause().toString())).append("</cause>"); |
|
115 |
} |
|
116 |
|
|
117 |
StackTraceElement[] trace = e.getStackTrace(); |
|
118 |
writer.append("<trace>"); |
|
119 |
for (int i = 0; i < trace.length; i++) { |
|
120 |
writer.append(StringEscapeUtils.escapeXml(trace[i].toString())).append("\n"); |
|
121 |
} |
|
122 |
writer.append("</trace>"); |
|
123 |
|
|
124 |
writer.append("</error>"); |
|
125 |
|
|
126 |
} finally { |
|
127 |
if (writer != null) { |
|
128 |
writer.close(); |
|
129 |
} |
|
130 |
} |
|
131 |
} |
|
132 |
|
|
133 |
private Collection<String> readParameter(HttpServletRequest request, String parameterName) { |
|
134 |
Collection<String> fields = null; |
|
135 |
String[] paramfields = request.getParameterValues(parameterName); |
|
136 |
if (paramfields != null) { |
|
137 |
fields = new ArrayList<String>(); |
|
138 |
for (int i=0; i<paramfields.length; i++) { |
|
139 |
fields.add(paramfields[i]); |
|
140 |
} |
|
141 |
} |
|
142 |
return fields; |
|
143 |
} |
|
144 |
|
|
145 |
public int readParameter(HttpServletRequest request, String parameterName, int defaultValue) { |
|
146 |
String param = request.getParameter(parameterName); |
|
147 |
return (param != null)?Integer.parseInt(param):defaultValue; |
|
148 |
} |
|
149 |
} |
modules/uoa-search/branches/newAPI/src/main/java/eu/dnetlib/data/search/web/api/QueryEnhancer.java | ||
---|---|---|
1 |
package eu.dnetlib.data.search.web.api; |
|
2 |
|
|
3 |
import eu.dnetlib.data.search.utils.vocabulary.VocabularyManager; |
|
4 |
import eu.dnetlib.domain.enabling.Vocabulary; |
|
5 |
import org.apache.log4j.Logger; |
|
6 |
|
|
7 |
import javax.servlet.http.HttpServletRequest; |
|
8 |
import java.text.ParseException; |
|
9 |
import java.text.SimpleDateFormat; |
|
10 |
import java.util.Date; |
|
11 |
import java.util.Locale; |
|
12 |
|
|
13 |
/** |
|
14 |
* Created by kiatrop on 10/7/2014. |
|
15 |
*/ |
|
16 |
public class QueryEnhancer { |
|
17 |
|
|
18 |
private static final Logger logger = Logger.getLogger(QueryEnhancer.class); |
|
19 |
|
|
20 |
/** |
|
21 |
* Enhance the given CQL query with FP7 specific index fields |
|
22 |
* @param queryBuilder |
|
23 |
* @param request |
|
24 |
*/ |
|
25 |
public static void enhanceQueryWithFundingLevelParams(StringBuilder queryBuilder, HttpServletRequest request, VocabularyManager vocabularyManager, boolean isModelSygma) { |
|
26 |
logger.debug("enhance funding " + isModelSygma); |
|
27 |
|
|
28 |
String funder = request.getParameter("funder"); |
|
29 |
String fundingStream = request.getParameter("fundingStream"); |
|
30 |
String FP7scientificArea = request.getParameter("FP7scientificArea"); |
|
31 |
|
|
32 |
if(isModelSygma) { |
|
33 |
if (funder != null) { |
|
34 |
if(!funder.equalsIgnoreCase("WT")) { |
|
35 |
Vocabulary sygmaFundersVocabulary = vocabularyManager.getVocabulary("sygma_funders", Locale.ROOT); |
|
36 |
addExactQueryTerm("relfundinglevel0_id", devocabularizedTerm(funder.toUpperCase(), sygmaFundersVocabulary), queryBuilder); |
|
37 |
} else { |
|
38 |
Vocabulary funderVocabulary = vocabularyManager.getVocabulary("funders_simple", Locale.ROOT); |
|
39 |
addExactQueryTerm("relfunderid", devocabularizedTerm(funder.toUpperCase(), funderVocabulary), queryBuilder); |
|
40 |
} |
|
41 |
} |
|
42 |
|
|
43 |
if (fundingStream != null && !fundingStream.trim().isEmpty()) { |
|
44 |
Vocabulary relfundinglevel1Vocabulary = vocabularyManager.getVocabulary("programmes_simple", Locale.ROOT); |
|
45 |
queryBuilder.append(" and (relfundinglevel1_id exact \"").append(devocabularizedTerm(fundingStream.toUpperCase(), relfundinglevel1Vocabulary)).append("\")"); |
|
46 |
} |
|
47 |
|
|
48 |
if (FP7scientificArea != null && !FP7scientificArea.trim().isEmpty()) { |
|
49 |
Vocabulary relfundinglevel2Vocabulary = vocabularyManager.getVocabulary("areas", Locale.ROOT); |
|
50 |
queryBuilder.append(" and (relfundinglevel2_id exact \"").append(devocabularizedTerm(FP7scientificArea, relfundinglevel2Vocabulary)).append("\")"); |
|
51 |
} |
|
52 |
|
|
53 |
} else { |
|
54 |
|
|
55 |
if (funder != null) { |
|
56 |
Vocabulary funderVocabulary = vocabularyManager.getVocabulary("funders_simple", Locale.ROOT); |
|
57 |
addExactQueryTerm("relfunderid", devocabularizedTerm(funder.toUpperCase(), funderVocabulary), queryBuilder); |
|
58 |
} |
|
59 |
|
|
60 |
if (fundingStream != null && !fundingStream.trim().isEmpty()) { |
|
61 |
addORQueryTerm(new String[]{"relfundinglevel0_name", "relfundinglevel1_name", "relfundinglevel2_name"}, fundingStream.toUpperCase(), queryBuilder); |
|
62 |
} |
|
63 |
|
|
64 |
if (FP7scientificArea != null && !FP7scientificArea.trim().isEmpty()) { |
|
65 |
Vocabulary relfundinglevel2Vocabulary = vocabularyManager.getVocabulary("areas", Locale.ROOT); |
|
66 |
queryBuilder.append(" and (relfundinglevel2_id exact \"").append(devocabularizedTerm(FP7scientificArea, relfundinglevel2Vocabulary)).append("\")"); |
|
67 |
} |
|
68 |
} |
|
69 |
|
|
70 |
} |
|
71 |
|
|
72 |
public static void enhanceProjectQueryWithFundingLevelParams(StringBuilder queryBuilder, HttpServletRequest request, VocabularyManager vocabularyManager) { |
|
73 |
String funder = request.getParameter("funder"); |
|
74 |
String fundingStream = request.getParameter("fundingStream"); |
|
75 |
String FP7scientificArea = request.getParameter("FP7scientificArea"); |
|
76 |
|
|
77 |
if (funder != null) { |
|
78 |
//addExactQueryTerm("fundinglevel0_name", funder.toUpperCase(), queryBuilder); |
|
79 |
Vocabulary funderVocabulary = vocabularyManager.getVocabulary("funders_simple", Locale.ROOT); |
|
80 |
addExactQueryTerm("funderid", devocabularizedTerm(funder.toUpperCase(), funderVocabulary), queryBuilder); |
|
81 |
} |
|
82 |
|
|
83 |
if (fundingStream != null && !fundingStream.trim().isEmpty()) { |
|
84 |
//addExactQueryTerm("fundinglevel1_name", fundingStream.toUpperCase(), queryBuilder); |
|
85 |
addORQueryTerm(new String[]{"fundinglevel0_name", "fundinglevel1_name", "fundinglevel2_name"}, fundingStream.toUpperCase(), queryBuilder); |
|
86 |
} |
|
87 |
|
|
88 |
if (FP7scientificArea != null && !FP7scientificArea.trim().isEmpty()) { |
|
89 |
Vocabulary relfundinglevel2Vocabulary = vocabularyManager.getVocabulary("areas", Locale.ROOT); |
|
90 |
queryBuilder.append(" and (fundinglevel2_id exact \"").append(devocabularizedTerm(FP7scientificArea, relfundinglevel2Vocabulary)).append("\")"); |
|
91 |
} |
|
92 |
} |
|
93 |
|
|
94 |
public static void enhanceQueryWithFundingParams(StringBuilder queryBuilder, HttpServletRequest request) { |
|
95 |
String hasECFunding = request.getParameter("hasECFunding"); |
|
96 |
String hasWTFunding = request.getParameter("hasWTFunding"); |
|
97 |
|
|
98 |
addBooleanQueryTerm("contextid", hasECFunding, "EC", queryBuilder); |
|
99 |
addBooleanQueryTerm("contextid", hasWTFunding, "WT", queryBuilder); |
|
100 |
} |
|
101 |
|
|
102 |
public static void enhanceQueryWithProjectFundingParams(StringBuilder queryBuilder, HttpServletRequest request) { |
|
103 |
String hasECFunding = request.getParameter("hasECFunding"); |
|
104 |
String hasWTFunding = request.getParameter("hasWTFunding"); |
|
105 |
|
|
106 |
addBooleanQueryTerm("funderid", hasECFunding, "ec__________::EC", queryBuilder); |
|
107 |
addBooleanQueryTerm("funderid", hasWTFunding, "wt__________::WT", queryBuilder); |
|
108 |
|
|
109 |
} |
|
110 |
|
|
111 |
public static void enhanceQueryWithRelProjectParams(StringBuilder queryBuilder, HttpServletRequest request) { |
|
112 |
String hasProject = request.getParameter("hasProject"); |
|
113 |
String projectID = request.getParameter("projectID"); |
|
114 |
String FP7ProjectID = request.getParameter("FP7ProjectID"); |
|
115 |
|
|
116 |
if (hasProject != null && !hasProject.isEmpty()) { |
|
117 |
if (hasProject.equals("true")) { |
|
118 |
addEqualQueryTerm("relprojectid", "*", queryBuilder); |
|
119 |
} else { |
|
120 |
addNotEqualQueryTerm("relprojectid", "*", queryBuilder); |
|
121 |
} |
|
122 |
} |
|
123 |
|
|
124 |
if (FP7ProjectID != null && !FP7ProjectID.trim().isEmpty()) { |
|
125 |
addExactQueryTerm("relprojectcode", FP7ProjectID, queryBuilder); |
|
126 |
addExactQueryTerm(" relfundinglevel0_id", "ec__________::EC::FP7", queryBuilder); |
|
127 |
} |
|
128 |
|
|
129 |
if (projectID != null && !projectID.trim().isEmpty()) { |
|
130 |
queryBuilder.append(" and (relprojectcode exact \"").append(projectID).append("\")"); |
|
131 |
} |
|
132 |
} |
|
133 |
|
|
134 |
public static void enhanceQueryWithAccessRights(StringBuilder queryBuilder, HttpServletRequest request) { |
|
135 |
String oa = request.getParameter("OA"); |
|
136 |
addBooleanQueryTerm("resultbestlicense", oa, "Open Access", queryBuilder); |
|
137 |
} |
|
138 |
|
|
139 |
public static void enhanceQueryWithDate(StringBuilder queryBuilder, HttpServletRequest request) throws IllegalArgumentException{ |
|
140 |
String fromDateAccepted = request.getParameter("fromDateAccepted"); |
|
141 |
String toDateAccepted = request.getParameter("toDateAccepted"); |
|
142 |
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd"); |
|
143 |
simpleDateFormat.setLenient(false); |
|
144 |
|
|
145 |
if (toDateAccepted != null && !toDateAccepted.isEmpty() && (fromDateAccepted == null || fromDateAccepted.isEmpty()) ) { |
|
146 |
if(!checkDate(toDateAccepted, simpleDateFormat)){ |
|
147 |
//logger.debug("Format is " + !checkDate(toDateAccepted, simpleDateFormat)); |
|
148 |
throw new IllegalArgumentException("toDateAccepted date must be formatted as YYYY-MM-DD"); |
|
149 |
} |
|
150 |
fromDateAccepted = "*"; |
|
151 |
} |
|
152 |
|
|
153 |
if (fromDateAccepted != null && !fromDateAccepted.isEmpty() && (toDateAccepted == null || toDateAccepted.isEmpty()) ) { |
|
154 |
if (!checkDate(fromDateAccepted, simpleDateFormat)){ |
|
155 |
//logger.debug("Format from is " + fromDateAccepted != null && !fromDateAccepted.isEmpty()); |
|
156 |
throw new IllegalArgumentException("fromDateAccepted date must be formatted as YYYY-MM-DD"); |
|
157 |
} |
|
158 |
toDateAccepted = simpleDateFormat.format(new Date()); |
|
159 |
} |
|
160 |
|
|
161 |
if (toDateAccepted != null && !toDateAccepted.isEmpty() && fromDateAccepted != null && !fromDateAccepted.isEmpty()) { |
|
162 |
queryBuilder.append(" and (resultdateofacceptance within \"").append(fromDateAccepted).append(" ").append(toDateAccepted).append("\")"); |
|
163 |
} |
|
164 |
} |
|
165 |
|
|
166 |
|
|
167 |
public static void enhanceQueryWithYearParams(StringBuilder queryBuilder, HttpServletRequest request) { |
|
168 |
String startYear = request.getParameter("startYear"); |
|
169 |
String endYear = request.getParameter("endYear"); |
|
170 |
|
|
171 |
int sYear = -1; |
|
172 |
if (startYear != null && !startYear.isEmpty()) { |
|
173 |
try { |
|
174 |
sYear = Integer.parseInt(startYear); |
|
175 |
|
|
176 |
} catch(NumberFormatException e) { |
|
177 |
throw new IllegalArgumentException("startYear parameter must be a numeric value.", e); |
|
178 |
} |
|
179 |
} |
|
180 |
|
|
181 |
int eYear = -1; |
|
182 |
if (endYear !=null && !endYear.isEmpty()) { |
|
183 |
try { |
|
184 |
eYear = Integer.parseInt(endYear); |
|
185 |
|
|
186 |
} catch(NumberFormatException e) { |
|
187 |
throw new IllegalArgumentException("endYear parameter must be a numeric value.",e); |
|
188 |
} |
|
189 |
} |
|
190 |
|
|
191 |
if (eYear != -1 && sYear != -1 && eYear < sYear) { |
|
192 |
throw new IllegalArgumentException("endYear must be greater than startYear."); |
|
193 |
} |
|
194 |
|
|
195 |
addExactQueryTerm("projectstartyear", startYear, queryBuilder); |
|
196 |
addExactQueryTerm("projectendyear", endYear, queryBuilder); |
|
197 |
} |
|
198 |
|
|
199 |
public static void enhanceQueryWithResultsSortParameters(StringBuilder queryBuilder, HttpServletRequest request) { |
|
200 |
String sortByParameter = request.getParameter("sortBy"); |
|
201 |
|
|
202 |
if (sortByParameter != null) { |
|
203 |
String[] sortParams = sortByParameter.split(","); |
|
204 |
|
|
205 |
if (sortParams.length != 2) { |
|
206 |
throw new IllegalArgumentException("Invalid sort paremeter. 'sortBy' parameter format is <fieldName>[,ascending|,descending]."); |
|
207 |
} |
|
208 |
|
|
209 |
String sortByField = sortParams[0]; |
|
210 |
String order = sortParams[1]; |
|
211 |
|
|
212 |
if (!checkPublicationSortParameterFields(sortByField)){ |
|
213 |
throw new IllegalArgumentException("'" + sortByField + "' is not a sortable field."); |
|
214 |
} |
|
215 |
|
|
216 |
if (!checkOrder(order)) { |
|
217 |
throw new IllegalArgumentException("'" + order + "' is not a valid ordering. Please use one of {ascending, descending}"); |
|
218 |
} |
|
219 |
|
|
220 |
addSortParameter(sortByField, order, queryBuilder); |
|
221 |
} |
|
222 |
} |
|
223 |
|
|
224 |
public static void enhanceQueryWithProjectSortParameters(StringBuilder queryBuilder, HttpServletRequest request) { |
|
225 |
String sortByParameter = request.getParameter("sortBy"); |
|
226 |
|
|
227 |
if (sortByParameter != null) { |
|
228 |
String[] sortParams = sortByParameter.split(","); |
|
229 |
|
|
230 |
if (sortParams.length != 2) { |
|
231 |
throw new IllegalArgumentException("Invalid sort paremeter. 'sortBy' parameter format is <fieldName>[,ascending|,descending]."); |
|
232 |
} |
|
233 |
|
|
234 |
String sortByField = sortParams[0]; |
|
235 |
String order = sortParams[1]; |
|
236 |
|
|
237 |
if (!checkProjectSortParameterFields(sortByField)){ |
|
238 |
throw new IllegalArgumentException("'" + sortByField + "' is not a sortable field."); |
|
239 |
} |
|
240 |
|
|
241 |
if (!checkOrder(order)) { |
|
242 |
throw new IllegalArgumentException("'" + order + "' is not a valid ordering. Please use one of {ascending, descending}"); |
|
243 |
} |
|
244 |
|
|
245 |
addSortParameter(sortByField, order, queryBuilder); |
|
246 |
} |
|
247 |
} |
|
248 |
|
|
249 |
private static boolean checkPublicationSortParameterFields(String sortField) { |
|
250 |
if ((sortField != null) && (!sortField.isEmpty()) && |
|
251 |
sortField.matches("dateofcollection|resultstoragedate|resultembargoenddate|resultembargoendyear|" + |
|
252 |
"resulttypeid|resulttypename|resultlanguageid|resultlanguagename|resultbestlicense|" + |
|
253 |
"resultbestlicenseid|resultdateofacceptance|resultacceptanceyear")) { |
|
254 |
return true; |
|
255 |
} |
|
256 |
return false; |
|
257 |
} |
|
258 |
|
|
259 |
private static boolean checkProjectSortParameterFields(String sortField) { |
|
260 |
if ((sortField != null) && (!sortField.isEmpty()) && |
|
261 |
sortField.matches("dateofcollection|projectstartdate|projectstartyear|" + |
|
262 |
"projectenddate|projectendyear|projectcallidentifier|projectduration|" + |
|
263 |
"projectecsc39|projectcontracttypeid|projectcontracttypename")) { |
|
264 |
return true; |
|
265 |
} |
|
266 |
return false; |
|
267 |
} |
|
268 |
|
|
269 |
private static boolean checkOrder(String order) { |
|
270 |
if (order.matches("ascending|descending")) { |
|
271 |
return true; |
|
272 |
} |
|
273 |
return false; |
|
274 |
} |
|
275 |
|
|
276 |
public static boolean checkDate(String date, SimpleDateFormat simpleDateFormat) { |
|
277 |
try { |
|
278 |
simpleDateFormat.parse(date); |
|
279 |
|
|
280 |
} catch (ParseException pe) { |
|
281 |
logger.warn("Wrong date format.", pe); |
|
282 |
return false; |
|
283 |
} |
|
284 |
|
|
285 |
return true; |
|
286 |
} |
|
287 |
|
|
288 |
/** |
|
289 |
* Enhance the given CQL query with OpenAIRE specific ids |
|
290 |
* @param queryBuilder |
|
291 |
* @param request |
|
292 |
* @return |
|
293 |
*/ |
|
294 |
public static void enhanceQueryWithOpenAIREIds(StringBuilder queryBuilder, HttpServletRequest request) { |
|
295 |
String[] openairePublicationIDs = request.getParameterValues("openairePublicationID"); |
|
296 |
String[] openaireDatasetIDs = request.getParameterValues("openaireDatasetID"); |
|
297 |
String[] openaireAuthorIDs = request.getParameterValues("openaireAuthorID"); |
|
298 |
String[] openaireProviderIDs = request.getParameterValues("openaireProviderID"); |
|
299 |
String[] openaireProjectIDs = request.getParameterValues("openaireProjectID"); |
|
300 |
|
|
301 |
enhanceQueryWithIds("objidentifier", openairePublicationIDs, queryBuilder); |
|
302 |
enhanceQueryWithIds("objidentifier", openaireDatasetIDs, queryBuilder); |
|
303 |
enhanceQueryWithIds("relpersonid", openaireAuthorIDs, queryBuilder); |
|
304 |
enhanceQueryWithIds("resulthostingdatasourceid", openaireProviderIDs, queryBuilder); |
|
305 |
enhanceQueryWithIds("relprojectid", openaireProjectIDs, queryBuilder); |
|
306 |
} |
|
307 |
|
|
308 |
public static void enhanceQueryWithMetadataKeywords(StringBuilder queryBuilder, HttpServletRequest request) { |
|
309 |
String keywords = request.getParameter("keywords"); |
|
310 |
String title = request.getParameter("title"); |
|
311 |
String author = request.getParameter("author"); |
|
312 |
|
|
313 |
addMetadataQueryTerm(null, keywords, queryBuilder); |
|
314 |
addMetadataQueryTerm("resulttitle", title, queryBuilder); |
|
315 |
addMetadataQueryTerm("relperson", author, queryBuilder); |
|
316 |
} |
|
317 |
|
|
318 |
public static void enhanceQueryWithProjectMetadataKeywords(StringBuilder queryBuilder, HttpServletRequest request) { |
|
319 |
String keywords = request.getParameter("keywords"); |
|
320 |
String acronym = request.getParameter("acronym"); |
|
321 |
String name = request.getParameter("name"); |
|
322 |
String grantID = request.getParameter("grantID"); |
|
323 |
String callID = request.getParameter("callID"); |
|
324 |
|
|
325 |
addMetadataQueryTerm(null, keywords, queryBuilder); |
|
326 |
addMetadataQueryTerm("projectacronym", acronym, queryBuilder); |
|
327 |
addEqualQueryTerm("projecttitle", name, queryBuilder); |
|
328 |
addExactQueryTerm("projectcode", grantID, queryBuilder); |
|
329 |
addExactQueryTerm("projectcallidentifier", callID, queryBuilder); |
|
330 |
} |
|
331 |
|
|
332 |
public static void enhanceQueryWithParticipantsInfoParams(StringBuilder queryBuilder, HttpServletRequest request) { |
|
333 |
String[] participantCountries = request.getParameterValues("participantCountries"); |
|
334 |
String participantAcronyms = request.getParameter("participantAcronyms"); |
|
335 |
String openaireParticipantID = request.getParameter("openaireParticipantID"); |
|
336 |
|
|
337 |
|
|
338 |
if(participantCountries != null) { |
|
339 |
enhanceQueryWithCommaSeparatedValues("relorganizationcountryid", participantCountries, queryBuilder); |
|
340 |
} |
|
341 |
|
|
342 |
addORQueryTerm("relorganizationname", "relorganizationshortname", participantAcronyms, queryBuilder); |
|
343 |
|
|
344 |
if(openaireParticipantID != null) { |
|
345 |
addEqualQueryTerm("relorganizationid", openaireParticipantID, queryBuilder); |
|
346 |
} |
|
347 |
|
|
348 |
} |
|
349 |
|
|
350 |
public static void enhanceQueryWithSC39Params(StringBuilder queryBuilder, HttpServletRequest request) { |
|
351 |
String sc39 = request.getParameter("sc39"); |
|
352 |
addBooleanQueryTerm("projectecsc39", sc39, queryBuilder); |
|
353 |
} |
|
354 |
|
|
355 |
|
|
356 |
|
|
357 |
|
|
358 |
public static void enhanceQueryWithDoi(StringBuilder queryBuilder, HttpServletRequest request) { |
|
359 |
String[] dois = request.getParameterValues("doi"); |
|
360 |
|
|
361 |
if (dois != null && !(dois.length==0)) { |
|
362 |
queryBuilder.append(" and "); |
|
363 |
for (int i = 0; i < dois.length; i++) { |
|
364 |
String[] commaSeparated = dois[i].split(","); |
|
365 |
for (int j = 0; j < commaSeparated.length; j++) { |
|
366 |
queryBuilder.append("(pidclassid exact \"doi\" and pid exact \"").append(commaSeparated[j]).append("\")"); |
|
367 |
if (i < dois.length-1 || j < commaSeparated.length-1 ) { |
|
368 |
queryBuilder.append(" or "); |
|
369 |
} |
|
370 |
} |
|
371 |
} |
|
372 |
} |
|
373 |
} |
|
374 |
|
|
375 |
public static void enhanceProjectQueryWithOpenAIREIds(StringBuilder queryBuilder, HttpServletRequest request) { |
|
376 |
String openaireProjectID = request.getParameter("openaireProjectID"); |
|
377 |
|
|
378 |
if (openaireProjectID != null) { |
|
379 |
addExactQueryTerm("objIdentifier", openaireProjectID, queryBuilder); |
|
380 |
} |
|
381 |
} |
|
382 |
|
|
383 |
public static void enhanceQueryWithIds(String idIndexFieldName, String[] ids, StringBuilder queryBuilder) { |
|
384 |
if (ids != null && !(ids.length==0)) { |
|
385 |
queryBuilder.append(" and "); |
|
386 |
for (int i = 0; i < ids.length; i++) { |
|
387 |
String[] commaSeparated = ids[i].split(","); |
|
388 |
for (int j = 0; j < commaSeparated.length; j++) { |
|
389 |
queryBuilder.append("(" + idIndexFieldName + " exact \"").append(commaSeparated[j]).append("\")"); |
|
390 |
if (i < ids.length-1 || j < commaSeparated.length-1 ) { |
|
391 |
queryBuilder.append(" or "); |
|
392 |
} |
|
393 |
} |
|
394 |
} |
|
395 |
} |
|
396 |
} |
|
397 |
|
|
398 |
public static void enhanceQueryWithCommaSeparatedValues(String indexFieldName, String[] fieldValues, StringBuilder queryBuilder) { |
|
399 |
if (fieldValues != null && !(fieldValues.length==0)) { |
|
400 |
queryBuilder.append(" and "); |
|
401 |
for (int i = 0; i < fieldValues.length; i++) { |
|
402 |
String[] commaSeparated = fieldValues[i].split(","); |
|
403 |
for (int j = 0; j < commaSeparated.length; j++) { |
|
404 |
queryBuilder.append("(").append(indexFieldName).append(" exact \"").append(commaSeparated[j].toUpperCase()).append("\")"); |
|
405 |
if (i < fieldValues.length-1 || j < commaSeparated.length-1 ) { |
|
406 |
queryBuilder.append(" and "); |
|
407 |
} |
|
408 |
} |
|
409 |
} |
|
410 |
} |
|
411 |
} |
|
412 |
|
|
413 |
public static void addMetadataQueryTerm(String indexFieldName, String fieldValue, StringBuilder queryBuilder) { |
|
414 |
if (fieldValue != null && !fieldValue.trim().isEmpty()) { |
|
415 |
if(indexFieldName != null) { |
|
416 |
for (String term: fieldValue.trim().split(" ")){ |
|
417 |
queryBuilder.append(" and (").append(indexFieldName).append(" = ").append(term).append(")"); |
|
418 |
} |
|
419 |
} else { |
|
420 |
queryBuilder.append(" and ( " ); |
|
421 |
String[] keywords = fieldValue.trim().split(" "); |
|
422 |
for (int i = 0; i < keywords.length; i++) { |
|
423 |
if (i == keywords.length -1) { |
|
424 |
queryBuilder.append(keywords[i]); |
|
425 |
} else { |
|
426 |
queryBuilder.append(keywords[i]).append(" and "); |
|
427 |
} |
|
428 |
} |
|
429 |
queryBuilder.append(")" ); |
|
430 |
} |
|
431 |
} |
|
432 |
} |
|
433 |
|
|
434 |
public static void addORQueryTerm(String indexFieldName1, String indexFieldName2, String fieldValue, StringBuilder queryBuilder) { |
|
435 |
if (fieldValue != null && !fieldValue.trim().isEmpty()) { |
|
436 |
for (String term: fieldValue.trim().split(" ")) { |
|
437 |
queryBuilder.append(" and (" + indexFieldName1 + " = " + term + " or " + indexFieldName2 + " = " + term + ")"); |
|
438 |
} |
|
439 |
} |
|
440 |
} |
|
441 |
|
|
442 |
public static void addORQueryTerm(String[] indexFields, String fieldValue, StringBuilder queryBuilder) { |
|
443 |
if (fieldValue != null && !fieldValue.trim().isEmpty()) { |
|
444 |
queryBuilder.append(" and ( "); |
|
445 |
for (int i = 0; i < indexFields.length; i++) { |
|
446 |
for (String term : fieldValue.trim().split(" ")) { |
|
447 |
queryBuilder.append(" (" + indexFields[i] + " = " + term + ")"); |
|
448 |
} |
|
449 |
|
|
450 |
if (i == indexFields.length-1) { |
|
451 |
queryBuilder.append(")"); |
|
452 |
|
|
453 |
} else { |
|
454 |
queryBuilder.append(" or "); |
|
455 |
} |
|
456 |
|
|
457 |
} |
|
458 |
} |
|
459 |
} |
|
460 |
|
|
461 |
private static void addBooleanQueryTerm(String indexFieldName, String requestValue, StringBuilder queryBuilder) { |
|
462 |
if (requestValue != null && !requestValue.trim().isEmpty()) { |
|
463 |
addExactQueryTerm(indexFieldName, requestValue, queryBuilder); |
|
464 |
} |
|
465 |
} |
|
466 |
|
|
467 |
public static void addBooleanQueryTerm(String indexFieldName, String requestValue, String fieldValue, StringBuilder queryBuilder){ |
|
468 |
if (requestValue != null && !requestValue.trim().isEmpty()) { |
|
469 |
if (requestValue.trim().equals("true")) { |
|
470 |
addExactQueryTerm(indexFieldName, fieldValue, queryBuilder); |
|
471 |
} else { |
|
472 |
addDifferentEqualQueryTerm(indexFieldName, fieldValue, queryBuilder); |
|
473 |
} |
|
474 |
} |
|
475 |
} |
|
476 |
|
|
477 |
public static void addExactQueryTerm(String indexFieldName, String fieldValue, StringBuilder queryBuilder){ |
|
478 |
if (fieldValue != null && !fieldValue.trim().isEmpty()) { |
|
479 |
queryBuilder.append(" and (" + indexFieldName + " exact \""+ fieldValue +"\")"); |
|
480 |
} |
|
481 |
} |
|
482 |
|
|
483 |
public static void addEqualQueryTerm(String indexFieldName, String fieldValue, StringBuilder queryBuilder){ |
|
484 |
if (fieldValue != null && !fieldValue.trim().isEmpty()) { |
|
485 |
queryBuilder.append(" and (" + indexFieldName + " = \""+ fieldValue.replaceAll("\"","") +"\")"); |
|
486 |
} |
|
487 |
} |
|
488 |
|
|
489 |
public static void addNotEqualQueryTerm(String indexFieldName, String fieldValue, StringBuilder queryBuilder){ |
|
490 |
if (fieldValue != null && !fieldValue.trim().isEmpty()) { |
|
491 |
queryBuilder.append(" not ").append(indexFieldName).append(" = \"").append(fieldValue).append("\""); |
|
492 |
} |
|
493 |
} |
|
494 |
|
|
495 |
public static void addDifferentEqualQueryTerm(String indexFieldName, String fieldValue, StringBuilder queryBuilder){ |
|
496 |
if (fieldValue != null && !fieldValue.trim().isEmpty()) { |
|
497 |
queryBuilder.append(" and (").append(indexFieldName).append(" <> \"").append(fieldValue).append("\")"); |
|
498 |
} |
|
499 |
} |
|
500 |
|
|
501 |
public static void addVocabularizedQueryTerm(String indexFieldName, String fieldValue, StringBuilder queryBuilder, Vocabulary vocabulary){ |
|
502 |
if (fieldValue != null && !fieldValue.trim().isEmpty()) { |
|
503 |
queryBuilder.append(" and (").append(indexFieldName).append(" exact \"").append(devocabularizedTerm(fieldValue, vocabulary)).append("\")"); |
|
504 |
} |
|
505 |
} |
|
506 |
|
|
507 |
private static void addSortParameter(String indexField, String order, StringBuilder queryBuilder) { |
|
508 |
queryBuilder.append(" sortBy " + indexField + "/sort." + order); |
|
509 |
} |
|
510 |
|
|
511 |
/** |
|
512 |
* Returns the encoding of the given value. If the encoding does not exist |
|
513 |
* in the given vocabulary the method returns the original value.| |
|
514 |
* @param value the value to be encoded |
|
515 |
* @param vocabulary the vocabulary containing the encoding - value mapping |
|
516 |
* @return |
|
517 |
*/ |
|
518 |
public static String devocabularizedTerm(String value, Vocabulary vocabulary) { |
|
519 |
if (vocabulary != null) { |
|
520 |
String term = vocabulary.getEncoding(value); |
|
521 |
if (term == null) { |
|
522 |
return value; |
|
523 |
} |
|
524 |
return term; |
|
525 |
} |
|
526 |
return value; |
|
527 |
} |
|
528 |
} |
modules/uoa-search/branches/newAPI/src/main/java/eu/dnetlib/data/search/web/api/CQLQueryGeneration.java | ||
---|---|---|
1 |
package eu.dnetlib.data.search.web.api; |
|
2 |
|
|
3 |
import java.util.List; |
|
4 |
|
|
5 |
/** |
|
6 |
* Created by kiatrop on 28/9/2016. |
|
7 |
*/ |
|
8 |
public class CQLQueryGeneration { |
|
9 |
|
|
10 |
public enum Operator { |
|
11 |
AND("and"), |
|
12 |
OR("or"), |
|
13 |
NOT("not"), |
|
14 |
EQUAL("equal"), |
|
15 |
EXACT("exact"); |
|
16 |
|
|
17 |
private String value; |
|
18 |
Operator(String value) { |
|
19 |
this.value = value; |
|
20 |
} |
|
21 |
private String getValue() { |
|
22 |
return value; |
|
23 |
} |
|
24 |
|
|
25 |
@Override |
|
26 |
public String toString() { |
|
27 |
return this.getValue(); |
|
28 |
} |
|
29 |
|
|
30 |
|
|
31 |
} |
|
32 |
|
|
33 |
public static void appendKeywords(StringBuilder stringBuilder, String keywords) { |
|
34 |
if (keywords != null && !keywords.trim().isEmpty()) { |
|
35 |
stringBuilder.append(" ").append(Operator.AND).append(" (").append(keywords).append(")"); |
|
36 |
} |
|
37 |
} |
|
38 |
|
|
39 |
public static void appendSimpleTerm(StringBuilder stringBuilder, Operator queryOperator, String termValue) { |
|
40 |
stringBuilder.append(' ').append(queryOperator).append(" (").append(termValue).append(')'); |
|
41 |
} |
|
42 |
|
|
43 |
public static void appendSimpleTerms(StringBuilder stringBuilder, Operator queryOperator, List<String> termValues) { |
|
44 |
for (String termValue: termValues) { |
|
45 |
appendSimpleTerm(stringBuilder, queryOperator, termValue); |
|
46 |
} |
|
47 |
} |
|
48 |
|
|
49 |
public static void appendTerm(StringBuilder stringBuilder, Operator queryOperator, String termName, Operator termOperator, String termValue) { |
|
50 |
stringBuilder.append(' ').append(queryOperator).append(" ( "). |
|
51 |
append(termName).append(' ').append(termOperator).append(' ').append(termValue).append(" )"); |
|
52 |
} |
|
53 |
|
|
54 |
public static void appendTerms(StringBuilder stringBuilder, Operator queryOperator, String termName, Operator termOperator, List<String> termValues) { |
|
55 |
for (String termValue: termValues) { |
|
56 |
appendTerm(stringBuilder, queryOperator, termName, termOperator, termValue); |
|
57 |
} |
|
58 |
} |
|
59 |
} |
modules/uoa-search/branches/newAPI/src/test/java/eu/dnetlib/data/search/app/plan/web/api/APIResponseFormatterTest.java | ||
---|---|---|
1 | 1 |
package eu.dnetlib.data.search.app.plan.web.api; |
2 | 2 |
|
3 | 3 |
import eu.dnetlib.data.search.web.api.APIResponseFormatter; |
4 |
import eu.dnetlib.data.search.web.utils.RequestResponseHandler; |
|
5 |
import eu.dnetlib.domain.data.SearchResult; |
|
6 | 4 |
import junit.framework.Assert; |
7 | 5 |
import org.apache.commons.lang.StringEscapeUtils; |
8 | 6 |
import org.apache.log4j.BasicConfigurator; |
... | ... | |
59 | 57 |
|
60 | 58 |
} |
61 | 59 |
|
62 |
@Test |
|
63 |
public void createEntitiesResponseTest(){ |
|
64 |
//SearchApiService.getResponseByEntity(String fullQuery, RequestResponseHandler.Entity entity, int offset, int limit, String format, HttpServletRequest request, boolean refine, List<String> refineFields, List<String> fieldQueries, String type) { |
|
65 |
SearchResult emptySearchResult = new SearchResult("oaftype=result", null, 0, 0, 0, null, null, null); |
|
66 |
System.out.println(APIResponseFormatter.createEntitiesResponse(requestMock, RequestResponseHandler.Entity.PUBLICATION, "oaftype=result", null, emptySearchResult, true, MediaType.APPLICATION_JSON)); |
|
67 |
} |
|
68 | 60 |
} |
modules/uoa-search/branches/newAPI/src/test/java/eu/dnetlib/data/search/utils/cql/QueryEnhancerTest.java | ||
---|---|---|
1 |
package eu.dnetlib.data.search.utils.cql; |
|
2 |
|
|
3 |
import org.junit.Assert; |
|
4 |
import org.junit.Test; |
|
5 |
|
|
6 |
/** |
|
7 |
* Created by kiatrop on 30/11/2016. |
|
8 |
*/ |
|
9 |
public class QueryEnhancerTest { |
|
10 |
String indexfieldName = "indexfieldName"; |
|
11 |
String fieldvalue = "fieldvalue"; |
|
12 |
|
|
13 |
@Test |
|
14 |
public void testExact(){ |
|
15 |
StringBuilder queryBuilder1 = new StringBuilder(); |
|
16 |
StringBuilder queryBuilder = new StringBuilder(); |
|
17 |
|
|
18 |
ParameterQueryEnhancer.addExactQueryTerm(indexfieldName, fieldvalue, queryBuilder1); |
|
19 |
CQLQueryBuilder.appendFieldQuotedTerm(queryBuilder, CQLQueryBuilder.Operator.AND, indexfieldName, CQLQueryBuilder.Operator.EXACT, fieldvalue); |
|
20 |
|
|
21 |
Assert.assertEquals(queryBuilder1.toString(), queryBuilder.toString()); |
|
22 |
} |
|
23 |
|
|
24 |
@Test |
|
25 |
public void testEqual(){ |
|
26 |
StringBuilder queryBuilder1 = new StringBuilder(); |
|
27 |
StringBuilder queryBuilder2 = new StringBuilder(); |
|
28 |
|
|
29 |
ParameterQueryEnhancer.addEqualQueryTerm(indexfieldName, fieldvalue, queryBuilder1); |
|
30 |
CQLQueryBuilder.appendFieldQuotedTerm(queryBuilder2, CQLQueryBuilder.Operator.AND, indexfieldName, CQLQueryBuilder.Operator.EQUAL, fieldvalue); |
|
31 |
|
|
32 |
Assert.assertEquals(queryBuilder1.toString(), queryBuilder2.toString()); |
|
33 |
} |
|
34 |
|
|
35 |
@Test |
|
36 |
public void testNotEqual(){ |
|
37 |
StringBuilder queryBuilder1 = new StringBuilder(); |
|
38 |
StringBuilder queryBuilder2 = new StringBuilder(); |
|
39 |
|
|
40 |
ParameterQueryEnhancer.addNotEqualQueryTerm(indexfieldName, fieldvalue, queryBuilder1); |
|
41 |
CQLQueryBuilder.appendFieldQuotedTerm(queryBuilder2, CQLQueryBuilder.Operator.NOT, indexfieldName, CQLQueryBuilder.Operator.EQUAL, fieldvalue); |
|
42 |
|
|
43 |
Assert.assertEquals(queryBuilder1.toString(), queryBuilder2.toString()); |
|
44 |
} |
|
45 |
|
|
46 |
@Test |
|
47 |
public void testDifferentEqual(){ |
|
48 |
StringBuilder queryBuilder1 = new StringBuilder(); |
|
49 |
StringBuilder queryBuilder2 = new StringBuilder(); |
|
50 |
|
|
51 |
ParameterQueryEnhancer.addDifferentQueryTerm(indexfieldName, fieldvalue, queryBuilder1); |
|
52 |
CQLQueryBuilder.appendFieldQuotedTerm(queryBuilder2, CQLQueryBuilder.Operator.AND, indexfieldName, CQLQueryBuilder.Operator.NOTEQUAL, fieldvalue); |
|
53 |
|
|
54 |
Assert.assertEquals(queryBuilder1.toString(), queryBuilder2.toString()); |
|
55 |
} |
|
56 |
|
|
57 |
@Test |
|
58 |
public void testKeywords() { |
|
59 |
StringBuilder queryBuilder1 = new StringBuilder(); |
|
60 |
StringBuilder queryBuilder2 = new StringBuilder(); |
|
61 |
|
|
62 |
CQLQueryBuilder.appendKeywords(queryBuilder1, "keywords"); |
|
63 |
CQLQueryBuilder.appendSimpleTerm(queryBuilder2, CQLQueryBuilder.Operator.AND, "keywords"); |
|
64 |
|
|
65 |
Assert.assertEquals(queryBuilder1.toString(), queryBuilder2.toString()); |
|
66 |
} |
|
67 |
} |
modules/uoa-search/branches/newAPI/src/test/java/eu/dnetlib/data/search/utils/solr/SolrUtilsTest.java | ||
---|---|---|
1 |
package eu.dnetlib.data.search.utils.solr; |
|
2 |
|
|
3 |
import eu.dnetlib.data.search.app.SearchServiceImpl; |
|
4 |
import eu.dnetlib.data.search.solr.SolrResultSet; |
|
5 |
import eu.dnetlib.data.search.web.api.SearchApiService; |
|
6 |
import eu.dnetlib.data.search.web.utils.RequestResponseHandler; |
|
7 |
import eu.dnetlib.functionality.index.cql.CqlTranslator; |
|
8 |
import eu.dnetlib.functionality.index.cql.CqlTranslatorImpl; |
|
9 |
import org.apache.log4j.BasicConfigurator; |
|
10 |
import org.apache.solr.client.solrj.impl.CloudSolrServer; |
|
11 |
import org.apache.solr.common.util.NamedList; |
|
12 |
import org.junit.Assert; |
|
13 |
import org.junit.Before; |
|
14 |
import org.junit.Test; |
|
15 |
import org.mockito.Mockito; |
|
16 |
import org.z3950.zing.cql.CQLParseException; |
|
17 |
|
|
18 |
import javax.servlet.http.HttpServletRequest; |
|
19 |
import java.io.IOException; |
|
20 |
import java.util.ArrayList; |
|
21 |
import java.util.Arrays; |
|
22 |
import java.util.List; |
|
23 |
|
|
24 |
import static org.mockito.Matchers.anyString; |
|
25 |
import static org.mockito.Mockito.doNothing; |
|
26 |
|
|
27 |
|
|
28 |
/** |
|
29 |
* Created by kiatrop on 9/11/2016. |
|
30 |
*/ |
|
31 |
public class SolrUtilsTest { |
|
32 |
|
|
33 |
private org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(SearchApiService.class); |
|
34 |
|
|
35 |
HttpServletRequest requestMock; |
|
36 |
RequestResponseHandler.Entity entity; |
|
37 |
List<String> refineFields = new ArrayList<String>(); |
|
38 |
List<String> fieldQueries = new ArrayList<String>(); |
|
39 |
CloudSolrServer mockSolrClient; |
|
40 |
SolrResultSet mockRS; |
|
41 |
|
|
42 |
@Before |
|
43 |
public void before() { |
|
44 |
BasicConfigurator.configure(); |
|
45 |
mockSolrClient = Mockito.mock(CloudSolrServer.class); |
|
46 |
doNothing().when(mockSolrClient).setDefaultCollection(anyString()); |
|
47 |
|
|
48 |
mockRS = Mockito.mock(SolrResultSet.class); |
|
49 |
|
|
50 |
refineFields = Arrays.asList(new String[]{"relfunderid", "relfundinglevel0_id", "relfundinglevel1_id", "relfundinglevel2_id"}); |
|
51 |
|
|
52 |
fieldQueries = Arrays.asList(new String[]{"(relfunderid exact ec___::EC) and (relfunderid exact wt___::WT)", |
|
53 |
"relfundinglevel0_id exact ec___::EC::SP1" , "(relfundinglevel1_id exact ec___::EC::SP1::VALUE1) or (relfundinglevel1_id exact ec___::EC::SP1::VALUE2)"}); |
|
54 |
} |
|
55 |
|
|
56 |
|
|
57 |
@Test |
|
58 |
public void createEprQuery() { |
|
59 |
String query = SearchServiceImpl.createEprQuery("(oaftype=result)", refineFields, fieldQueries); |
|
60 |
Assert.assertEquals("query=(oaftype=result)" + |
|
61 |
"&groupby=relfunderid,relfundinglevel0_id,relfundinglevel1_id,relfundinglevel2_id" + |
|
62 |
"&fq=(relfunderid exact ec___::EC) and (relfunderid exact wt___::WT),relfundinglevel0_id exact ec___::EC::SP1," + |
|
63 |
"(relfundinglevel1_id exact ec___::EC::SP1::VALUE1) or (relfundinglevel1_id exact ec___::EC::SP1::VALUE2)", query); |
|
64 |
|
|
65 |
//empty refine, empty fq |
|
66 |
query = SearchServiceImpl.createEprQuery("(oaftype=result)", new ArrayList<String>(), new ArrayList<String>()); |
|
67 |
Assert.assertEquals("query=(oaftype=result)" + |
|
68 |
"&groupby=" + |
|
69 |
"&fq=", query); |
|
70 |
|
|
71 |
//empty fq |
|
72 |
query = SearchServiceImpl.createEprQuery("(oaftype=result)", refineFields, new ArrayList<String>()); |
|
73 |
Assert.assertEquals("query=(oaftype=result)" + |
|
74 |
"&groupby=relfunderid,relfundinglevel0_id,relfundinglevel1_id,relfundinglevel2_id" + |
|
75 |
"&fq=", query); |
|
76 |
|
|
77 |
//empty refine |
|
78 |
query = SearchServiceImpl.createEprQuery("(oaftype=result)", new ArrayList<String>(), fieldQueries); |
|
79 |
Assert.assertEquals("query=(oaftype=result)" + |
|
80 |
"&groupby=" + |
|
81 |
"&fq=(relfunderid exact ec___::EC) and (relfunderid exact wt___::WT),relfundinglevel0_id exact ec___::EC::SP1," + |
|
82 |
"(relfundinglevel1_id exact ec___::EC::SP1::VALUE1) or (relfundinglevel1_id exact ec___::EC::SP1::VALUE2)", query); |
|
83 |
|
|
84 |
//empty query |
|
85 |
query = SearchServiceImpl.createEprQuery("", new ArrayList<String>(), new ArrayList<String>()); |
|
86 |
Assert.assertEquals("query=" + |
|
87 |
"&groupby=" + |
|
88 |
"&fq=", query); |
|
89 |
|
|
90 |
//null values |
|
91 |
query = SearchServiceImpl.createEprQuery(null, null, null); |
|
92 |
Assert.assertEquals("query=" + |
|
93 |
"&groupby=" + |
|
94 |
"&fq=", query); |
|
95 |
|
|
96 |
} |
|
97 |
|
|
98 |
@Test |
|
99 |
public void extractQueryOptions() throws IOException, CQLParseException { |
|
100 |
CqlTranslator cqlTranslator = new CqlTranslatorImpl(); |
|
101 |
|
|
102 |
NamedList<String> actual = SolrResultSetOptionsUtil.extractQueryOptions(SearchServiceImpl.createEprQuery("(oaftype=result)", refineFields, fieldQueries)); |
|
103 |
Assert.assertEquals(cqlTranslator.toLucene("(oaftype=result)"), actual.get("q")); |
|
104 |
Assert.assertEquals("__result", actual.get("fl")); |
|
105 |
Assert.assertEquals("AND", actual.get("q.op")); |
|
106 |
Assert.assertEquals("true", actual.get("shards.tolerant")); |
|
107 |
Assert.assertEquals("true", actual.get("facet")); |
|
108 |
Assert.assertEquals("1", actual.get("facet.mincount")); |
|
109 |
Assert.assertEquals(refineFields.size() + "", actual.get("facet.threads")); |
|
110 |
Assert.assertEquals(refineFields.size(), actual.getAll("facet.field").size()); |
|
111 |
Assert.assertTrue(actual.getAll("facet.field").contains("relfunderid")); |
|
112 |
Assert.assertTrue(actual.getAll("facet.field").contains("relfundinglevel1_id")); |
|
113 |
Assert.assertTrue(actual.getAll("facet.field").contains("relfundinglevel2_id")); |
|
114 |
Assert.assertFalse(actual.getAll("facet.field").contains("relfundinglevel3_id")); |
|
115 |
Assert.assertEquals(fieldQueries.size(), actual.getAll("fq").size()); |
|
116 |
Assert.assertTrue(actual.getAll("fq").contains(cqlTranslator.toLucene("(relfunderid exact ec___::EC) and (relfunderid exact wt___::WT)"))); |
|
117 |
Assert.assertTrue(actual.getAll("fq").contains(cqlTranslator.toLucene("relfundinglevel0_id exact ec___::EC::SP1"))); |
|
118 |
Assert.assertTrue(actual.getAll("fq").contains(cqlTranslator.toLucene("(relfundinglevel1_id exact ec___::EC::SP1::VALUE1) or (relfundinglevel1_id exact ec___::EC::SP1::VALUE2)"))); |
|
119 |
|
|
120 |
//empty refine, empty fq |
|
121 |
actual = SolrResultSetOptionsUtil.extractQueryOptions(SearchServiceImpl.createEprQuery("(oaftype=result)", new ArrayList<String>(), new ArrayList<String>())); |
|
122 |
Assert.assertEquals(cqlTranslator.toLucene("(oaftype=result)"), actual.get("q")); |
|
123 |
Assert.assertEquals("__result", actual.get("fl")); |
|
124 |
Assert.assertNull(actual.get("q.op")); |
|
125 |
Assert.assertEquals("true", actual.get("shards.tolerant")); |
|
126 |
Assert.assertNull(actual.get("facet")); |
|
127 |
Assert.assertNull(actual.get("facet.mincount")); |
|
128 |
Assert.assertNull(actual.get("facet.threads")); |
|
129 |
Assert.assertNull(actual.get("facet.field")); |
|
130 |
Assert.assertTrue(actual.getAll("facet.field").isEmpty()); |
|
131 |
Assert.assertNull(actual.get("fq")); |
|
132 |
Assert.assertTrue(actual.getAll("fq").isEmpty()); |
|
133 |
|
|
134 |
//empty fq |
|
135 |
actual = SolrResultSetOptionsUtil.extractQueryOptions(SearchServiceImpl.createEprQuery("(oaftype=result)", refineFields, new ArrayList<String>())); |
|
136 |
Assert.assertEquals(cqlTranslator.toLucene("(oaftype=result)"), actual.get("q")); |
|
137 |
Assert.assertEquals("__result", actual.get("fl")); |
|
138 |
Assert.assertNull(actual.get("q.op")); |
|
139 |
Assert.assertEquals("true", actual.get("shards.tolerant")); |
|
140 |
Assert.assertEquals("true", actual.get("facet")); |
|
141 |
Assert.assertEquals("1", actual.get("facet.mincount")); |
|
142 |
Assert.assertEquals(refineFields.size() + "", actual.get("facet.threads")); |
|
143 |
Assert.assertEquals(refineFields.size(), actual.getAll("facet.field").size()); |
|
144 |
Assert.assertTrue(actual.getAll("facet.field").contains("relfunderid")); |
|
145 |
Assert.assertTrue(actual.getAll("facet.field").contains("relfundinglevel1_id")); |
|
146 |
Assert.assertTrue(actual.getAll("facet.field").contains("relfundinglevel2_id")); |
|
147 |
Assert.assertFalse(actual.getAll("facet.field").contains("relfundinglevel3_id")); |
|
148 |
Assert.assertNull(actual.get("fq")); |
|
149 |
Assert.assertTrue(actual.getAll("fq").isEmpty()); |
|
150 |
|
|
151 |
//null fq |
|
152 |
actual = SolrResultSetOptionsUtil.extractQueryOptions(SearchServiceImpl.createEprQuery("(oaftype=result)", refineFields, null)); |
|
153 |
Assert.assertEquals(cqlTranslator.toLucene("(oaftype=result)"), actual.get("q")); |
|
154 |
Assert.assertEquals("__result", actual.get("fl")); |
|
155 |
Assert.assertNull(actual.get("q.op")); |
|
156 |
Assert.assertEquals("true", actual.get("shards.tolerant")); |
|
157 |
Assert.assertEquals("true", actual.get("facet")); |
|
158 |
Assert.assertEquals("1", actual.get("facet.mincount")); |
|
159 |
Assert.assertEquals(refineFields.size() + "", actual.get("facet.threads")); |
|
160 |
Assert.assertEquals(refineFields.size(), actual.getAll("facet.field").size()); |
|
161 |
Assert.assertTrue(actual.getAll("facet.field").contains("relfunderid")); |
|
162 |
Assert.assertTrue(actual.getAll("facet.field").contains("relfundinglevel1_id")); |
|
163 |
Assert.assertTrue(actual.getAll("facet.field").contains("relfundinglevel2_id")); |
|
164 |
Assert.assertFalse(actual.getAll("facet.field").contains("relfundinglevel3_id")); |
|
165 |
Assert.assertNull(actual.get("fq")); |
|
166 |
Assert.assertTrue(actual.getAll("fq").isEmpty()); |
|
167 |
|
|
168 |
//empty refine |
|
169 |
actual = SolrResultSetOptionsUtil.extractQueryOptions(SearchServiceImpl.createEprQuery("(oaftype=result)", new ArrayList<String>(), fieldQueries)); |
|
170 |
Assert.assertEquals(cqlTranslator.toLucene("(oaftype=result)"), actual.get("q")); |
|
171 |
Assert.assertEquals("__result", actual.get("fl")); |
|
172 |
Assert.assertEquals("AND", actual.get("q.op")); |
|
173 |
Assert.assertEquals("true", actual.get("shards.tolerant")); |
|
174 |
Assert.assertNull(actual.get("facet")); |
|
175 |
Assert.assertNull(actual.get("facet.mincount")); |
|
176 |
Assert.assertNull(actual.get("facet.threads")); |
|
177 |
Assert.assertNull(actual.get("facet.field")); |
|
178 |
Assert.assertTrue(actual.getAll("facet.field").isEmpty()); |
|
179 |
Assert.assertEquals(fieldQueries.size(), actual.getAll("fq").size()); |
|
180 |
Assert.assertTrue(actual.getAll("fq").contains(cqlTranslator.toLucene("(relfunderid exact ec___::EC) and (relfunderid exact wt___::WT)"))); |
|
181 |
Assert.assertTrue(actual.getAll("fq").contains(cqlTranslator.toLucene("relfundinglevel0_id exact ec___::EC::SP1"))); |
|
182 |
Assert.assertTrue(actual.getAll("fq").contains(cqlTranslator.toLucene("(relfundinglevel1_id exact ec___::EC::SP1::VALUE1) or (relfundinglevel1_id exact ec___::EC::SP1::VALUE2)"))); |
|
183 |
|
|
184 |
//null refine |
|
185 |
actual = SolrResultSetOptionsUtil.extractQueryOptions(SearchServiceImpl.createEprQuery("(oaftype=result)", null, fieldQueries)); |
|
186 |
Assert.assertEquals(cqlTranslator.toLucene("(oaftype=result)"), actual.get("q")); |
|
187 |
Assert.assertEquals("__result", actual.get("fl")); |
|
188 |
Assert.assertEquals("AND", actual.get("q.op")); |
|
189 |
Assert.assertEquals("true", actual.get("shards.tolerant")); |
|
190 |
Assert.assertNull(actual.get("facet")); |
|
191 |
Assert.assertNull(actual.get("facet.mincount")); |
|
192 |
Assert.assertNull(actual.get("facet.threads")); |
|
193 |
Assert.assertNull(actual.get("facet.field")); |
|
194 |
Assert.assertTrue(actual.getAll("facet.field").isEmpty()); |
|
195 |
Assert.assertEquals(fieldQueries.size(), actual.getAll("fq").size()); |
|
196 |
Assert.assertTrue(actual.getAll("fq").contains(cqlTranslator.toLucene(("(relfunderid exact ec___::EC) and (relfunderid exact wt___::WT)")))); |
|
197 |
Assert.assertTrue(actual.getAll("fq").contains(cqlTranslator.toLucene("relfundinglevel0_id exact ec___::EC::SP1"))); |
|
198 |
Assert.assertTrue(actual.getAll("fq").contains(cqlTranslator.toLucene("(relfundinglevel1_id exact ec___::EC::SP1::VALUE1) or (relfundinglevel1_id exact ec___::EC::SP1::VALUE2)"))); |
|
199 |
|
|
200 |
//null refine, null fq |
|
201 |
actual = SolrResultSetOptionsUtil.extractQueryOptions(SearchServiceImpl.createEprQuery("(oaftype=result)", null, null)); |
|
202 |
Assert.assertEquals(cqlTranslator.toLucene("(oaftype=result)"), actual.get("q")); |
|
203 |
Assert.assertEquals("__result", actual.get("fl")); |
|
204 |
Assert.assertNull(actual.get("q.op")); |
|
205 |
Assert.assertEquals("true", actual.get("shards.tolerant")); |
|
206 |
Assert.assertNull(actual.get("facet")); |
|
207 |
Assert.assertNull(actual.get("facet.mincount")); |
|
208 |
Assert.assertNull(actual.get("facet.threads")); |
|
209 |
Assert.assertNull(actual.get("facet.field")); |
|
210 |
Assert.assertTrue(actual.getAll("facet.field").isEmpty()); |
|
211 |
Assert.assertNull(actual.get("fq")); |
|
212 |
Assert.assertTrue(actual.getAll("fq").isEmpty()); |
|
213 |
} |
|
214 |
|
|
215 |
@Test(expected=CQLParseException.class) |
|
216 |
public void extractQueryOptionsEmptyQuery() throws IOException, CQLParseException { |
|
217 |
NamedList<String> actual = SolrResultSetOptionsUtil.extractQueryOptions(SearchServiceImpl.createEprQuery(null, null, null)); |
|
218 |
} |
|
219 |
|
|
220 |
} |
modules/uoa-search/branches/newAPI/src/test/java/eu/dnetlib/data/search/transform/TransformerFactoryTest.java | ||
---|---|---|
1 | 1 |
package eu.dnetlib.data.search.transform; |
2 | 2 |
|
3 |
import java.io.IOException; |
|
4 |
|
|
3 |
import eu.dnetlib.data.search.transform.config.ConfigurationFactory; |
|
5 | 4 |
import org.apache.log4j.BasicConfigurator; |
6 | 5 |
import org.junit.BeforeClass; |
7 | 6 |
import org.junit.Test; |
8 | 7 |
|
9 |
import eu.dnetlib.data.search.transform.config.ConfigurationFactory;
|
|
8 |
import java.io.IOException;
|
|
10 | 9 |
|
10 |
|
|
11 | 11 |
public class TransformerFactoryTest { |
12 | 12 |
|
13 | 13 |
private static String registryAddress = "http://node1.t.openaire.research-infrastructures.eu:8280/is/services/isRegistry"; |
modules/uoa-search/branches/newAPI/src/main/java/eu/dnetlib/data/search/solr/SolrResultSetOld.java | ||
---|---|---|
1 | 1 |
package eu.dnetlib.data.search.solr; |
2 | 2 |
|
3 |
import com.google.gson.Gson; |
|
4 |
import eu.dnetlib.domain.EPR; |
|
5 |
import eu.dnetlib.functionality.index.cql.CqlTranslator; |
|
6 |
import eu.dnetlib.functionality.index.cql.CqlTranslatorImpl; |
|
7 |
import eu.dnetlib.functionality.index.cql.TranslatedQuery; |
|
8 |
import gr.uoa.di.driver.enabling.resultset.ResultSet; |
|
9 |
import org.apache.log4j.Logger; |
|
10 |
import org.apache.solr.client.solrj.SolrServerException; |
|
11 |
import org.apache.solr.client.solrj.impl.CloudSolrServer; |
|
12 |
import org.apache.solr.client.solrj.response.FacetField; |
|
13 |
import org.apache.solr.client.solrj.response.QueryResponse; |
|
14 |
import org.apache.solr.common.SolrDocumentList; |
|
15 |
import org.apache.solr.common.params.SolrParams; |
|
16 |
import org.apache.solr.common.util.NamedList; |
|
17 |
import org.z3950.zing.cql.CQLParseException; |
|
18 |
|
|
19 |
import java.io.IOException; |
|
20 |
import java.util.*; |
|
21 |
|
|
3 | 22 |
/** |
4 | 23 |
* Created by antleb on 2/4/14. |
5 | 24 |
*/ |
6 |
public class SolrResultSetOld { //implements ResultSet<String> {
|
|
7 |
/* |
|
25 |
public class SolrResultSetOld implements ResultSet<String> { |
|
26 |
|
|
8 | 27 |
private Logger logger = Logger.getLogger(getClass()); |
9 | 28 |
|
10 | 29 |
private EPR epr = null; |
... | ... | |
39 | 58 |
} */ |
40 | 59 |
|
41 | 60 |
//logger.debug("action "+ epr.getParameter("action")); |
42 |
/* TranslatedQuery translatedQuery = translator.getTranslatedQuery(queryParts[0].replace("query=",""));
|
|
61 |
TranslatedQuery translatedQuery = translator.getTranslatedQuery(queryParts[0].replace("query=","")); |
|
43 | 62 |
|
44 | 63 |
if (epr.getParameter("action").equals("lookup")) { |
45 | 64 |
queryOpts.add("q", translatedQuery.asLucene()); |
... | ... | |
200 | 219 |
|
201 | 220 |
return res; |
202 | 221 |
}*/ |
203 |
/* |
|
222 |
|
|
204 | 223 |
private List<String> getBrowseResults (int from, int to) { |
Also available in: Unified diff
Working on backwards compatibility. IMPORTANT: sygma and refine are not yet compatible