use of org.graylog.shaded.elasticsearch7.org.elasticsearch.search.builder.SearchSourceBuilder in project elasticsearch by elastic.
the class HighlighterSearchIT method testFastVectorHighlighter.
public void testFastVectorHighlighter() throws Exception {
assertAcked(prepareCreate("test").addMapping("type1", type1TermVectorMapping()));
ensureGreen();
indexRandom(true, client().prepareIndex("test", "type1").setSource("field1", "this is a test", "field2", "The quick brown fox jumps over the lazy dog"));
logger.info("--> highlighting and searching on field1");
SearchSourceBuilder source = searchSource().query(termQuery("field1", "test")).highlighter(highlight().field("field1", 100, 0).order("score").preTags("<xxx>").postTags("</xxx>"));
SearchResponse searchResponse = client().prepareSearch("test").setSource(source).get();
assertHighlight(searchResponse, 0, "field1", 0, 1, equalTo("this is a <xxx>test</xxx>"));
logger.info("--> searching with boundary characters");
source = searchSource().query(matchQuery("field2", "quick")).highlighter(highlight().field("field2", 30, 1).boundaryChars(new char[] { ' ' }));
searchResponse = client().prepareSearch("test").setSource(source).get();
assertHighlight(searchResponse, 0, "field2", 0, 1, equalTo("The <em>quick</em> brown fox jumps over"));
logger.info("--> searching with boundary characters on the field");
source = searchSource().query(matchQuery("field2", "quick")).highlighter(highlight().field(new Field("field2").fragmentSize(30).numOfFragments(1).boundaryChars(new char[] { ' ' })));
searchResponse = client().prepareSearch("test").setSource(source).get();
assertHighlight(searchResponse, 0, "field2", 0, 1, equalTo("The <em>quick</em> brown fox jumps over"));
}
use of org.graylog.shaded.elasticsearch7.org.elasticsearch.search.builder.SearchSourceBuilder in project elasticsearch by elastic.
the class HighlighterSearchIT method testHighlighterWithWordBoundaryScanner.
public void testHighlighterWithWordBoundaryScanner() throws Exception {
assertAcked(prepareCreate("test").addMapping("type1", type1TermVectorMapping()));
ensureGreen();
indexRandom(true, client().prepareIndex("test", "type1").setSource("field1", "some quick and hairy brown:fox jumped over the lazy dog"));
logger.info("--> highlighting and searching on 'field' with word boundary_scanner");
for (String type : new String[] { "unified", "fvh" }) {
SearchSourceBuilder source = searchSource().query(termQuery("field1", "some")).highlighter(highlight().field("field1", 23, 1).highlighterType(type).preTags("<xxx>").postTags("</xxx>").boundaryScannerType(BoundaryScannerType.WORD));
SearchResponse searchResponse = client().prepareSearch("test").setSource(source).get();
assertHighlight(searchResponse, 0, "field1", 0, 1, anyOf(equalTo("<xxx>some</xxx> quick and hairy brown"), equalTo("<xxx>some</xxx>")));
}
}
use of org.graylog.shaded.elasticsearch7.org.elasticsearch.search.builder.SearchSourceBuilder in project zipkin by openzipkin.
the class HttpClient method collectBucketKeys.
@Override
protected ListenableFuture<List<String>> collectBucketKeys(String[] indices, QueryBuilder query, AbstractAggregationBuilder... aggregations) {
SearchSourceBuilder elasticQuery = new SearchSourceBuilder().query(query).size(0);
for (AbstractAggregationBuilder aggregation : aggregations) {
elasticQuery.aggregation(aggregation);
}
HttpCall<List<String>> searchRequest = http.newCall(new Request.Builder().url(lenientSearch(indices, SPAN)).post(RequestBody.create(APPLICATION_JSON, elasticQuery.toString())).tag("search-spansAggregations").build(), BodyConverters.SORTED_KEYS);
CallbackListenableFuture<List<String>> result = new CallbackListenableFuture<>();
searchRequest.submit(result);
return result;
}
use of org.graylog.shaded.elasticsearch7.org.elasticsearch.search.builder.SearchSourceBuilder in project spring-boot by spring-projects.
the class JestAutoConfigurationTests method jestCanCommunicateWithElasticsearchInstance.
@Test
public void jestCanCommunicateWithElasticsearchInstance() throws IOException {
int port = SocketUtils.findAvailableTcpPort();
load(ElasticsearchAutoConfiguration.class, "spring.data.elasticsearch.properties.path.home:target/elastic", "spring.data.elasticsearch.properties.http.enabled:true", "spring.data.elasticsearch.properties.http.port:" + port, "spring.elasticsearch.jest.uris:http://localhost:" + port);
JestClient client = this.context.getBean(JestClient.class);
Map<String, String> source = new HashMap<>();
source.put("a", "alpha");
source.put("b", "bravo");
Index index = new Index.Builder(source).index("foo").type("bar").build();
client.execute(index);
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(QueryBuilders.matchQuery("a", "alpha"));
assertThat(client.execute(new Search.Builder(searchSourceBuilder.toString()).addIndex("foo").build()).getResponseCode()).isEqualTo(200);
}
use of org.graylog.shaded.elasticsearch7.org.elasticsearch.search.builder.SearchSourceBuilder in project camel by apache.
the class ElasticsearchActionRequestConverter method toSearchRequest.
@Converter
public static SearchRequest toSearchRequest(Object queryObject, Exchange exchange) {
SearchRequest searchRequest = new SearchRequest(exchange.getIn().getHeader(ElasticsearchConstants.PARAM_INDEX_NAME, String.class)).types(exchange.getIn().getHeader(ElasticsearchConstants.PARAM_INDEX_TYPE, String.class));
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
String queryText = null;
if (queryObject instanceof Map<?, ?>) {
Map<String, Object> mapQuery = (Map<String, Object>) queryObject;
// Remove 'query' prefix from the query object for backward compatibility
if (mapQuery.containsKey(ElasticsearchConstants.ES_QUERY_DSL_PREFIX)) {
mapQuery = (Map<String, Object>) mapQuery.get(ElasticsearchConstants.ES_QUERY_DSL_PREFIX);
}
try {
XContentBuilder contentBuilder = XContentFactory.contentBuilder(XContentType.JSON);
queryText = contentBuilder.map(mapQuery).string();
} catch (IOException e) {
LOG.error(e.getMessage());
}
} else if (queryObject instanceof String) {
queryText = (String) queryObject;
ObjectMapper mapper = new ObjectMapper();
try {
JsonNode jsonTextObject = mapper.readValue(queryText, JsonNode.class);
JsonNode parentJsonNode = jsonTextObject.get(ElasticsearchConstants.ES_QUERY_DSL_PREFIX);
if (parentJsonNode != null) {
queryText = parentJsonNode.toString();
}
} catch (IOException e) {
LOG.error(e.getMessage());
}
} else {
// Cannot convert the queryObject into SearchRequest
return null;
}
searchSourceBuilder.query(QueryBuilders.wrapperQuery(queryText));
searchRequest.source(searchSourceBuilder);
return searchRequest;
}
Aggregations