Skip to content
This repository has been archived by the owner on Jun 20, 2023. It is now read-only.

Commit

Permalink
Merge branch 'feature.simplesuggest' of https://github.com/ldez/Jest
Browse files Browse the repository at this point in the history
…into suggest
  • Loading branch information
kramer committed Apr 17, 2015
2 parents c0bf9f1 + 5ac86e4 commit b3d564b
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 0 deletions.
69 changes: 69 additions & 0 deletions jest-common/src/main/java/io/searchbox/core/Suggest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package io.searchbox.core;

import io.searchbox.action.AbstractAction;
import io.searchbox.action.AbstractMultiTypeActionBuilder;
import io.searchbox.client.JestResult;

import com.google.gson.Gson;

public class Suggest extends AbstractAction<JestResult> {

private final String query;

private Suggest(final Builder builder) {
super(builder);

this.query = builder.getQuery();
this.setURI(this.buildURI());
}

@Override
public JestResult createNewElasticSearchResult(final String json, final int statusCode, final String reasonPhrase, final Gson gson) {
return this.createNewElasticSearchResult(new JestResult(gson), json, statusCode, reasonPhrase, gson);
}

@Override
public String getRestMethodName() {
return "POST";
}

public String getIndex() {
return this.indexName;
}

public String getType() {
return this.typeName;
}

@Override
public Object getData(final Gson gson) {
return this.query;
}

@Override
protected String buildURI() {
final StringBuilder sb = new StringBuilder();
sb.append(super.buildURI()).append("/_suggest");
return sb.toString();
}

public static class Builder extends AbstractMultiTypeActionBuilder<Suggest, Builder> {

private final String query;

public Builder(final String query) {
this.query = query;
}

public final String getQuery() {
return this.query;
}

@Override
public Suggest build() {
return new Suggest(this);
}

}

}
67 changes: 67 additions & 0 deletions jest-common/src/test/java/io/searchbox/core/SuggestTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package io.searchbox.core;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import io.searchbox.action.Action;
import io.searchbox.client.JestResult;

import org.junit.Test;

public class SuggestTest {

@Test
public void getURIWithoutIndexAndType() {
Action<JestResult> suggest = new Suggest.Builder("").build();
assertEquals("_all/_suggest", suggest.getURI());
}

@Test
public void getURIWithOnlyOneIndex() {
Action<JestResult> suggest = new Suggest.Builder("").addIndex("twitter").build();
assertEquals("twitter/_suggest", suggest.getURI());
}

@Test
public void getURIWithOneIndexAndOneType() {
Action<JestResult> suggest = new Suggest.Builder("").addIndex("twitter").addType("tweet").build();
assertEquals("twitter/tweet/_suggest", suggest.getURI());
}

@Test
public void getURIWithOnlyOneType() {
Action<JestResult> suggest = new Suggest.Builder("").addType("tweet").build();
assertEquals("_all/tweet/_suggest", suggest.getURI());
}

@Test
public void getURIWithOnlyMultipleIndex() {
Action<JestResult> suggest = new Suggest.Builder("").addIndex("twitter").addIndex("suggestbox").build();
assertEquals("twitter%2Csuggestbox/_suggest", suggest.getURI());
}

@Test
public void getURIWithOnlyMultipleType() {
Action<JestResult> suggest = new Suggest.Builder("").addType("tweet").addType("jest").build();
assertEquals("_all/tweet%2Cjest/_suggest", suggest.getURI());
}

@Test
public void getURIWithMultipleIndexAndTypes() {
Action<JestResult> suggest = new Suggest.Builder("")
.addIndex("twitter")
.addIndex("suggestbox")
.addType("tweet")
.addType("jest")
.build();
assertEquals("twitter%2Csuggestbox/tweet%2Cjest/_suggest", suggest.getURI());
}

@Test
public void getDataSimple() {
String query = "{\"elementSuggestion\":{\"text\":\"courgette\",\"completion\":{\"field\":\"element.completion\",\"size\":10,\"fuzzy\":{}}}}";
Action<JestResult> suggest = new Suggest.Builder(query).build();

assertNotNull("data", suggest.getData(null));
assertEquals(query, suggest.getData(null).toString());
}
}

0 comments on commit b3d564b

Please sign in to comment.