Subversion Repositories SmartDukaan

Rev

Rev 22346 | Rev 25386 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 22346 Rev 25380
Line 1... Line 1...
1
package com.spice.profitmandi.common.solr;
1
package com.spice.profitmandi.common.solr;
2
 
2
 
3
import java.io.BufferedReader;
3
import java.util.ArrayList;
4
import java.io.IOException;
4
import java.util.HashMap;
5
import java.io.InputStream;
5
import java.util.List;
6
import java.io.InputStreamReader;
6
import java.util.Map;
7
 
-
 
8
import org.apache.http.client.methods.HttpGet;
-
 
9
import org.apache.http.impl.client.HttpClients;
-
 
10
import org.springframework.web.util.UriComponentsBuilder;
-
 
11
 
7
 
-
 
8
import javax.servlet.http.HttpServletRequest;
-
 
9
 
-
 
10
import org.apache.commons.lang3.StringUtils;
-
 
11
import org.apache.http.conn.HttpHostConnectException;
-
 
12
import org.json.JSONArray;
-
 
13
import org.json.JSONObject;
-
 
14
import org.springframework.stereotype.Service;
-
 
15
 
-
 
16
import com.spice.profitmandi.common.enumuration.SchemeType;
-
 
17
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
-
 
18
import com.spice.profitmandi.common.web.client.RestClient;
-
 
19
 
-
 
20
@Service
12
public class SolrService {
21
public class SolrService {
13
	public static void main(String[] args) throws Throwable {
22
	public String getContent( String queryTerm) throws Exception {
14
		//System.out.println(String.format("{!parent which=\"id:catalog*\"}tagId_i:%s", StringUtils.join(elements)));
-
 
15
		/*RestClient rc  = new RestClient(SchemeType.HTTP, "dtr", 8984);
23
		RestClient rc = new RestClient();
16
		Map<String, String> params = new HashMap<>();
24
		Map<String, String> params = new HashMap<>();
17
		Map<String, String> headers = new HashMap<>();
25
		List<String> mandatoryQ = new ArrayList<>();
-
 
26
		if (queryTerm != null && !queryTerm.equals("null")) {
-
 
27
			mandatoryQ.add(String.format("+(%s)", queryTerm));
-
 
28
		} else {
-
 
29
			queryTerm = null;
-
 
30
		}
18
		params.put("q", "{!parent which=\"id:catalog*\"}tagId_i:4");
31
		params.put("q", StringUtils.join(mandatoryQ, " "));
19
		params.put("fl", "*, [child parentFilter=id:catalog*]");
32
		params.put("fl", "*");
-
 
33
		if (queryTerm == null) {
20
		params.put("sort", "rank_i asc");
34
			params.put("sort", "create_s desc");
-
 
35
		}
21
		params.put("start", String.valueOf(0));
36
		params.put("start", String.valueOf(0));
22
		params.put("rows", String.valueOf(10));
37
		params.put("rows", String.valueOf(20));
23
		params.put("wt", "json");
38
		params.put("wt", "json");
24
		String response  =rc.get("solr/demo/select", params);
-
 
25
		JSONObject solrResponseJSONObj = new JSONObject(response);*/
-
 
26
 
-
 
27
		/*rc.e
-
 
28
        SolrQuery query = new SolrQuery();
-
 
29
        query.setQuery("{!parent which=\"id:catalog*\"}tagId_i:4");
-
 
30
        query.setFields("*, [child parentFilter=id:catalog*]");
-
 
31
        query.addSort("rank_i", ORDER.asc);
-
 
32
        query.setStart(0);
-
 
33
 
-
 
34
        QueryResponse response = client.query(query);
-
 
35
        System.out.println(response.toString());
-
 
36
        SolrDocumentList results = response.getResults();
-
 
37
        for (int i = 0; i < results.size(); ++i) {
-
 
38
            System.out.println(results.get(i));
-
 
39
        }*/
-
 
40
		
-
 
41
		UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl("http://dtr:8984/solr/demo/select");
-
 
42
			builder.queryParam("q", "+brand_s:Samsung +{!parent which=\"id:catalog*\"}");
-
 
43
			builder.queryParam("fl", "*, [child parentFilter=id:catalog*]");
-
 
44
			builder.queryParam("wt", "json");
-
 
45
		HttpGet request = new HttpGet(builder.build().encode().toUri());
-
 
46
		System.out.println(toString(HttpClients.createDefault().execute(request).getEntity().getContent()));
-
 
47
    }
-
 
48
	
-
 
49
	private static String toString(InputStream inputStream){
-
 
50
		BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
-
 
51
		StringBuilder responseString = new StringBuilder();
-
 
52
		String line = null;
39
		String response = null;
53
		try {
40
		try {
54
			while((line = reader.readLine()) != null){
41
			response = rc.get(SchemeType.HTTP, "dtr", 8984, "solr/demo/select", params);
55
				responseString.append(line);
-
 
56
			}
-
 
57
			inputStream.close();
-
 
58
		} catch (IOException e) {
42
		} catch (HttpHostConnectException e) {
59
			throw new RuntimeException();
43
			throw new ProfitMandiBusinessException("", "", "Could not connect to host");
60
		}
44
		}
-
 
45
		JSONObject solrResponseJSONObj = new JSONObject(response).getJSONObject("response");
-
 
46
		JSONArray docs = solrResponseJSONObj.getJSONArray("docs");
61
		return responseString.toString();
47
		return docs.toString();
62
	}
48
	}
-
 
49
 
63
}
50
}