use of org.elasticsearch.action.index.IndexRequestBuilder in project elasticsearch by elastic.
the class SimpleSortIT method testSimpleSorts.
public void testSimpleSorts() throws Exception {
Random random = random();
assertAcked(prepareCreate("test").addMapping("type1", jsonBuilder().startObject().startObject("type1").startObject("properties").startObject("str_value").field("type", "keyword").endObject().startObject("boolean_value").field("type", "boolean").endObject().startObject("byte_value").field("type", "byte").endObject().startObject("short_value").field("type", "short").endObject().startObject("integer_value").field("type", "integer").endObject().startObject("long_value").field("type", "long").endObject().startObject("float_value").field("type", "float").endObject().startObject("double_value").field("type", "double").endObject().endObject().endObject().endObject()));
ensureGreen();
List<IndexRequestBuilder> builders = new ArrayList<>();
for (int i = 0; i < 10; i++) {
builders.add(client().prepareIndex("test", "type1", Integer.toString(i)).setSource(jsonBuilder().startObject().field("str_value", new String(new char[] { (char) (97 + i), (char) (97 + i) })).field("boolean_value", true).field("byte_value", i).field("short_value", i).field("integer_value", i).field("long_value", i).field("float_value", 0.1 * i).field("double_value", 0.1 * i).endObject()));
}
Collections.shuffle(builders, random);
for (IndexRequestBuilder builder : builders) {
builder.execute().actionGet();
if (random.nextBoolean()) {
if (random.nextInt(5) != 0) {
refresh();
} else {
client().admin().indices().prepareFlush().get();
}
}
}
refresh();
// STRING script
int size = 1 + random.nextInt(10);
Script script = new Script(ScriptType.INLINE, CustomScriptPlugin.NAME, "doc['str_value'].value", Collections.emptyMap());
SearchResponse searchResponse = client().prepareSearch().setQuery(matchAllQuery()).setSize(size).addSort(new ScriptSortBuilder(script, ScriptSortType.STRING)).get();
assertHitCount(searchResponse, 10);
assertThat(searchResponse.getHits().getHits().length, equalTo(size));
for (int i = 0; i < size; i++) {
SearchHit searchHit = searchResponse.getHits().getAt(i);
assertThat(searchHit.getId(), equalTo(Integer.toString(i)));
String expected = new String(new char[] { (char) (97 + i), (char) (97 + i) });
assertThat(searchHit.getSortValues()[0].toString(), equalTo(expected));
}
size = 1 + random.nextInt(10);
searchResponse = client().prepareSearch().setQuery(matchAllQuery()).setSize(size).addSort("str_value", SortOrder.DESC).get();
assertHitCount(searchResponse, 10);
assertThat(searchResponse.getHits().getHits().length, equalTo(size));
for (int i = 0; i < size; i++) {
SearchHit searchHit = searchResponse.getHits().getAt(i);
assertThat(searchHit.getId(), equalTo(Integer.toString(9 - i)));
String expected = new String(new char[] { (char) (97 + (9 - i)), (char) (97 + (9 - i)) });
assertThat(searchHit.getSortValues()[0].toString(), equalTo(expected));
}
assertThat(searchResponse.toString(), not(containsString("error")));
assertNoFailures(searchResponse);
}
use of org.elasticsearch.action.index.IndexRequestBuilder in project elasticsearch by elastic.
the class CompletionSuggestSearchIT method testTextAndGlobalText.
/**
* test that suggestion works if prefix is either provided via {@link CompletionSuggestionBuilder#text(String)} or
* {@link SuggestBuilder#setGlobalText(String)}
*/
public void testTextAndGlobalText() throws Exception {
final CompletionMappingBuilder mapping = new CompletionMappingBuilder();
createIndexAndMapping(mapping);
int numDocs = 10;
List<IndexRequestBuilder> indexRequestBuilders = new ArrayList<>();
for (int i = 1; i <= numDocs; i++) {
indexRequestBuilders.add(client().prepareIndex(INDEX, TYPE, "" + i).setSource(jsonBuilder().startObject().startObject(FIELD).field("input", "suggestion" + i).field("weight", i).endObject().endObject()));
}
indexRandom(true, indexRequestBuilders);
CompletionSuggestionBuilder noText = SuggestBuilders.completionSuggestion(FIELD);
SearchResponse searchResponse = client().prepareSearch(INDEX).suggest(new SuggestBuilder().addSuggestion("foo", noText).setGlobalText("sugg")).execute().actionGet();
assertSuggestions(searchResponse, "foo", "suggestion10", "suggestion9", "suggestion8", "suggestion7", "suggestion6");
CompletionSuggestionBuilder withText = SuggestBuilders.completionSuggestion(FIELD).text("sugg");
searchResponse = client().prepareSearch(INDEX).suggest(new SuggestBuilder().addSuggestion("foo", withText)).execute().actionGet();
assertSuggestions(searchResponse, "foo", "suggestion10", "suggestion9", "suggestion8", "suggestion7", "suggestion6");
// test that suggestion text takes precedence over global text
searchResponse = client().prepareSearch(INDEX).suggest(new SuggestBuilder().addSuggestion("foo", withText).setGlobalText("bogus")).execute().actionGet();
assertSuggestions(searchResponse, "foo", "suggestion10", "suggestion9", "suggestion8", "suggestion7", "suggestion6");
}
use of org.elasticsearch.action.index.IndexRequestBuilder in project elasticsearch by elastic.
the class CompletionSuggestSearchIT method testSuggestDocumentSourceFiltering.
public void testSuggestDocumentSourceFiltering() throws Exception {
final CompletionMappingBuilder mapping = new CompletionMappingBuilder();
createIndexAndMapping(mapping);
int numDocs = randomIntBetween(10, 100);
List<IndexRequestBuilder> indexRequestBuilders = new ArrayList<>();
for (int i = 1; i <= numDocs; i++) {
indexRequestBuilders.add(client().prepareIndex(INDEX, TYPE, "" + i).setSource(jsonBuilder().startObject().startObject(FIELD).field("input", "suggestion" + i).field("weight", i).endObject().field("a", "include").field("b", "exclude").endObject()));
}
indexRandom(true, indexRequestBuilders);
CompletionSuggestionBuilder prefix = SuggestBuilders.completionSuggestion(FIELD).prefix("sugg").size(numDocs);
SearchResponse searchResponse = client().prepareSearch(INDEX).suggest(new SuggestBuilder().addSuggestion("foo", prefix)).setFetchSource("a", "b").get();
CompletionSuggestion completionSuggestion = searchResponse.getSuggest().getSuggestion("foo");
CompletionSuggestion.Entry options = completionSuggestion.getEntries().get(0);
assertThat(options.getOptions().size(), equalTo(numDocs));
int id = numDocs;
for (CompletionSuggestion.Entry.Option option : options) {
assertThat(option.getText().toString(), equalTo("suggestion" + id));
assertSearchHit(option.getHit(), hasId("" + id));
assertSearchHit(option.getHit(), hasScore((id)));
assertNotNull(option.getHit().getSourceAsMap());
Set<String> sourceFields = option.getHit().getSourceAsMap().keySet();
assertThat(sourceFields, contains("a"));
assertThat(sourceFields, not(contains("b")));
id--;
}
}
use of org.elasticsearch.action.index.IndexRequestBuilder in project elasticsearch by elastic.
the class CompletionSuggestSearchIT method testRegex.
public void testRegex() throws Exception {
final CompletionMappingBuilder mapping = new CompletionMappingBuilder();
createIndexAndMapping(mapping);
int numDocs = 10;
List<IndexRequestBuilder> indexRequestBuilders = new ArrayList<>();
for (int i = 1; i <= numDocs; i++) {
indexRequestBuilders.add(client().prepareIndex(INDEX, TYPE, "" + i).setSource(jsonBuilder().startObject().startObject(FIELD).field("input", "sugg" + i + "estion").field("weight", i).endObject().endObject()));
}
indexRandom(true, indexRequestBuilders);
CompletionSuggestionBuilder prefix = SuggestBuilders.completionSuggestion(FIELD).regex("sugg.*es");
assertSuggestions("foo", prefix, "sugg10estion", "sugg9estion", "sugg8estion", "sugg7estion", "sugg6estion");
}
use of org.elasticsearch.action.index.IndexRequestBuilder in project elasticsearch by elastic.
the class CompletionSuggestSearchIT method testFuzzy.
public void testFuzzy() throws Exception {
final CompletionMappingBuilder mapping = new CompletionMappingBuilder();
createIndexAndMapping(mapping);
int numDocs = 10;
List<IndexRequestBuilder> indexRequestBuilders = new ArrayList<>();
for (int i = 1; i <= numDocs; i++) {
indexRequestBuilders.add(client().prepareIndex(INDEX, TYPE, "" + i).setSource(jsonBuilder().startObject().startObject(FIELD).field("input", "sugxgestion" + i).field("weight", i).endObject().endObject()));
}
indexRandom(true, indexRequestBuilders);
CompletionSuggestionBuilder prefix = SuggestBuilders.completionSuggestion(FIELD).prefix("sugg", Fuzziness.ONE);
assertSuggestions("foo", prefix, "sugxgestion10", "sugxgestion9", "sugxgestion8", "sugxgestion7", "sugxgestion6");
}
Aggregations