Project

General

Profile

« Previous | Next » 

Revision 44602

Added methods for single solr query for search and browse. Size does not trigger extra solr query. Refine uses field queries. Pending APIs for different entities - only the genarla resources api is currently enabled. Pending handling format for both XML and JSON. All these related to #1432 and #2479

View differences:

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.assertEquals("AND", 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.assertEquals("AND", 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.assertEquals("AND", 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.assertEquals("AND", 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/solr/SolrResultSetFactory.java
33 33

  
34 34
                clients.put(epr.getAddress(), solrClient);
35 35
            }
36
            return new SolrResultSet(epr, solrClient);
36 37

  
37
            return new SolrResultSet(epr, solrClient);
38 38
        } catch (Exception e) {
39 39
            logger.error("Error creating solr client", e);
40 40
        }
modules/uoa-search/branches/newAPI/src/main/java/eu/dnetlib/data/search/solr/SolrIndexClient.java
48 48
        return epr;
49 49
    }
50 50

  
51
    public void newMethod(){}
52

  
53
    public EPR getBrowsingStatistics2(String query, String index, String mdFormatId, String layoutId) throws IndexServiceException {
54
        EPR epr = new EPR();
55

  
56
        epr.setAddress(solrServerUrl);
57
        epr.setParameter("action", "browse");
58
        epr.setParameter("id", index);
59
        epr.setParameter("query", query);
60
        epr.setParameter("mdformat", mdFormatId);
61
        epr.setParameter("layout", layoutId);
62
        epr.setParameter("interpretation", this.interpretation);
63

  
64
        return epr;
65
    }
66

  
51 67
    @Override
52 68
    public Hint suggestiveSearch(String s, String s2, String s3, String s4, String s5) throws IndexServiceException {
53 69
        throw new UnsupportedOperationException();
modules/uoa-search/branches/newAPI/src/main/java/eu/dnetlib/data/search/solr/SolrResultSetOld.java
1
package eu.dnetlib.data.search.solr;
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

  
22
/**
23
 * Created by antleb on 2/4/14.
24
 */
25
public class SolrResultSetOld implements ResultSet<String> {
26

  
27
    private Logger logger = Logger.getLogger(getClass());
28

  
29
    private EPR epr = null;
30
    private CloudSolrServer solrClient = null;
31

  
32
    private NamedList<String> queryOpts = new NamedList<String>();
33
    int size = -1;
34

  
35

  
36
    public SolrResultSetOld(EPR epr, CloudSolrServer solrClient) throws IOException, CQLParseException {
37
        this.epr = epr;
38
        this.solrClient = solrClient;
39

  
40
        this.queryOpts = extractQueryOptions(epr);
41

  
42
        String layout = epr.getParameter("layout");
43
        String mdformat = epr.getParameter("mdformat");
44
        String interpretation = epr.getParameter("interpretation");
45

  
46
        solrClient.setDefaultCollection(mdformat + "-" + layout + "-" + interpretation);
47
    }
48

  
49
    public NamedList<String> extractQueryOptions(EPR epr) throws CQLParseException, IOException {
50
        CqlTranslator translator = new CqlTranslatorImpl();
51
        String cqlQuery = epr.getParameter("query");
52
        logger.debug("epr query param: " + cqlQuery);
53
        String[] queryParts = cqlQuery.split("&groupby=");
54

  
55
        /*logger.debug("Got query " + cqlQuery);
56
        for (int i = 0; i < queryParts.length; i++) {
57
            logger.debug("queryParts [" + i + "] = '" + queryParts[i] + "'");
58
        } */
59

  
60
        //logger.debug("action "+ epr.getParameter("action"));
61
        TranslatedQuery translatedQuery = translator.getTranslatedQuery(queryParts[0].replace("query=",""));
62

  
63
        if (epr.getParameter("action").equals("lookup")) {
64
            queryOpts.add("q", translatedQuery.asLucene());
65
            queryOpts.add("fl", "__result");
66

  
67
            if (translatedQuery.getOptions() != null && translatedQuery.getOptions().getSort()!= null  ) {
68
                queryOpts.add("sort", translatedQuery.getOptions().getSort().getField() + " " + translatedQuery.getOptions().getSort().getMode());
69
            }
70

  
71
        } else if (epr.getParameter("action").equals("browse")) {
72
            logger.debug("Browse query: " + translatedQuery.asLucene());
73
            queryOpts.add("q", translatedQuery.asLucene());
74
            queryOpts.add("facet", "true");
75
            queryOpts.add("facet.mincount", "1");
76
            queryOpts.add("rows", "0");
77
            queryOpts.add("q.op", "AND");
78

  
79
            if (queryParts.length > 1) {
80
                String[] facetParts = queryParts[1].split("&fq=");
81
                queryOpts.add("facet.threads", facetParts[0].split(",").length + "");
82

  
83
                for (String field:facetParts[0].split(",")) {
84
                    logger.debug(field);
85
                    queryOpts.add("facet.field", field);
86
                }
87

  
88
                if (facetParts.length > 1) {
89
                    for(String facetPart:facetParts[1].split(",")) {
90
                        queryOpts.add("fq", facetPart);
91
                    }
92
                }
93
            }
94

  
95
            logger.debug(queryOpts.getAll("fq"));
96
            logger.debug(queryOpts.getAll("facet.field"));
97
        }
98

  
99
        queryOpts.add("shards.tolerant","true");
100

  
101
        return queryOpts;
102
    }
103

  
104
    @Override
105
    public boolean isOpen() {
106
        return true;
107
    }
108

  
109
    @Override
110
    public boolean isAlive() {
111
        return true;
112
    }
113

  
114
    @Override
115
    public void close() {
116
        //solrClient.shutdown();
117
    }
118

  
119
    @Override
120
    public int size() {
121
        if (size == -1) {
122
            try {
123
                size = getSize();
124
            } catch (SolrServerException sse) {
125
                logger.error("Fail to get size", sse);
126
            }
127
        }
128

  
129
        return size;
130
    }
131

  
132
    private int getSize() throws SolrServerException {
133
        //logger.debug("Query opts" + queryOpts);
134
        QueryResponse rsp = null;
135

  
136
        synchronized (solrClient) {
137
            rsp = solrClient.query(SolrParams.toSolrParams(queryOpts));
138
        }
139

  
140
        //logger.debug(queryOpts);
141

  
142
        if (epr.getParameter("action").equals("lookup")) {
143
            return (int) rsp.getResults().getNumFound();
144
        } else if (epr.getParameter("action").equals("browse")) {
145
            int max = -12;
146

  
147
            for (FacetField field:rsp.getFacetFields()) {
148
                if (field.getValueCount() > max)
149
                    max = field.getValueCount();
150
            }
151

  
152
            return max;
153
        }
154

  
155
        return 0;
156
    }
157

  
158
    @Override
159
    public List<String> getElements(int from, int to) {
160
        return get(from, to);
161
    }
162

  
163
    @Override
164
    public List<String> get(int from, int to) {
165

  
166
        logger.debug("Getting records from " + from + "  to " + to);
167

  
168
        if ("lookup".equals(epr.getParameter("action")))
169
            return getDocumentResults(from, to);
170

  
171
        else if ("browse".equals(epr.getParameter("action"))) {
172
            return getBrowseResults(from, to);
173
        }
174

  
175
        return null;
176
    }
177

  
178
    List<FacetField> facetFields = null;
179
/*
180
    private List<String> getBrowseResults(int from, int to) {
181
        List<String> res = new ArrayList<String>();
182

  
183

  
184
        if (facetFields == null) {
185
            try {
186
                QueryResponse rsp = null;
187

  
188
                synchronized (solrClient) {
189
                    rsp = solrClient.query(SolrParams.toSolrParams(queryOpts));
190
                }
191

  
192
                facetFields = rsp.getFacetFields();
193
            } catch (SolrServerException e) {
194
                e.printStackTrace();
195
            }
196

  
197

  
198
            for (int i = from - 1; i < to; i++) {
199
                StringBuilder sb = new StringBuilder();
200

  
201
                sb.append("<row>");
202

  
203
                for (FacetField field:facetFields) {
204
                    if (field.getValueCount() > i) {
205
                        sb.append("<groupresult field=\"").append(field.getName()).append("\">");
206
                        sb.append("<count>").append(field.getValues().get(i).getCount()).append("</count>");
207
                        sb.append("<value>").append(StringEscapeUtils.escapeXml(field.getValues().get(i).getName())).append("</value>");
208
                        sb.append("</groupresult>");
209
                    }
210
                }
211

  
212
                sb.append("</row>");
213

  
214
            //    logger.debug("row: " + sb.toString());
215

  
216
                res.add(sb.toString());
217
            }
218
        }
219

  
220
        return res;
221
    }*/
222

  
223
    private List<String> getBrowseResults (int from, int to) {
224
        List<String> res = new ArrayList<String>();
225

  
226
        HashMap<String, List<String>> map = new HashMap<String, List<String>>();
227
        if (facetFields == null) {
228
            try {
229
                QueryResponse rsp = null;
230

  
231
                synchronized (solrClient) {
232
                    rsp = solrClient.query(SolrParams.toSolrParams(queryOpts));
233
                }
234

  
235
                facetFields = rsp.getFacetFields();
236
            } catch (SolrServerException e) {
237
                e.printStackTrace();
238
            }
239

  
240
            logger.debug("HELLO from " + from + " to " + to);
241

  
242
            for (int i = from - 1; i < to; i++) {
243
                for (FacetField field : facetFields) {
244
                    if (field.getValueCount() > i) {
245
                        BrowseField bf = new BrowseField();
246
                        bf.setId(field.getValues().get(i).getName());
247
                        bf.setName(field.getValues().get(i).getName());
248
                        bf.setCount(field.getValues().get(i).getCount() + "");
249
                        if (map.get(field.getName()) == null) {
250
                            map.put(field.getName(), new ArrayList<String>());
251
                        }
252

  
253
                        map.get(field.getName()).add(new Gson().toJson(bf));
254
                    }
255
                }
256
            }
257

  
258
            for (Map.Entry<String, List<String>> facetEntry : map.entrySet()) {
259
                StringBuilder builder = new StringBuilder();
260
                builder.append("\"" + facetEntry.getKey() + "\"" + " : ");
261
                builder.append(facetEntry.getValue());
262
                res.add(builder.toString());
263
            }
264
        }
265

  
266
        return res;
267
    }
268

  
269
    private List<String> getDocumentResults(int from, int to) {
270
        try {
271
            QueryResponse rsp = null;
272
            NamedList<String> extraOpts = new NamedList<String>();
273

  
274
            extraOpts.add("start", (from - 1) + "");
275
            extraOpts.add("rows", (to - from) + 1 + "");
276
            extraOpts.addAll(queryOpts);
277

  
278
            synchronized (solrClient) {
279
                rsp = solrClient.query(SolrParams.toSolrParams(extraOpts));
280
            }
281

  
282
            SolrDocumentList docs = rsp.getResults();
283

  
284
            logger.debug("time: " + rsp.getElapsedTime());
285
            logger.debug("found: " + docs.getNumFound());
286

  
287
            List<String> res = new ArrayList<String>();
288

  
289
            for (int i = 0; i < (to - from) + 1; i++) {
290
                String result = ((ArrayList<String>) docs.get(i).get("__result")).get(0);
291

  
292
                res.add(result);
293
            }
294

  
295
            return res;
296

  
297
        } catch (SolrServerException sse) {
298
            logger.error("Fail to get document results.", sse);
299
        }
300

  
301
        return Collections.EMPTY_LIST;
302
    }
303

  
304
    public static void main(String[] args) throws IOException, CQLParseException, SolrServerException {
305
        CloudSolrServer solrClient = new CloudSolrServer("beta.solr.openaire.eu:9983");
306
        NamedList<String> queryOpts = new NamedList<String>();
307

  
308
        //   queryOpts.add("q", new CqlTranslatorImpl().getTranslatedQuery("oaftype exact project").asLucene());
309
        NamedList<String> extraOpts = new NamedList<String>();
310

  
311
        //extraOpts.add("start", "1");
312
        // extraOpts.add("rows", "10");
313
        // extraOpts.addAll(queryOpts);
314

  
315
        queryOpts.add("facet", "true");
316
        TranslatedQuery translatedQuery = new CqlTranslatorImpl().getTranslatedQuery("oaftype=result sortBy resultdateofacceptance/sort.descending");
317

  
318
        //   queryOpts.add("q", "oaftype=project");
319
        queryOpts.add("facet", "true");
320
        queryOpts.add("facet.mincount", "1");
321
        queryOpts.add("fq", "popularity");
322

  
323

  
324
//        queryOpts.put("fq", new CqlTranslatorImpl().getTranslatedQuery("").asLucene());
325
        // queryOpts.add("facet.field", "contextid");
326
        //  queryOpts.add("facet.field", "contextname");
327
        //  queryOpts.add("facet.mincount", "1");
328
        //  queryOpts.add("facet.threads", "10");
329
        // System.out.println(translatedQuery.getOptions().getSort().getMode());
330
        // System.out.println(translatedQuery.getOptions().getSort().getField());
331

  
332
        //queryOpts.add("sort", translatedQuery.getOptions().getSort().getField() + " " + translatedQuery.getOptions().getSort().getMode() );
333

  
334
        solrClient.setDefaultCollection("DMF-index-openaire");
335

  
336
        QueryResponse resp = null;
337
        synchronized (solrClient) {
338
            resp = solrClient.query(SolrParams.toSolrParams(extraOpts));
339
        }
340
        System.out.println("time: " + resp.getElapsedTime());
341
        //System.out.println("results: " + resp.getResults());
342

  
343
/*      System.out.println(resp.getFacetField("contextname").getValueCount());
344

  
345
        for (FacetField.Count count:resp.getFacetField("contextname").getValues())
346
            System.out.println(count.getName() + " : " +  count.getCount());
347

  
348

  
349
        int max = -12;
350

  
351
        for (FacetField field:resp.getFacetFields()) {
352
            if (field.getValueCount() > max)
353
                max = field.getValueCount();
354

  
355
        }
356

  
357
        System.out.println("max: " + max);
358
*/
359
    }
360

  
361
    @Override
362
    public EPR getEpr() {
363
        return epr;
364
    }
365
}
366

  
367
class BrowseField2 {
368
    String name;
369

  
370
    public String getName() {
371
        return name;
372
    }
373

  
374
    public void setName(String name) {
375
        this.name = name;
376
    }
377

  
378
    public String getId() {
379
        return id;
380
    }
381

  
382
    public void setId(String id) {
383
        this.id = id;
384
    }
385

  
386
    public String getCount() {
387
        return count;
388
    }
389

  
390
    public void setCount(String count) {
391
        this.count = count;
392
    }
393

  
394
    String id;
395
    String count;
396

  
397

  
398
}
modules/uoa-search/branches/newAPI/src/main/java/eu/dnetlib/data/search/solr/SolrResultSet.java
1 1
package eu.dnetlib.data.search.solr;
2 2

  
3 3
import com.google.gson.Gson;
4
import eu.dnetlib.data.search.utils.SolrResultSetOptionsUtil;
5
import eu.dnetlib.data.search.utils.SolrResultsFormatter;
4 6
import eu.dnetlib.domain.EPR;
5
import eu.dnetlib.functionality.index.cql.CqlTranslator;
6 7
import eu.dnetlib.functionality.index.cql.CqlTranslatorImpl;
7 8
import eu.dnetlib.functionality.index.cql.TranslatedQuery;
8 9
import gr.uoa.di.driver.enabling.resultset.ResultSet;
10
import org.apache.commons.lang.StringEscapeUtils;
9 11
import org.apache.log4j.Logger;
10 12
import org.apache.solr.client.solrj.SolrServerException;
11 13
import org.apache.solr.client.solrj.impl.CloudSolrServer;
......
18 20

  
19 21
import java.io.IOException;
20 22
import java.util.ArrayList;
21
import java.util.Collections;
22 23
import java.util.HashMap;
23 24
import java.util.List;
25
import java.util.Map;
24 26

  
25 27
/**
26 28
 * Created by antleb on 2/4/14.
......
33 35
    private CloudSolrServer solrClient = null;
34 36

  
35 37
    private NamedList<String> queryOpts = new NamedList<String>();
36
    int size = -1;
38
    long size = -1;
37 39

  
38 40

  
39 41
    public SolrResultSet(EPR epr, CloudSolrServer solrClient) throws IOException, CQLParseException {
40 42
        this.epr = epr;
41 43
        this.solrClient = solrClient;
44
        this.queryOpts = SolrResultSetOptionsUtil.extractQueryOptions(epr.getParameter("query"));
42 45

  
43
        CqlTranslator translator = new CqlTranslatorImpl();
44
        String cqlQuery = epr.getParameter("query");
45
        //logger.debug("epr query param: " + cqlQuery);
46
        String[] queryParts = cqlQuery.split("&groupby=");
47

  
48
        //logger.debug("Got query " + cqlQuery);
49
        //for (int i = 0; i < queryParts.length; i++) {
50
        //    logger.debug("queryParts [" + i + "] = '" + queryParts[i] +"'");
51
        //}
52

  
53
        TranslatedQuery translatedQuery = translator.getTranslatedQuery(queryParts[0].replace("query=",""));
54

  
55
        if (epr.getParameter("action").equals("lookup")) {
56
            queryOpts.add("q", translatedQuery.asLucene());
57
            queryOpts.add("fl", "__result");
58

  
59
          if (translatedQuery.getOptions() != null && translatedQuery.getOptions().getSort()!= null  ) {
60
                queryOpts.add("sort", translatedQuery.getOptions().getSort().getField() + " " + translatedQuery.getOptions().getSort().getMode());
61
          }
62

  
63
        } else if (epr.getParameter("action").equals("browse")) {
64
            logger.debug("Browse query: " + translatedQuery.asLucene());
65

  
66
            queryOpts.add("q", translatedQuery.asLucene());
67
            queryOpts.add("facet", "true");
68
            queryOpts.add("facet.mincount", "1");
69

  
70
            if (queryParts.length > 1) {
71

  
72
                queryOpts.add("facet.threads", queryParts[1].split(",").length + "");
73

  
74
                for (String field:queryParts[1].split(","))
75
                    queryOpts.add("facet.field", field);
76
            }
77
        }
78

  
79

  
80
        queryOpts.add("shards.tolerant","true");
81

  
82 46
        String layout = epr.getParameter("layout");
83 47
        String mdformat = epr.getParameter("mdformat");
84 48
        String interpretation = epr.getParameter("interpretation");
......
86 50
        solrClient.setDefaultCollection(mdformat + "-" + layout + "-" + interpretation);
87 51
    }
88 52

  
53

  
54

  
89 55
    @Override
90 56
    public boolean isOpen() {
91 57
        return true;
......
102 68
    }
103 69

  
104 70
    @Override
71
    @Deprecated
105 72
    public int size() {
106 73
        if (size == -1) {
107 74
            try {
......
111 78
            }
112 79
        }
113 80

  
114
        return size;
81
        return (int)size;
115 82
    }
116 83

  
84
    @Deprecated
117 85
    private int getSize() throws SolrServerException {
118 86
        //logger.debug("Query opts" + queryOpts);
119 87
        QueryResponse rsp = null;
......
140 108
        return 0;
141 109
    }
142 110

  
111

  
143 112
    @Override
113
    @Deprecated
144 114
    public List<String> getElements(int from, int to) {
145 115
        return get(from, to);
146 116
    }
147 117

  
118
    List<FacetField> facetFields = null;
119

  
148 120
    @Override
121
    @Deprecated
149 122
    public List<String> get(int from, int to) {
123
        List<String> res = new ArrayList<String>();
150 124

  
151
        logger.debug("Getting records from " + from + "  to " + to);
125
        QueryResponse rsp = null;
152 126

  
153
        if ("lookup".equals(epr.getParameter("action")))
154
            return getDocumentResults(from, to);
127
        HashMap<String, List<String>> map = new HashMap<String, List<String>>();
155 128

  
156
        else if ("browse".equals(epr.getParameter("action"))) {
157
            return getBrowseResults(from, to);
158
        }
129
        logger.debug("from: " + from);
130
        logger.debug("to: " + to);
159 131

  
160
        return null;
161
    }
162 132

  
163
    List<FacetField> facetFields = null;
164
/*
165
    private List<String> getBrowseResults(int from, int to) {
166
        List<String> res = new ArrayList<String>();
133
        queryOpts.add("start", from+1 + "");
134
        queryOpts.add("rows", to + 1+"");
167 135

  
136
        try {
137
            rsp = solrClient.query(SolrParams.toSolrParams(queryOpts));
138
            facetFields = rsp.getFacetFields();
139
            SolrDocumentList docs = rsp.getResults();
168 140

  
169
        if (facetFields == null) {
170
            try {
171
                QueryResponse rsp = null;
141
            if (facetFields!=null && !facetFields.isEmpty()) {
142
                for (int i = from - 1; i < to; i++) {
143
                    for (FacetField field : facetFields) {
144
                        if (field.getValueCount() > i) {
145
                            BrowseField bf = new BrowseField();
146
                            bf.setId(field.getValues().get(i).getName());
147
                            bf.setName(field.getValues().get(i).getName());
148
                            bf.setCount(field.getValues().get(i).getCount() + "");
149
                            if (map.get(field.getName()) == null) {
150
                                map.put(field.getName(), new ArrayList<String>());
151
                            }
172 152

  
173
                synchronized (solrClient) {
174
                    rsp = solrClient.query(SolrParams.toSolrParams(queryOpts));
153
                            map.get(field.getName()).add(new Gson().toJson(bf));
154
                        }
155
                    }
175 156
                }
176 157

  
177
                facetFields = rsp.getFacetFields();
178
            } catch (SolrServerException e) {
179
                e.printStackTrace();
158
                for (Map.Entry<String, List<String>> facetEntry : map.entrySet()) {
159
                    StringBuilder builder = new StringBuilder();
160
                    builder.append("\"" + facetEntry.getKey() + "\"" + " : ");
161
                    builder.append(facetEntry.getValue());
162
                    res.add(builder.toString());
163
                }
180 164
            }
181 165

  
166
            logger.debug("time: " + rsp.getElapsedTime());
167
            logger.debug("found: " + docs.getNumFound());
168
            logger.debug("docs: " + docs.size());
182 169

  
183
            for (int i = from - 1; i < to; i++) {
184
                StringBuilder sb = new StringBuilder();
170
            for (int i = 0; i < docs.size(); i++) {
171
                String result = ((ArrayList<String>) docs.get(i).get("__result")).get(0);
172
                res.add(result);
173
            }
185 174

  
186
                sb.append("<row>");
175
            return res;
187 176

  
188
                for (FacetField field:facetFields) {
189
                    if (field.getValueCount() > i) {
190
                        sb.append("<groupresult field=\"").append(field.getName()).append("\">");
191
                        sb.append("<count>").append(field.getValues().get(i).getCount()).append("</count>");
192
                        sb.append("<value>").append(StringEscapeUtils.escapeXml(field.getValues().get(i).getName())).append("</value>");
193
                        sb.append("</groupresult>");
194
                    }
195
                }
196

  
197
                sb.append("</row>");
198

  
199
            //    logger.debug("row: " + sb.toString());
200

  
201
                res.add(sb.toString());
202
            }
177
        } catch (SolrServerException sse) {
178
            logger.error(sse);
203 179
        }
204 180

  
205
        return res;
206
    }*/
181
        return null;
182
    }
207 183

  
208
    private List<String> getBrowseResults (int from, int to) {
209
        List<String> res = new ArrayList<String>();
184
    public Map<String,List<String>> newGet(int from, int to, String format) {
185
        List<String> refineSolrResults = new ArrayList<String>();
186
        List<String> searchSolrResults = new ArrayList<String>();
210 187

  
188
        QueryResponse rsp = null;
211 189
        HashMap<String, List<String>> map = new HashMap<String, List<String>>();
212
        if (facetFields == null) {
213
            try {
214
                QueryResponse rsp = null;
215 190

  
216
                synchronized (solrClient) {
217
                    rsp = solrClient.query(SolrParams.toSolrParams(queryOpts));
218
                }
191
        logger.debug("from: " + from);
192
        logger.debug("to: " + to);
219 193

  
220
                facetFields = rsp.getFacetFields();
221
            } catch (SolrServerException e) {
222
                e.printStackTrace();
223
            }
194
        queryOpts.add("start", from+1 + "");
195
        queryOpts.add("rows", to +"");
224 196

  
225
            logger.debug("HELLO from " + from + " to " + to);
197
        try {
198
            rsp = solrClient.query(SolrParams.toSolrParams(queryOpts));
199
            facetFields = rsp.getFacetFields();
200
            
201
            SolrDocumentList docs = rsp.getResults();
226 202

  
227
            for (int i = from - 1; i < to; i++) {
228
                for (FacetField field : facetFields) {
229
                    if (field.getValueCount() > i) {
230
                        BrowseField bf = new BrowseField();
231
                        bf.setId(field.getValues().get(i).getName());
232
                        bf.setName(field.getValues().get(i).getName());
233
                        bf.setCount(field.getValues().get(i).getCount() + "");
234
                        if (map.get(field.getName()) == null) {
203
            this.size = docs.getNumFound();
204

  
205
            if (facetFields!=null && !facetFields.isEmpty()) {
206
                if (format != null && format.equalsIgnoreCase("json")) {
207
                     for (FacetField field : facetFields) {
235 208
                            map.put(field.getName(), new ArrayList<String>());
209
                            BrowseField bf = null;
210
                            logger.debug("field " + field.getValues());
211
                            logger.debug("field value " + field.getValueCount());
212
                        for (int i = 0; i < field.getValueCount(); i++) {
213
                            bf = new BrowseField();
214
                            bf.setId(field.getValues().get(i).getName());
215
                            bf.setName(field.getValues().get(i).getName());
216
                            bf.setCount(field.getValues().get(i).getCount() + "");
217
                            map.get(field.getName()).add(new Gson().toJson(bf));
236 218
                        }
237 219

  
238
                        map.get(field.getName()).add(new Gson().toJson(bf));
239 220
                    }
240
                }
241
            }
242 221

  
243
            for (String index : map.keySet()) {
244
                StringBuilder builder = new StringBuilder();
245
                builder.append("\"" + index + "\"" + " : ");
246
                builder.append(map.get(index));
247
                res.add(builder.toString());
248
            }
249
        }
222
                    StringBuilder builder = null;
250 223

  
251
        return res;
252
    }
224
                    for (Map.Entry<String, List<String>> facetEntry : map.entrySet()) {
225
                        builder = new StringBuilder();
226
                        builder.append("\"" + facetEntry.getKey() + "\"" + " : ");
227
                        builder.append(facetEntry.getValue());
228
                        refineSolrResults.add(builder.toString());
229
                    }
253 230

  
254
    private List<String> getDocumentResults(int from, int to) {
255
        try {
256
            QueryResponse rsp = null;
257
            NamedList<String> extraOpts = new NamedList<String>();
231
                } else { //the old implementation & xml as default
232
                    for (int i = from - 1; i < facetFields.size(); i++) {
233
                        StringBuilder sb = new StringBuilder();
234
                        sb.append("<row>");
235
                        for (FacetField field : facetFields) {
236
                            if (field.getValueCount() > i) {
237
                                sb.append("<groupresult field=\"").append(field.getName()).append("\">");
238
                                sb.append("<count>").append(field.getValues().get(i).getCount()).append("</count>");
239
                                sb.append("<value>").append(StringEscapeUtils.escapeXml(field.getValues().get(i).getName())).append("</value>");
240
                                sb.append("</groupresult>");
241
                            }
242
                        }
243
                        sb.append("</row>");
244
                        refineSolrResults.add(sb.toString());
245
                    }
246
                }
247
            }
258 248

  
259
            extraOpts.add("start", (from - 1) + "");
260
            extraOpts.add("rows", (to - from) + 1 + "");
261
            extraOpts.addAll(queryOpts);
262

  
263
            synchronized (solrClient) {
264
                rsp = solrClient.query(SolrParams.toSolrParams(extraOpts));
249
            for (int i = 0; i < docs.size(); i++) {
250
                String result = ((ArrayList<String>) docs.get(i).get("__result")).get(0);
251
                if (format != null && format.equalsIgnoreCase("json")) {
252
                    searchSolrResults.add(SolrResultsFormatter.xml2Json(result));
253
                } else { // default xml
254
                    searchSolrResults.add(result);
255
                }
265 256
            }
266 257

  
267
            SolrDocumentList docs = rsp.getResults();
258
            Map<String,List<String>> response = new HashMap<String, List<String>>();
268 259

  
269
            logger.debug("time: " + rsp.getElapsedTime());
270
            logger.debug("found: " + docs.getNumFound());
260
            logger.debug("refine results " + refineSolrResults);
261
            logger.debug("search results " + searchSolrResults);
271 262

  
272
            List<String> res = new ArrayList<String>();
263
            response.put("refine",refineSolrResults);
264
            response.put("search", searchSolrResults);
273 265

  
274
            for (int i = 0; i < (to - from) + 1; i++) {
275
                String result = ((ArrayList<String>) docs.get(i).get("__result")).get(0);
266
            return response;
276 267

  
277
                res.add(result);
278
            }
279

  
280
            return res;
281

  
282 268
        } catch (SolrServerException sse) {
283
            logger.error("Fail to get document results.", sse);
269
            logger.error("Error calling solr", sse);
284 270
        }
285 271

  
286
        return Collections.EMPTY_LIST;
272
        return null;
287 273
    }
288 274

  
275

  
289 276
    public static void main(String[] args) throws IOException, CQLParseException, SolrServerException {
290 277
        CloudSolrServer solrClient = new CloudSolrServer("beta.solr.openaire.eu:9983");
291 278
        NamedList<String> queryOpts = new NamedList<String>();
......
306 293
        queryOpts.add("fq", "popularity");
307 294

  
308 295

  
296

  
309 297
//        queryOpts.put("fq", new CqlTranslatorImpl().getTranslatedQuery("").asLucene());
310 298
       // queryOpts.add("facet.field", "contextid");
311 299
       //  queryOpts.add("facet.field", "contextname");
modules/uoa-search/branches/newAPI/src/main/java/eu/dnetlib/data/search/app/SearchServiceImpl.java
8 8
import eu.dnetlib.common.rmi.UnimplementedException;
9 9
import eu.dnetlib.data.search.app.plan.FieldRewriteRule;
10 10
import eu.dnetlib.data.search.app.plan.QueryRewriteRule;
11
import eu.dnetlib.data.search.solr.SolrResultSet;
11 12
import eu.dnetlib.data.search.transform.Transformer;
12 13
import eu.dnetlib.data.search.transform.TransformerException;
13 14
import eu.dnetlib.data.search.transform.config.SearchRegistry;
......
19 20
import eu.dnetlib.domain.data.SearchResult;
20 21
import eu.dnetlib.domain.data.SuggestiveResult;
21 22
import eu.dnetlib.domain.enabling.Notification;
23
import eu.dnetlib.functionality.index.cql.CqlTranslator;
24
import eu.dnetlib.functionality.index.cql.CqlTranslatorImpl;
22 25
import gr.uoa.di.driver.app.DriverServiceImpl;
23 26
import gr.uoa.di.driver.enabling.issn.NotificationListener;
24 27
import gr.uoa.di.driver.enabling.resultset.ResultSet;
......
750 753
    public void setLookUpServiceServiceLocator(ServiceLocator<ISLookUpService> lookUpServiceServiceLocator) {
751 754
        this.lookUpServiceServiceLocator = lookUpServiceServiceLocator;
752 755
    }
756

  
757

  
758
    public SearchResult newSearch (String text, String locale, List<String> refinefields, List<String> fieldQueries, int from, int to, String format) throws SearchServiceException {
759
        logger.info("Received NEW search request for: '" + text + "' and fields " + fieldQueries);
760
        IndexService index = getIndexLocator().getService();
761

  
762
        EPR epr = null;
763
        ResultSet<String> rs = null;
764

  
765
        List<String> browseResults = null;
766
        List<String> searchResults = null;
767

  
768
        String query = rewrite(text);
769

  
770
        try {
771
            query = new CQLParser().parse(query).toCQL();
772
            String eprQuery = createEprQuery(query, refinefields, fieldQueries);
773

  
774
            epr = index.getBrowsingStatistics(eprQuery, "all", mdFormat, indexLayout);
775

  
776
            if (epr == null) {
777
                throw new SearchServiceException("Something really strange happened there! Index returned null result set id.");
778
            }
779

  
780
            //get the locale
781
            String correctLocale = getCorrectLocale(locale);
782
            StringTokenizer tokenizer = new StringTokenizer(correctLocale, "_");
783
            Locale requestLocale = new Locale(tokenizer.nextToken(), tokenizer.nextToken());
784

  
785
            rs = rsFactory.createResultSet(epr);
786

  
787
            long startTime = System.nanoTime();
788
            Map<String, List<String>> list = ((SolrResultSet)rs).newGet(from, to, format);
789
            long estimatedTime = System.nanoTime() - startTime;
790
            logger.debug("Actual time until SolrResultSet response for " + eprQuery + " took " + estimatedTime/1000000 +  " milliseconds");
791

  
792
            searchResults = list.get("search");
793
            browseResults = list.get("refine");
794

  
795
        } catch (CQLParseException cqle) {
796
            logger.error("Bad CQL query: " + query + ".", cqle);
797
            throw new SearchServiceException("Error calling index.", cqle);
798

  
799
        } catch (IndexServiceException ise) {
800
            logger.error("Error getting refine results.", ise);
801
            throw new SearchServiceException("Error getting refine results.", ise);
802

  
803
        } catch (IOException ioe) {
804
            logger.error("IO Exception from CQL Paser", ioe);
805
            throw new SearchServiceException("Error calling index.", ioe);
806
        }
807

  
808
        logger.info("Returned results for NEW search query '" + query + "' and fields " + fieldQueries);
809
        return new SearchResult(query, Locale.getDefault().toString(), rs.size(), 1, 10, searchResults, browseResults, refinefields);
810
    }
811

  
812

  
813

  
814
    public static String createEprQuery(String query, List<String> refineFields, List<String> fieldQueries) {
815
        StringBuffer queryBuffer = new StringBuffer();
816
        queryBuffer.append("query=");
817

  
818
        StringBuffer facetsBuffer = new StringBuffer();
819
        facetsBuffer.append("&groupby=");
820

  
821
        StringBuffer fqBuffer = new StringBuffer();
822
        fqBuffer.append("&fq=");
823

  
824
        if (query != null) { //TODO consider exception?
825
            queryBuffer.append(query);
826
        }
827

  
828
        if(refineFields != null) {
829
            for (Iterator<String> iterator = refineFields.iterator(); iterator.hasNext(); ) {
830
                facetsBuffer.append(iterator.next());
831
                if (iterator.hasNext()) {
832
                    facetsBuffer.append(",");
833
                }
834
            }
835
        }
836

  
837
        if(fieldQueries != null) {
838
            for (Iterator<String> iterator = fieldQueries.iterator(); iterator.hasNext(); ) {
839
                fqBuffer.append(iterator.next());
840
                if (iterator.hasNext()) {
841
                    fqBuffer.append(",");
842
                }
843
            }
844
        }
845

  
846
        return queryBuffer.append(facetsBuffer.toString()).append(fqBuffer.toString()).toString();
847
    }
848

  
849
    /*
850
    public static String createEprQuery(List<FieldQuery> fieldQueries, String query) {
851
        StringBuffer queryBuffer = new StringBuffer();
852
        queryBuffer.append("query=").append(query);
853

  
854
        StringBuffer facetsBuffer = new StringBuffer();
855
        facetsBuffer.append("&groupby=");
856

  
857
        StringBuffer fqBuffer = new StringBuffer();
858
        fqBuffer.append("&fq=");
859

  
860
        for (Iterator<FieldQuery> iter = fieldQueries.iterator(); iter.hasNext();) {
861
            FieldQuery field = (FieldQuery) iter.next();
862
            facetsBuffer.append(field.getIndexField());
863
            if (!field.toSolrQuery().isEmpty()) {
864
                fqBuffer.append(field.toSolrQuery()).append(",");
865
            }
866
            if (iter.hasNext()) {
867
                facetsBuffer.append(",");
868
            }
869
        }
870

  
871
        return queryBuffer.append(facetsBuffer.toString()).append(fqBuffer.toString()).toString();
872
    }*/
873

  
874

  
875
    public static void main(String[] args) throws IOException, CQLParseException {
876
        CqlTranslator translator = new CqlTranslatorImpl();
877

  
878
        System.out.println(translator.getTranslatedQuery("(relfunderid = ec__________::EC)and(relfunderid = fct_________::FCT)").getQuery().toLucene());
879
    }
880

  
753 881
}
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("q.op", "AND");
28
        queryOpts.add("shards.tolerant","true");
29

  
30
        if (translatedQuery.getOptions() != null && translatedQuery.getOptions().getSort()!= null  ) {
31
            queryOpts.add("sort", translatedQuery.getOptions().getSort().getField() + " " + translatedQuery.getOptions().getSort().getMode());
32
        }
33

  
34
        if (queryParts.length > 1) {
35
            String[] facetParts = queryParts[1].split("&fq=");
36

  
37
            String[] refineParts = null;
38
            String[] facetQueries = null;
39

  
40
            if (facetParts != null && facetParts.length > 0) {
41
                if (!facetParts[0].isEmpty()) {
42
                    refineParts = facetParts[0].split(",");
43
                }
44

  
45
                if (facetParts.length > 1){
46
                    facetQueries = facetParts[1].split(",");
47
                }
48

  
49
                if (facetParts[0].isEmpty() && facetParts.length > 1) {
50
                    facetQueries = facetParts[1].split(",");
51
                }
52
            }
53

  
54
            if (refineParts != null && refineParts.length > 0) {
55
                queryOpts.add("facet", "true");
56
                queryOpts.add("facet.mincount", "1");
57
                queryOpts.add("facet.threads", refineParts.length + "");
58
                for (String field : refineParts) {
59
                    queryOpts.add("facet.field", field);
60
                }
61
            }
62

  
63
            if (facetQueries!=null && facetQueries.length > 0) {
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/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/vocabulary/VocabularyManagerImpl.java
44 44
			vocabularyLoaderMap.put(vocabularyName, localVocabularyLoader);	
45 45
			vocabularyMap.put(vocabularyName, config.getLocalVocabularyMap().get(vocabularyName));
46 46
		}
47
		
48
		for (String v: vocabularyMap.keySet()) {
49
			logger.debug("vocabulary names " + vocabularyMap.get(v).getName());
47

  
48
		if(logger.isDebugEnabled()) {
49
			for (Map.Entry<String, Vocabulary> vocabularyEntry : vocabularyMap.entrySet()) {
50
				logger.debug("vocabulary names " + vocabularyEntry.getValue().getName());
51
			}
50 52
		}
51 53
	}
52 54
	
modules/uoa-search/branches/newAPI/src/main/java/eu/dnetlib/data/search/utils/vocabulary/VocabularyLoader.java
4 4

  
5 5
import java.util.Locale;
6 6

  
7
import org.apache.log4j.Logger;
8

  
9 7
abstract class VocabularyLoader {
10 8
	
11 9
	//the local path where vocabularies are saved
......
13 11
	
14 12
	private static final String EMPTY_FILE = "<?xml version=\"1.0\"?><RESOURCE_PROFILE></RESOURCE_PROFILE>";
15 13

  
16
	private static Logger logger = Logger.getLogger(VocabularyLoader.class);  
14
	//private static final Logger logger = Logger.getLogger(VocabularyLoader.class);
17 15
	
18 16
	public eu.dnetlib.domain.enabling.Vocabulary loadVocabulary(Vocabulary vocabulary, Locale locale) throws Exception {
19 17
		String xml = getVocabularyXml(vocabulary, locale);
modules/uoa-search/branches/newAPI/src/main/java/eu/dnetlib/data/search/resources/FieldQuery.java
1
package eu.dnetlib.data.search.resources;
2

  
3
/**
4
 * Created by kiatrop on 10/11/2016.
5
 */
6
public class FieldQuery {
7
    String indexField;
8
    String operator; //TODO change this
9
    String[] values;
10

  
11
    public String getIndexField() {
12
        return indexField;
13
    }
14

  
15
    public void setIndexField(String indexField) {
16
        this.indexField = indexField;
17
    }
18

  
19
    public String getOperator() {
20
        return operator;
21
    }
22

  
23
    public void setOperator(String operator) {
24
        this.operator = operator;
25
    }
26

  
27
    public String[] getValues() {
28
        return values;
29
    }
30

  
31
    public void setValues(String[] values) {
32
        this.values = values;
33
    }
34

  
35
    public FieldQuery(String indexField, String operator, String[] values) {
36
        this.indexField = indexField;
37
        this.operator = operator;
38
        this.values = values;
39
    }
40

  
41
    public FieldQuery(String indexField, String[] values) {
42
        this.indexField = indexField;
43
        this.operator = "AND";
44
        this.values = values;
45
    }
46

  
47
    public String toSolrQuery() {
48
        StringBuilder stringBuilder = new StringBuilder();
49

  
50
        if ( values!=null && values.length>0 ) {
51
            for(int i=0; i< values.length; i++) {
52
                stringBuilder.append(indexField).append(":").append("\"").append(values[i]).append("\"");
53
                if( i<values.length-1) {
54
                    stringBuilder.append("+").append(operator).append("+");
55
                }
56
            }
57
        }
58

  
59
        return stringBuilder.toString();
60
    }
61

  
62
    @Override
63
    public String toString() {
64
        if(getValues()!=null)
65
            return indexField + "(" + operator + "): " + getValues();
66
        return indexField;
67
    }
68
}
modules/uoa-search/branches/newAPI/src/main/java/eu/dnetlib/data/search/web/utils/RequestResponseHandler.java
82 82

  
83 83
        }
84 84

  
85
        public Map<String, String> geIndexParameterMap() {
86
            switch (this) {
87
                case PUBLICATION:
88
                    return CQLQueryGeneration.resultIndexParameterMap;
89
                default:
90
                    throw new IllegalArgumentException();
91
            }
92
        }
93

  
85 94
        public Map<String, String> getParameterIndexMap() {
86 95
            switch (this) {
87 96
                case PUBLICATION:
modules/uoa-search/branches/newAPI/src/main/java/eu/dnetlib/data/search/web/api/SearchApiService.java
2 2

  
3 3
import eu.dnetlib.api.data.SearchServiceException;
4 4
import eu.dnetlib.data.search.app.SearchServiceImpl;
5
import eu.dnetlib.data.search.utils.SolrResultsFormatter;
5 6
import eu.dnetlib.data.search.web.utils.RequestResponseHandler;
6 7
import eu.dnetlib.domain.data.SearchResult;
8
import eu.dnetlib.functionality.index.cql.CqlTranslator;
9
import eu.dnetlib.functionality.index.cql.CqlTranslatorImpl;
7 10
import org.apache.log4j.Logger;
8
import org.json.JSONObject;
9 11
import org.json.XML;
10 12
import org.springframework.beans.factory.annotation.Autowired;
11 13
import org.springframework.stereotype.Component;
14
import org.z3950.zing.cql.CQLParseException;
12 15

  
13 16
import javax.servlet.http.HttpServletRequest;
14 17
import javax.ws.rs.*;
15 18
import javax.ws.rs.core.Context;
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff