Skip to content
This repository has been archived by the owner on Apr 15, 2020. It is now read-only.

Commit

Permalink
Fixed search query creation
Browse files Browse the repository at this point in the history
  • Loading branch information
tahseen committed Apr 24, 2014
1 parent 025b027 commit b3f11ae
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 49 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ target/*
.classpath
.project
bin/
/target
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,13 @@ Below is a simple example of searching documents.
```java
AmazonCloudSearchQuery query = new AmazonCloudSearchQuery();
query.query = "Dining Tables";
query.returnFields = "title,sku_no,description";
query.start = 20;
query.size = 20;
query.addExpression("sort_expr","(0.3*popularity)+(0.7*_score)");
query.sort("sort_expr", "desc");
query.queryParser = "simple";
query.start = 0;
query.size = 16;
query.setDefaultOperator("or");
query.setFields("sku_no^11", "title^10", "description^9", "features^8", "specification^8", "categories^7");
query.addExpression("sort_expr", "(0.3*popularity)+(0.7*_score)");
query.addSort("sort_expr", "desc");

AmazonCloudSearchResult result = client.search(query);
```
Expand Down
19 changes: 19 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,23 @@
<version>4.2</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
<version>1.5</version>
<executions>
<execution>
<id>sign-artifacts</id>
<phase>verify</phase>
<goals>
<goal>sign</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ public AmazonCloudSearchClient(AWSCredentialsProvider awsCredentialsProvider, Cl
public void addDocument(AmazonCloudSearchAddRequest document) throws AmazonCloudSearchRequestException, AmazonCloudSearchInternalServerException, JSONException {
JSONArray docs = new JSONArray();
docs.put(toJSON(document));
updateDocumentRequest(docs.toString());
}

/**
Expand All @@ -200,6 +201,7 @@ public void addDocuments(List<AmazonCloudSearchAddRequest> documents) throws JSO
for(AmazonCloudSearchAddRequest doc : documents) {
docs.put(toJSON(doc));
}
updateDocumentRequest(docs.toString());
}


Expand Down Expand Up @@ -374,7 +376,7 @@ public AmazonCloudSearchResult search(AmazonCloudSearchQuery query) throws Illeg
AmazonCloudSearchResult result = null;

try {
Response response = Request.Post("https://" + getSearchEndpoint() + "/2013-01-01/search?" + query.build())
Response response = Request.Get("https://" + getSearchEndpoint() + "/2013-01-01/search?" + query.build())
.useExpectContinue()
.version(HttpVersion.HTTP_1_1)
.addHeader("Accept", ContentType.APPLICATION_JSON.getMimeType())
Expand Down Expand Up @@ -415,25 +417,26 @@ private AmazonCloudSearchResult fromJSON(String responseBody) throws JSONExcepti
if(hits != null) {
result.found = hits.getInt("found");
result.start = hits.getInt("start");

JSONArray hitArray = root.getJSONArray("hit");
if(hitArray != null) {
for(int i = 0; i < hitArray.length(); i++) {
JSONObject row = hitArray.getJSONObject(i);
Hit hit = new Hit();
hit.id = row.getString("id");
JSONObject fields = row.getJSONObject("fields");
String[] names = JSONObject.getNames(fields);
for(String name : names) {
if(hit.fields == null) {
hit.fields = new HashMap<String, String>();
if(result.found > 0) {
JSONArray hitArray = hits.getJSONArray("hit");
if(hitArray != null) {
for(int i = 0; i < hitArray.length(); i++) {
JSONObject row = hitArray.getJSONObject(i);
Hit hit = new Hit();
hit.id = row.getString("id");
JSONObject fields = row.getJSONObject("fields");
String[] names = JSONObject.getNames(fields);
for(String name : names) {
if(hit.fields == null) {
hit.fields = new HashMap<String, String>();
}
hit.fields.put(name, fields.getString(name));
}
hit.fields.put(name, fields.getString(name));
}
if(result.hits == null) {
result.hits = new ArrayList<Hit>();
if(result.hits == null) {
result.hits = new ArrayList<Hit>();
}
result.hits.add(hit);
}
result.hits.add(hit);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package aws.services.cloudsearchv2.search;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -313,19 +315,19 @@ public void setReturnFields(String ... returnFields) {
this.returnFields = builder.toString();
}

public String build() {
public String build() throws UnsupportedEncodingException {
StringBuilder builder = new StringBuilder();

if(cursor != null) {
builder.append("cursor").append("=").append(cursor);
builder.append("cursor").append("=").append(URLEncoder.encode(cursor, "UTF-8"));
}

if(expressions.size() > 0) {
for(Map.Entry<String, String> entry : expressions.entrySet()) {
if(builder.length() > 0) {
builder.append("&");
}
builder.append(entry.getKey()).append("=").append(entry.getValue());
builder.append("expr").append(".").append(entry.getKey()).append("=").append(URLEncoder.encode(entry.getValue(), "UTF-8"));
}
}

Expand All @@ -334,17 +336,23 @@ public String build() {
if(builder.length() > 0) {
builder.append("&");
}
builder.append("facet").append(".").append(facet.field).append("=").append("{");
builder.append("facet").append(".").append(facet.field).append("=");

StringBuilder value = new StringBuilder();
value.append("{");
if(facet.sort != null) {
builder.append("sort").append(":").append("\"").append(facet.sort).append("\"");
value.append("sort").append(":").append("\"").append(facet.sort).append("\"");
}
if(facet.buckets != null) {
builder.append("buckets").append(":").append(facet.buckets);
value.append("buckets").append(":").append(facet.buckets);
}
if(facet.size != null) {
builder.append("size").append(":").append(facet.size);
value.append("size").append(":").append(facet.size);
}
builder.append("}");

value.append("}");

builder.append(URLEncoder.encode(value.toString(), "UTF-8"));
}
}

Expand All @@ -361,96 +369,114 @@ public String build() {
if(builder.length() > 0) {
builder.append("&");
}
builder.append("highlight").append(".").append(highlight.field).append("=").append("{");
builder.append("highlight").append(".").append(highlight.field).append("=");

StringBuilder value = new StringBuilder();
value.append("{");
if(highlight.format != null) {
builder.append("format").append(":").append("'").append(highlight.format).append("'");
value.append("format").append(":").append("'").append(highlight.format).append("'");
}
if(highlight.maxPhrases != null) {
builder.append("max_phrases").append(":").append(highlight.maxPhrases);
value.append("max_phrases").append(":").append(highlight.maxPhrases);
}
if(highlight.preTag != null) {
builder.append("pre_tag").append(":").append("'").append(highlight.preTag).append("'");
value.append("pre_tag").append(":").append("'").append(highlight.preTag).append("'");
}
if(highlight.postTag != null) {
builder.append("post_tag").append(":").append("'").append(highlight.postTag).append("'");
value.append("post_tag").append(":").append("'").append(highlight.postTag).append("'");
}
builder.append("}");
value.append("}");

builder.append(URLEncoder.encode(value.toString(), "UTF-8"));

}
}

if(partial != null) {
if(builder.length() > 0) {
builder.append("&");
}
builder.append("partial").append("=").append(partial);
builder.append("partial").append("=").append(URLEncoder.encode(partial + "", "UTF-8"));
}

if(pretty != null) {
if(builder.length() > 0) {
builder.append("&");
}
builder.append("pretty").append("=").append(pretty);
builder.append("pretty").append("=").append(URLEncoder.encode(pretty + "", "UTF-8"));
}

if(query != null) {
if(builder.length() > 0) {
builder.append("&");
}
builder.append("q").append("=").append(query);
builder.append("q").append("=").append(URLEncoder.encode(query, "UTF-8"));
}

if(queryOptions.size() > 0) {
if(builder.length() > 0) {
builder.append("&");
}
builder.append("q").append(".").append("options").append("=").append("{");

builder.append("q").append(".").append("options").append("=");

StringBuilder value = new StringBuilder();
value.append("{");
for(Map.Entry<String, String> entry : queryOptions.entrySet()) {
builder.append(entry.getKey()).append(":").append(entry.getValue());
value.append(entry.getKey()).append(":").append("'").append(entry.getValue()).append("'");
}
builder.append("}");
value.append("}");

builder.append(URLEncoder.encode(value.toString(), "UTF-8"));
}

if(queryParser != null) {
if(builder.length() > 0) {
builder.append("&");
}
builder.append("q.parser").append("=").append(queryParser);
builder.append("q.parser").append("=").append(URLEncoder.encode(queryParser, "UTF-8"));
}

if(returnFields != null) {
if(builder.length() > 0) {
builder.append("&");
}
builder.append("return").append("=").append(returnFields);
builder.append("return").append("=").append(URLEncoder.encode(returnFields, "UTF-8"));
}

if(size != null) {
if(builder.length() > 0) {
builder.append("&");
}
builder.append("size").append("=").append(size);
builder.append("size").append("=").append(URLEncoder.encode(size + "", "UTF-8"));
}

if(sort.size() > 0) {
if(builder.length() > 0) {
builder.append("&");
}

builder.append("sort").append("=");

StringBuilder value = new StringBuilder();
int i = 0;
for(Map.Entry<String, String> entry : sort.entrySet()) {
builder.append(entry.getKey()).append(" ").append(entry.getValue());
value.append(entry.getKey()).append(" ").append(entry.getValue());
if(i != sort.size() - 1) {
builder.append(",");
value.append(",");

}
}

builder.append(URLEncoder.encode(value.toString(), "UTF-8"));
}

if(start != null) {
if(builder.length() > 0) {

builder.append("&");
}
builder.append("start").append("=").append(start);
builder.append("start").append("=").append(URLEncoder.encode(start + "", "UTF-8"));
}

return builder.toString();
Expand Down
Binary file modified target/amazon-cloudsearch-client-1.1.jar
Binary file not shown.

0 comments on commit b3f11ae

Please sign in to comment.