use of org.elasticsearch.search.suggest.CompletionSuggestSearchIT.CompletionMappingBuilder in project elasticsearch by elastic.
the class ContextCompletionSuggestSearchIT method testMissingContextValue.
public void testMissingContextValue() throws Exception {
LinkedHashMap<String, ContextMapping> map = new LinkedHashMap<>();
map.put("cat", ContextBuilder.category("cat").field("cat").build());
map.put("type", ContextBuilder.category("type").field("type").build());
final CompletionMappingBuilder mapping = new CompletionMappingBuilder().context(map);
createIndexAndMapping(mapping);
int numDocs = 10;
List<IndexRequestBuilder> indexRequestBuilders = new ArrayList<>();
for (int i = 0; i < numDocs; i++) {
XContentBuilder source = jsonBuilder().startObject().startObject(FIELD).field("input", "suggestion" + i).field("weight", i + 1).endObject();
if (randomBoolean()) {
source.field("cat", "cat" + i % 2);
}
if (randomBoolean()) {
source.field("type", "type" + i % 4);
}
source.endObject();
indexRequestBuilders.add(client().prepareIndex(INDEX, TYPE, "" + i).setSource(source));
}
indexRandom(true, indexRequestBuilders);
CompletionSuggestionBuilder prefix = SuggestBuilders.completionSuggestion(FIELD).prefix("sugg");
assertSuggestions("foo", prefix, "suggestion9", "suggestion8", "suggestion7", "suggestion6", "suggestion5");
}
use of org.elasticsearch.search.suggest.CompletionSuggestSearchIT.CompletionMappingBuilder in project elasticsearch by elastic.
the class ContextCompletionSuggestSearchIT method testSeveralContexts.
public void testSeveralContexts() throws Exception {
LinkedHashMap<String, ContextMapping> map = new LinkedHashMap<>();
final int numContexts = randomIntBetween(2, 5);
for (int i = 0; i < numContexts; i++) {
map.put("type" + i, ContextBuilder.category("type" + i).field("type" + i).build());
}
final CompletionMappingBuilder mapping = new CompletionMappingBuilder().context(map);
createIndexAndMapping(mapping);
int numDocs = randomIntBetween(10, 200);
List<IndexRequestBuilder> indexRequestBuilders = new ArrayList<>();
for (int i = 0; i < numDocs; i++) {
XContentBuilder source = jsonBuilder().startObject().startObject(FIELD).field("input", "suggestion" + i).field("weight", numDocs - i).endObject();
for (int c = 0; c < numContexts; c++) {
source.field("type" + c, "type" + c + i % 4);
}
source.endObject();
indexRequestBuilders.add(client().prepareIndex(INDEX, TYPE, "" + i).setSource(source));
}
indexRandom(true, indexRequestBuilders);
CompletionSuggestionBuilder prefix = SuggestBuilders.completionSuggestion(FIELD).prefix("sugg");
assertSuggestions("foo", prefix, "suggestion0", "suggestion1", "suggestion2", "suggestion3", "suggestion4");
}
use of org.elasticsearch.search.suggest.CompletionSuggestSearchIT.CompletionMappingBuilder in project elasticsearch by elastic.
the class ContextCompletionSuggestSearchIT method testContextPrefix.
public void testContextPrefix() throws Exception {
LinkedHashMap<String, ContextMapping> map = new LinkedHashMap<>();
map.put("cat", ContextBuilder.category("cat").field("cat").build());
boolean addAnotherContext = randomBoolean();
if (addAnotherContext) {
map.put("type", ContextBuilder.category("type").field("type").build());
}
final CompletionMappingBuilder mapping = new CompletionMappingBuilder().context(map);
createIndexAndMapping(mapping);
int numDocs = 10;
List<IndexRequestBuilder> indexRequestBuilders = new ArrayList<>();
for (int i = 0; i < numDocs; i++) {
XContentBuilder source = jsonBuilder().startObject().startObject(FIELD).field("input", "suggestion" + i).field("weight", i + 1).endObject().field("cat", "cat" + i % 2);
if (addAnotherContext) {
source.field("type", "type" + i % 3);
}
source.endObject();
indexRequestBuilders.add(client().prepareIndex(INDEX, TYPE, "" + i).setSource(source));
}
indexRandom(true, indexRequestBuilders);
CompletionSuggestionBuilder prefix = SuggestBuilders.completionSuggestion(FIELD).prefix("sugg");
assertSuggestions("foo", prefix, "suggestion9", "suggestion8", "suggestion7", "suggestion6", "suggestion5");
}
use of org.elasticsearch.search.suggest.CompletionSuggestSearchIT.CompletionMappingBuilder in project elasticsearch by elastic.
the class ContextCompletionSuggestSearchIT method testGeoFiltering.
public void testGeoFiltering() throws Exception {
LinkedHashMap<String, ContextMapping> map = new LinkedHashMap<>();
map.put("geo", ContextBuilder.geo("geo").build());
final CompletionMappingBuilder mapping = new CompletionMappingBuilder().context(map);
createIndexAndMapping(mapping);
int numDocs = 10;
List<IndexRequestBuilder> indexRequestBuilders = new ArrayList<>();
GeoPoint[] geoPoints = new GeoPoint[] { new GeoPoint("ezs42e44yx96"), new GeoPoint("u4pruydqqvj8") };
for (int i = 0; i < numDocs; i++) {
XContentBuilder source = jsonBuilder().startObject().startObject(FIELD).field("input", "suggestion" + i).field("weight", i + 1).startObject("contexts").field("geo", (i % 2 == 0) ? geoPoints[0].getGeohash() : geoPoints[1].getGeohash()).endObject().endObject().endObject();
indexRequestBuilders.add(client().prepareIndex(INDEX, TYPE, "" + i).setSource(source));
}
indexRandom(true, indexRequestBuilders);
CompletionSuggestionBuilder prefix = SuggestBuilders.completionSuggestion(FIELD).prefix("sugg");
assertSuggestions("foo", prefix, "suggestion9", "suggestion8", "suggestion7", "suggestion6", "suggestion5");
CompletionSuggestionBuilder geoFilteringPrefix = SuggestBuilders.completionSuggestion(FIELD).prefix("sugg").contexts(Collections.singletonMap("geo", Collections.singletonList(GeoQueryContext.builder().setGeoPoint(new GeoPoint(geoPoints[0])).build())));
assertSuggestions("foo", geoFilteringPrefix, "suggestion8", "suggestion6", "suggestion4", "suggestion2", "suggestion0");
}
use of org.elasticsearch.search.suggest.CompletionSuggestSearchIT.CompletionMappingBuilder in project elasticsearch by elastic.
the class ContextCompletionSuggestSearchIT method testGeoNeighbours.
public void testGeoNeighbours() throws Exception {
String geohash = "gcpv";
List<String> neighbours = new ArrayList<>();
neighbours.add("gcpw");
neighbours.add("gcpy");
neighbours.add("u10n");
neighbours.add("gcpt");
neighbours.add("u10j");
neighbours.add("gcps");
neighbours.add("gcpu");
neighbours.add("u10h");
LinkedHashMap<String, ContextMapping> map = new LinkedHashMap<>();
map.put("geo", ContextBuilder.geo("geo").precision(4).build());
final CompletionMappingBuilder mapping = new CompletionMappingBuilder().context(map);
createIndexAndMapping(mapping);
int numDocs = 10;
List<IndexRequestBuilder> indexRequestBuilders = new ArrayList<>();
for (int i = 0; i < numDocs; i++) {
XContentBuilder source = jsonBuilder().startObject().startObject(FIELD).field("input", "suggestion" + i).field("weight", i + 1).startObject("contexts").field("geo", randomFrom(neighbours)).endObject().endObject().endObject();
indexRequestBuilders.add(client().prepareIndex(INDEX, TYPE, "" + i).setSource(source));
}
indexRandom(true, indexRequestBuilders);
CompletionSuggestionBuilder prefix = SuggestBuilders.completionSuggestion(FIELD).prefix("sugg");
assertSuggestions("foo", prefix, "suggestion9", "suggestion8", "suggestion7", "suggestion6", "suggestion5");
CompletionSuggestionBuilder geoNeighbourPrefix = SuggestBuilders.completionSuggestion(FIELD).prefix("sugg").contexts(Collections.singletonMap("geo", Collections.singletonList(GeoQueryContext.builder().setGeoPoint(GeoPoint.fromGeohash(geohash)).build())));
assertSuggestions("foo", geoNeighbourPrefix, "suggestion9", "suggestion8", "suggestion7", "suggestion6", "suggestion5");
}
Aggregations