Get the document from solr using specific attribute value - java

I am new to solr. I am using solr which has data.Now I want to get the data of a particular document by searching into the solr. I have an attribute value with which I want to search in the solr. I am doing this by java. I have tried all the solution from web but not working.
public void searchQuery(String valuetobesearched) throws UnsupportedEncodingException {
server = new SolarConnectionClass().getSolrInstance("URL of my solr");
SolrQuery sQueryParams = new SolrQuery();
//String queryString = "*:*";
sQueryParams.setQuery("name:"+URLEncoder.encode(valuetobesearched));
//sQueryParams.addFilterQuery("name:"+URLEncoder.encode(valuetobesearched));
/*sQueryParams.setStart(0);
sQueryParams.setRows(5000);*/
//sQueryParams.set("defType", "edismax");
//sQueryParams.set
QueryResponse response = null;
try {
System.out.println(sQueryParams);
response = server.query(sQueryParams);
} catch (SolrServerException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
SolrDocumentList results = response.getResults();
System.out.println(results.size());
/*if(results.size() > 0) {
for (int i = 0; i < results.size(); ++i) {
SolrDocument sortedDocument = results.get(0);
System.out.println(sortedDocument.getFieldValue("PQR"));
}
}
*/
}
How can do this ?

You don't have encode parameters when using SolrQuery class.
Just set the query in this way the client will transparently do the "dirty" job:
sQueryParams.setQuery("name:" + valuetobesearched);
Again, you can see the total number of results found using:
response.getResults().getNumFound()
The size of response.getResults() depends from the setRows parameter (that in your post is commented).
In order to understand how Solr query works, I suggest to make your tests using your browser and then transpose that requests to SolrJ.
For example the url that comes out from of your post is:
http://localhost:8983/solr/collection1/select?q=name:hello

Related

Java JSON Object Request - How to receive the JZON parts I need (android studio)

due to the fact that I am a newbie, I am sorry in advance for making you angry ;-)
I am currently working on a project and today I reached a milestone. My API-Request works.
I am using this URL:
https://en.wikipedia.org/w/api.php?format=json&action=query&prop=extracts&exintro&explaintext&redirects=1&titles=turkey
My problem is , that for my app I only need the "Extract" part of this JSON (at the moment i receive everything). How can I go on?
Try this one:
try {
JSONObject jsonResult = new JSONObject(yourline);
String loudScreaming = jsonResult.getJSONObject("query").getJSONObject("pages").getJSONObject("11125639").getString("extract");
System.out.println(loudScreaming);
} catch (JSONException e) {
e.printStackTrace();
}
try this one:
public String getExtract(String json) {
try {
JSONObject object = new JSONObject(json).getJSONObject("query").getJSONObject("pages");
object = object.getJSONObject(object.names().get(0).toString());
return object.getString("extract");
} catch (Exception e) { return ""; }
}
in different wikis, pages object might have different page ids. But every JSON result has query, pages, and extract.
so we use the first item in pages object and return extract text from there.

Jsoup Google Search Results

I am attempting to parse the HTML of google's search results to grab the title of each result. This is done through android in a private nested class shown below:
private class WebScraper extends AsyncTask<String, Void, String> {
public WebScraper() {}
#Override
protected String doInBackground(String... urls) {
Document doc;
try {
doc = Jsoup.connect(urls[0]).get();
} catch (IOException e) {
System.out.println("Failed to open document");
return "";
}
Elements results = doc.getElementsByClass("rc");
int count = 0;
for (Element lmnt : results) {
System.out.println(count++);
System.out.println(lmnt.text());
}
System.out.println("Count is : " + count);
String key = "test";
//noinspection Since15
SearchActivity.this.songs.put(key, SearchActivity.this.songs.getOrDefault(key, 0) + 1);
// return requested
return "";
}
}
an example url I am trying to parse: http://www.google.com/#q=i+might+site:genius.com
For some reason, when i run the above code, my count is printed as 0, thus no elements are being stored in results. Any help is much appreciated! P.S. docs is definitely initialized and the HTML page is loading properly
This code will search a word like "Apple" in google and fetch all links from results and display their title and url. It can search upto 500 words in a day after that google detect it and stop giving results.
search="Apple"; //your word to be search on google
String userAgent = "ExampleBot 1.0 (+http://example.com/bot)";
Elements links=null;
try {
links = Jsoup.connect(google +
URLEncoder.encode(search,charset)).
userAgent(userAgent).get().select(".g>.r>a");
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
for (Element link : links) {
String title = link.text();
String url = link.absUrl("href"); // Google returns URLs in
format "http://www.google.com/url?q=<url>&sa=U&ei=<someKey>".
try {
url = URLDecoder.decode(url.substring(url.indexOf('=') +
1, url.indexOf('&')), "UTF-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (!url.startsWith("http")) {
continue; // Ads/news/etc.
}
System.out.println("Title: " + title);
System.out.println("URL: " + url);
}
If you check source code of the Google's page, you will notice that it does not contain any text data which is shown normally in the browser - there is only a bunch of javascript code. That means that Google outputs all the search results dynamically.
Jsoup will fetch that javascript code and it will not find any html code with "rc" classes, that's why you get zero count in your code sample.
Consider using Google's public search API instead of direct parsing of its html pages: https://developers.google.com/custom-search/.
I completely agree with Matvey Sidorenko but for using the google public search API, you need to have the Google Api key. But the problem is that google limits 100 searches per api key, exceeding which, it stops working and it gets reset in 24 hours.
Recently i was working on a project where we needed to get the google search result links for different queries provided by the user, so as to overcome this issue of API limit, i made my own API that searches directly on google/ncr and gives you the result link.
Free Google Search API-
http://freegoogleapi.azurewebsites.net/ OR http://google.bittque.com
I used HTML-UNIT library for making this API.
You can use my API or you can use the HTML UNIT Library for achieving what you need.

How to connect to ElasticSearch with Java transport client?

I am following the ElasticSearch documentation on Java Client. I have started ElasticSearch and I can interact with it with the Rest API. I want to use the Java Client and so far I have a main like this:
public class TestElastic {
public static void main(String[] args) {
try{
TransportClient client = TransportClient.builder().build()
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300));
JSONObject place = new JSONObject();
place.put("name", "AAAAA");
IndexResponse response = client.prepareIndex("my_database", "places", "1")
.setSource(place)
.get();
System.out.println(response.toString());
// Index name
String _index = response.getIndex();
System.out.println(_index);
// Type name
String _type = response.getType();
System.out.println(_type);
// Document ID (generated or not)
String _id = response.getId();
System.out.println(_id);
// Version (if it's the first time you index this document, you will get: 1)
long _version = response.getVersion();
System.out.println(_version);
// isCreated() is true if the document is a new one, false if it has been updated
boolean created = response.isCreated();
System.out.println(created);
client.close();
}catch (Exception ex){
ex.printStackTrace();
}
}
}
In the Java logs I can see that there is a connection with 127.0.0.1:9300. But after the "prepare index" command I do not see any error and nothing is printed(I have some system out commands). In the ElasticSearch logs is also nothing relative. When I create an index with the Rest API I can see this in the logs.
Ok, as #Val mentioned I forgot to print the errors. The problem was that JSONObject is not the format that ElasticSearch wants. Map and HashMap are acceptable.

Problems Creating New Initiative with Rally JAVA Rest API 2.0

I am using the following code snippet to try to create an Initiative in Rally (the values I am using for _ref I obtained while debugging).
public void createInitiative() {
CreateRequest request = null;
CreateResponse response = null;
JsonObject jo = new JsonObject();
String wsRef = "/workspace/11785043049";
String prjRef = "/project/11785043139";
jo.addProperty("Workspace", wsRef);
jo.addProperty("Project", prjRef);
jo.addProperty("Name","api_create_initiative_01");
jo.addProperty("Owner","/user/17085226946");
jo.addProperty("_ref", "/portfolioItem/initiative");
jo.addProperty("_type", "portfolioItem/initiative");
request = new CreateRequest("Initiative", jo);
try {
response = api.create(request);
JsonElement je = response.getObject();
System.out.println(je.getAsString());
} catch (IOException e) {
e.printStackTrace();
}
}
The response is :
{"CreateResult": {"_rallyAPIMajor": "2", "_rallyAPIMinor": "0", "Errors": ["Not authorized to perform action: Invalid key"], "Warnings": ["It is no longer necessary to append \".js\" to WSAPI resources."]}}
I have no idea which "key" is invalid.
Also, not sure if I should be using "Initiative" or "PortfolioItem/Initiative" both fail
The security token needed to be append to the url as specified in the docs when you find them.
I had cannibalized code from the rallyapi test program for api setup and the security token was not appending when performing a create. Also my code snippet above had to be changed to create("PortfolioInitiative",jo) and the property "_type" was not needed

How to know whether Comment is replied by some user or not of youtube in Java

I am using Youtube api version 2 and using Java language to develop my project.
I want to fetch comment and replies associated with that comment. I do not know how to do it. I have tried all the possibilities that's written in the youtube development guide but not succeed.
Can any one give me code example how to achieve this?
Now I'm using this code to fetch the replies of that comment(hardcoded in the code)
int startIndex = 1, maxResult = 50, j = 0;
url = "http://gdata.youtube.com/feeds/api/videos/6afPT1hbMLk?client=comment+research?start-index%3D1";
YouTubeService service = new YouTubeService(appName);
YouTubeQuery query = new YouTubeQuery(new URL(url));
try {
CommentFeed commentFeed = service.query(query, CommentFeed.class);
} catch (IOException | ServiceException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
but it give me following exception...
com.google.gdata.util.ParseException: [Line 1, Column 268] Invalid root element, expected (namespace uri:local name) of (http://www.w3.org/2005/Atom:feed), found (http://www.w3.org/2005/Atom:entry
at com.google.gdata.util.XmlParser.throwParseException(XmlParser.java:730)
at com.google.gdata.util.XmlParser.parse(XmlParser.java:693)
at com.google.gdata.util.XmlParser.parse(XmlParser.java:576)
at com.google.gdata.data.BaseFeed.parseAtom(BaseFeed.java:867)
at com.google.gdata.wireformats.input.AtomDataParser.parse(AtomDataParser.java:68)
at com.google.gdata.wireformats.input.AtomDataParser.parse(AtomDataParser.java:39)

Categories

Resources