Search in sources :

Example 36 with SearchSourceBuilder

use of 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"));
}
Also used : Field(org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder.Field) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 37 with SearchSourceBuilder

use of 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>")));
    }
}
Also used : Matchers.containsString(org.hamcrest.Matchers.containsString) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 38 with SearchSourceBuilder

use of 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;
}
Also used : AbstractAggregationBuilder(org.elasticsearch.search.aggregations.AbstractAggregationBuilder) Request(okhttp3.Request) List(java.util.List) Arrays.asList(java.util.Arrays.asList) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder)

Example 39 with SearchSourceBuilder

use of 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);
}
Also used : HashMap(java.util.HashMap) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) Index(io.searchbox.core.Index) JestClient(io.searchbox.client.JestClient) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) Test(org.junit.Test)

Example 40 with SearchSourceBuilder

use of 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;
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) MultiSearchRequest(org.elasticsearch.action.search.MultiSearchRequest) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) Map(java.util.Map) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) Converter(org.apache.camel.Converter)

Aggregations

SearchSourceBuilder (org.elasticsearch.search.builder.SearchSourceBuilder)152 SearchResponse (org.elasticsearch.action.search.SearchResponse)77 SearchRequest (org.elasticsearch.action.search.SearchRequest)52 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)37 Matchers.containsString (org.hamcrest.Matchers.containsString)32 IOException (java.io.IOException)31 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)28 BoolQueryBuilder (org.elasticsearch.index.query.BoolQueryBuilder)26 List (java.util.List)25 SearchSourceBuilder (org.graylog.shaded.elasticsearch6.org.elasticsearch.search.builder.SearchSourceBuilder)24 SearchSourceBuilder (org.graylog.shaded.elasticsearch7.org.elasticsearch.search.builder.SearchSourceBuilder)24 Search (io.searchbox.core.Search)23 Map (java.util.Map)23 Set (java.util.Set)19 ArrayList (java.util.ArrayList)18 Collectors (java.util.stream.Collectors)18 Test (org.junit.Test)18 Collections (java.util.Collections)15 SearchHit (org.elasticsearch.search.SearchHit)15 TermsAggregationBuilder (org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder)15