Proper way to search quoted strings (i.e. multiple word phrase) from Java?

82 views
Skip to first unread message

Ed

unread,
Feb 27, 2012, 4:34:51 PM2/27/12
to Otter API to Topsy
Hi

I am getting 400 error when I search for multiple strings, i.e. a
phrase like "Barack Obama", in which I want to find all Tweets that
contain the exact phrase "Barack Obama". In other words I am
interested in only finding Tweets with the phrase "Barack Obama", not
ones with either "Barack" or "Obama".

It seems that adding escaped character double quotes around the
search phrase does not work from my Java code using the Otter Restful
API. I check the HTTP request string that I create the URL connect
with by cutting and pasting it directly from the Java output into a
browser and it works fine in the browser, but not in the URL Java
call ?.

Does the Otter API allow queries for multiple terms (i.e. phrases)?

Can you please paste an example HTTP string with a search query for a
phrase containing multiple strings?

Thanks !

Ed



Jason Toffaletti

unread,
Feb 27, 2012, 5:11:29 PM2/27/12
to Otter API to Topsy
On Feb 27, 1:34 pm, Ed <macker...@gmail.com> wrote:
> Hi
>
> I am getting 400 error when I search for multiple strings, i.e. a
> phrase like "Barack Obama", in which I want to find all Tweets that
> contain the exact phrase "Barack Obama".  In other words I am
> interested in only finding Tweets with the phrase "Barack Obama", not
> ones with either "Barack" or "Obama".
>
>  It seems that adding escaped character double quotes around the
> search phrase does not work from my Java code using the Otter Restful
> API.  I check the HTTP request string that I create the URL connect
> with by cutting and pasting it directly from the Java output into a
> browser and it works fine in the browser, but not in the URL Java
> call ?.

This is not something specific to Otter, it is just the way URIs must
be encoded to be valid.

See http://en.wikipedia.org/wiki/Percent-encoding for more informaton.

A web browser is not a good test because it is designed to take human
input and encode it for you.

I'm not very familiar with Java, but this looks promising:

http://docs.oracle.com/javase/1.5.0/docs/api/java/net/URLEncoder.html

> Does the Otter API allow queries for multiple terms (i.e. phrases)?

Yes

> Can you please paste an example HTTP string with a search query for a
> phrase containing multiple strings?

This is what you'll want to end up with:

$ curl -i http://otter.topsy.com/search.json?q=%22Barack%20Obama%22

> Thanks !
>
> Ed

Ed

unread,
Feb 29, 2012, 9:24:08 AM2/29/12
to Otter API to Topsy
Thanks Jason for the fast reply and useful information.

Yes, you are correct about the URI. I was creating a URL in Java
instead of a URI. The URI class has more support for encoding.

Ed

On Feb 27, 3:11 pm, Jason Toffaletti <ja...@topsy.com> wrote:
> On Feb 27, 1:34 pm, Ed <macker...@gmail.com> wrote:
>
> > Hi
>
> > I am getting 400 error when I search for multiple strings, i.e. a
> > phrase like "Barack Obama", in which I want to find all Tweets that
> > contain the exact phrase "Barack Obama".  In other words I am
> > interested in only finding Tweets with the phrase "Barack Obama", not
> > ones with either "Barack" or "Obama".
>
> >  It seems that adding escaped character double quotes around the
> > search phrase does not work from my Java code using the Otter Restful
> > API.  I check the HTTP request string that I create the URL connect
> > with by cutting and pasting it directly from the Java output into a
> > browser and it works fine in the browser, but not in the URL Java
> > call ?.
>
> This is not something specific to Otter, it is just the way URIs must
> be encoded to be valid.
>
> Seehttp://en.wikipedia.org/wiki/Percent-encodingfor more informaton.
Reply all
Reply to author
Forward
0 new messages