Java 类org.elasticsearch.index.query.QueryBuilders 实例源码

项目:backside-servlet-ks    文件:ElasticQueryDSL.java   
/**
 * Here to establish QueryBuilder patterns
 */
void test() {
    SearchSourceBuilder b;
    BoolQueryBuilder qb = QueryBuilders.boolQuery();
    QueryBuilder qbmm = QueryBuilders.multiMatchQuery("name", "fieldNames");
    QueryBuilder qbm = QueryBuilders.matchQuery("name", "text");
    QueryBuilder qbmp = QueryBuilders.matchPhraseQuery("name", "text");
    QueryBuilder qb1 = QueryBuilders.termQuery(ITQCoreOntology.TUPLE_SUBJECT_PROPERTY, "");
    QueryBuilder qb2 = QueryBuilders.termQuery(ITQCoreOntology.TUPLE_OBJECT_PROPERTY, "");
    QueryBuilder qb3 = QueryBuilders.termQuery(ITQCoreOntology.INSTANCE_OF_PROPERTY_TYPE, "");
    qb.must(qb3);
    qb.should(qb1);
    qb.should(qb2);
    QueryBuilder partial = QueryBuilders.regexpQuery("name", "regexp");
    QueryBuilder wildcard = QueryBuilders.wildcardQuery("name", "query");
}
项目:elasticsearch_my    文件:RandomQueryGenerator.java   
private static QueryBuilder randomTerminalQuery(List<String> stringFields, List<String> numericFields, int numDocs) {
    switch (randomIntBetween(0,6)) {
        case 0:
            return randomTermQuery(stringFields, numDocs);
        case 1:
            return randomTermsQuery(stringFields, numDocs);
        case 2:
            return randomRangeQuery(numericFields, numDocs);
        case 3:
            return QueryBuilders.matchAllQuery();
        case 4:
            return randomCommonTermsQuery(stringFields, numDocs);
        case 5:
            return randomFuzzyQuery(stringFields);
        case 6:
            return randomIDsQuery();
        default:
            return randomTermQuery(stringFields, numDocs);
    }
}
项目:Elasticsearch-Tutorial-zh-CN    文件:BaseDemo.java   
/**
 * Scroll 获取多个对象
 *
 * @param transportClient
 * @throws IOException
 */
private static void queryByScroll(TransportClient transportClient) throws IOException {

    //setSize 是设置每批查询多少条数据
    SearchResponse searchResponse = transportClient.prepareSearch("product_index").setTypes("product")
            .setQuery(QueryBuilders.termQuery("product_name", "飞利浦"))
            .setScroll(new TimeValue(60000))
            .setSize(3)
            .get();

    int count = 0;

    do {
        for (SearchHit searchHit : searchResponse.getHits().getHits()) {
            //打印查询结果,或者做其他处理
            logger.info("count=" + ++count);
            logger.info(searchHit.getSourceAsString());
        }

        searchResponse = transportClient.prepareSearchScroll(searchResponse.getScrollId()).setScroll(new TimeValue(60000))
                .execute()
                .actionGet();
    } while (searchResponse.getHits().getHits().length != 0);
}
项目:elasticsearch_my    文件:CopyToMapperIntegrationIT.java   
public void testDynamicObjectCopyTo() throws Exception {
    String mapping = jsonBuilder().startObject().startObject("doc").startObject("properties")
        .startObject("foo")
            .field("type", "text")
            .field("copy_to", "root.top.child")
        .endObject()
        .endObject().endObject().endObject().string();
    assertAcked(
        client().admin().indices().prepareCreate("test-idx")
            .addMapping("doc", mapping, XContentType.JSON)
    );
    client().prepareIndex("test-idx", "doc", "1")
        .setSource("foo", "bar")
        .get();
    client().admin().indices().prepareRefresh("test-idx").execute().actionGet();
    SearchResponse response = client().prepareSearch("test-idx")
        .setQuery(QueryBuilders.termQuery("root.top.child", "bar")).get();
    assertThat(response.getHits().getTotalHits(), equalTo(1L));
}
项目:elasticsearch_my    文件:MatchedQueriesIT.java   
public void testRegExpQuerySupportsName() {
    createIndex("test1");
    ensureGreen();

    client().prepareIndex("test1", "type1", "1").setSource("title", "title1").get();
    refresh();

    SearchResponse searchResponse = client().prepareSearch()
            .setQuery(QueryBuilders.regexpQuery("title", "title1").queryName("regex")).get();
    assertHitCount(searchResponse, 1L);

    for (SearchHit hit : searchResponse.getHits()) {
        if (hit.getId().equals("1")) {
            assertThat(hit.getMatchedQueries().length, equalTo(1));
            assertThat(hit.getMatchedQueries(), hasItemInArray("regex"));
        } else {
            fail("Unexpected document returned with id " + hit.getId());
        }
    }
}
项目:css-elasticsearch    文件:ElasticsearchTransportFactory.java   
public String selectAll(String indexs,String types,String condition){
    try {
        if(client==null){
            init();
        }
        SearchRequestBuilder request = client.prepareSearch(indexs.split(",")).setTypes(types.split(","));
        request.setSearchType(SearchType.DFS_QUERY_THEN_FETCH);
        request.setQuery(QueryBuilders.queryStringQuery(condition));
        request.setExplain(false);
        SearchResponse response = request.get();
        return response.toString();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return null;
}
项目:elasticsearch_my    文件:RandomQueryGenerator.java   
private static QueryBuilder randomDisMaxQuery(List<String> stringFields, List<String> numericFields, int numDocs, int depth) {
    QueryBuilder q =  QueryBuilders.disMaxQuery();

    int numClauses = randomIntBetween(1, 10);
    for (int i = 0; i < numClauses; i++) {
        ((DisMaxQueryBuilder)q).add(randomQueryBuilder(stringFields, numericFields, numDocs, depth - 1));
    }

    if (randomBoolean()) {
        ((DisMaxQueryBuilder)q).boost(randomFloat());
    }

    if (randomBoolean()) {
        ((DisMaxQueryBuilder)q).tieBreaker(randomFloat());
    }

    return q;
}
项目:memory-graph    文件:Elasticsearch5SearchIndex.java   
private void deleteExtendedDataForElement(Element element) {
    try {
        QueryBuilder filter = QueryBuilders.termQuery(EXTENDED_DATA_ELEMENT_ID_FIELD_NAME, element.getId());

        SearchRequestBuilder s = getClient().prepareSearch(getIndicesToQuery())
                .setTypes(ELEMENT_TYPE)
                .setQuery(QueryBuilders.boolQuery().must(QueryBuilders.matchAllQuery()).filter(filter))
                .storedFields(
                        EXTENDED_DATA_ELEMENT_ID_FIELD_NAME,
                        EXTENDED_DATA_TABLE_NAME_FIELD_NAME,
                        EXTENDED_DATA_TABLE_ROW_ID_FIELD_NAME
                );
        for (SearchHit hit : s.execute().get().getHits()) {
            if (MUTATION_LOGGER.isTraceEnabled()) {
                LOGGER.trace("deleting extended data document %s", hit.getId());
            }
            getClient().prepareDelete(hit.getIndex(), ELEMENT_TYPE, hit.getId()).execute().actionGet();
        }
    } catch (Exception ex) {
        throw new MemgraphException("Could not delete extended data for element: " + element.getId());
    }
}
项目:grpc-mate    文件:ProductDao.java   
/**
 * Search Product by keywords.
 *
 * @param request search request which contains keyword and limit
 * @return SearchProductsResponse
 */
public SearchProductsResponse searchProducts(SearchProductsRequest request) throws InvalidProtocolBufferException {
  QueryBuilder queryBuilder = QueryBuilders.boolQuery()
      .must(QueryBuilders.matchQuery("productName", request.getKeyWord()));
  SearchResponse response = esClient.prepareSearch(INDEX)
      .setTypes(TYPE)
      .setQuery(queryBuilder)
      .setSize(request.getLimit())
      .execute()
      .actionGet();
  SearchHits hits = response.getHits();
  SearchProductsResponse.Builder responseBuilder = SearchProductsResponse.newBuilder();
  for (SearchHit hit : hits) {
    Product.Builder builder = Product.newBuilder();
    jsonParser.merge(hit.getSourceAsString(), builder);
    responseBuilder.addProducts(builder.build());
  }
  return responseBuilder.build();
}
项目:Elasticsearch    文件:DfsOnlyRequest.java   
public DfsOnlyRequest(Fields termVectorsFields, String[] indices, String[] types, Set<String> selectedFields) throws IOException {
    super(indices);

    // build a search request with a query of all the terms
    final BoolQueryBuilder boolBuilder = boolQuery();
    for (String fieldName : termVectorsFields) {
        if ((selectedFields != null) && (!selectedFields.contains(fieldName))) {
            continue;
        }
        Terms terms = termVectorsFields.terms(fieldName);
        TermsEnum iterator = terms.iterator();
        while (iterator.next() != null) {
            String text = iterator.term().utf8ToString();
            boolBuilder.should(QueryBuilders.termQuery(fieldName, text));
        }
    }
    // wrap a search request object
    this.searchRequest = new SearchRequest(indices).types(types).source(new SearchSourceBuilder().query(boolBuilder));
}
项目:Transwarp-Sample-Code    文件:SearchES.java   
/**
 * wildcard查询/or条件/and条件
 */
public static void wildcardQuery() {
    try {
        Settings settings = Settings.settingsBuilder().put("cluster.name", "elasticsearch1").build();
        TransportClient transportClient = TransportClient.builder().
                settings(settings).build().addTransportAddress(
                new InetSocketTransportAddress(InetAddress.getByName("172.16.2.94"), 9300));
        SearchRequestBuilder searchRequestBuilder = transportClient.prepareSearch("sqd.es_start");

        // {"query": {"bool": {"must": [{"or": [{"wildcard": {"content": "*oracle*"}},{"wildcard": {"content": "*mysql*"}}]}],"must_not": [],"should": []}},"from": 0, "size": 10, "sort": [],"aggs": {}}
        SearchResponse searchResponse = searchRequestBuilder.
                setQuery(QueryBuilders.boolQuery()
                .must(QueryBuilders.orQuery(QueryBuilders.wildcardQuery("content","*mysql*"),
                        QueryBuilders.wildcardQuery("content","*oracle*")))
                .must(QueryBuilders.termQuery("tbool","false")))
                .setFrom(0).setSize(100).setExplain(true).execute().actionGet();
        SearchHits searchHits = searchResponse.getHits();
        System.out.println();
        System.out.println("Total Hits is " + searchHits.totalHits());
        System.out.println();
        for (int i = 0; i < searchHits.getHits().length; ++i) {
            System.out.println("content is "
                    + searchHits.getHits()[i].getSource().get("content"));
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
项目:elasticsearch_my    文件:HighlighterSearchIT.java   
public void testPostingsHighlighterMultipleFields() throws Exception {
    assertAcked(prepareCreate("test").addMapping("type1", type1PostingsffsetsMapping()).get());
    ensureGreen();

    index("test", "type1", "1",
            "field1", "The <b>quick<b> brown fox. Second sentence.",
            "field2", "The <b>slow<b> brown fox. Second sentence.");
    refresh();

    for (String type : UNIFIED_AND_NULL) {
        SearchResponse response = client().prepareSearch("test")
            .setQuery(QueryBuilders.matchQuery("field1", "fox"))
            .highlighter(
                new HighlightBuilder().field(new Field("field1").preTags("<1>").postTags("</1>")
                    .requireFieldMatch(true).highlighterType(type)))
            .get();
        assertHighlight(response, 0, "field1", 0, 1, equalTo("The <b>quick<b> brown <1>fox</1>."));
    }
}
项目:es-sql    文件:QueryMaker.java   
/**
 * 增加嵌套插
 * 
 * @param boolQuery
 * @param where
 * @param subQuery
 */
private void addSubQuery(BoolQueryBuilder boolQuery, Where where, QueryBuilder subQuery) {
       if(where instanceof Condition){
           Condition condition = (Condition) where;

           if(condition.isNested()){
               subQuery = QueryBuilders.nestedQuery(condition.getNestedPath(), subQuery, ScoreMode.None);
           } else if(condition.isChildren()) {
            subQuery = QueryBuilders.hasChildQuery(condition.getChildType(), subQuery, ScoreMode.None);
           }
       }

    if (where.getConn() == CONN.AND) {
        boolQuery.must(subQuery);
    } else {
        boolQuery.should(subQuery);
    }
}
项目:elasticsearch_my    文件:ContextAndHeaderTransportIT.java   
public void testThatTermsLookupGetRequestContainsContextAndHeaders() throws Exception {
    transportClient().prepareIndex(lookupIndex, "type", "1")
        .setSource(jsonBuilder().startObject().array("followers", "foo", "bar", "baz").endObject()).get();
    transportClient().prepareIndex(queryIndex, "type", "1")
        .setSource(jsonBuilder().startObject().field("username", "foo").endObject()).get();
    transportClient().admin().indices().prepareRefresh(queryIndex, lookupIndex).get();

    TermsLookup termsLookup = new TermsLookup(lookupIndex, "type", "1", "followers");
    TermsQueryBuilder termsLookupFilterBuilder = QueryBuilders.termsLookupQuery("username", termsLookup);
    BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery().must(QueryBuilders.matchAllQuery()).must(termsLookupFilterBuilder);

    SearchResponse searchResponse = transportClient()
        .prepareSearch(queryIndex)
        .setQuery(queryBuilder)
        .get();
    assertNoFailures(searchResponse);
    assertHitCount(searchResponse, 1);

    assertGetRequestsContainHeaders();
}
项目:elasticsearch_my    文件:HighlighterSearchIT.java   
public void testMissingStoredField() throws Exception {
    assertAcked(prepareCreate("test")
            .addMapping("type1", "highlight_field", "type=text,store=true"));
    ensureGreen();
    client().prepareIndex("test", "type1", "1")
            .setSource(jsonBuilder().startObject()
                    .field("field", "highlight")
                    .endObject()).get();
    refresh();

    for (String type : UNIFIED_AND_NULL) {
        // This query used to fail when the field to highlight was absent
        SearchResponse response = client().prepareSearch("test")
            .setQuery(QueryBuilders.matchQuery("field", "highlight").type(MatchQuery.Type.BOOLEAN))
            .highlighter(
                new HighlightBuilder().field(new HighlightBuilder.Field("highlight_field").fragmentSize(-1).numOfFragments(1)
                    .fragmenter("simple")).highlighterType(type)).get();
        assertThat(response.getHits().getHits()[0].getHighlightFields().isEmpty(), equalTo(true));
    }
}
项目:css-elasticsearch    文件:ElasticsearchExtendHighRestFactory.java   
public String selectTermAll(String indexs,String types,String field,String value){
    try {
        if(StringUtil.isEmpty(indexs))indexs="_all";
        if(xclient==null){
            init();
        }
        SearchSourceBuilder search = new SearchSourceBuilder();
        if(!StringUtil.isEmpty(field)&&!StringUtil.isEmpty(value)&&!(field.matches(regex)||field.matches(value))){
            search.query(QueryBuilders.termQuery(field, value));
        }
        search.aggregation(AggregationBuilders.terms("data").field(field+".keyword"));
        search.explain(false);
        SearchRequest request = new SearchRequest();
        request.searchType(SearchType.DFS_QUERY_THEN_FETCH);
        request.source(search);
        request.indices(indexs.split(","));
        request.types(types.split(","));
        SearchResponse response = xclient.search(request);
        return response.toString();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return null;
}
项目:elasticsearch_my    文件:HighlighterSearchIT.java   
public void testPlainHighlighterMultipleFields() {
    createIndex("test");
    ensureGreen();

    index("test", "type1", "1", "field1", "The <b>quick<b> brown fox", "field2", "The <b>slow<b> brown fox");
    refresh();

    SearchResponse response = client().prepareSearch("test")
            .setQuery(QueryBuilders.matchQuery("field1", "fox"))
            .highlighter(
                    new HighlightBuilder().field(
                            new HighlightBuilder.Field("field1").preTags("<1>").postTags("</1>").requireFieldMatch(true)).field(
                            new HighlightBuilder.Field("field2").preTags("<2>").postTags("</2>").requireFieldMatch(false)))
            .get();
    assertHighlight(response, 0, "field1", 0, 1, equalTo("The <b>quick<b> brown <1>fox</1>"));
    assertHighlight(response, 0, "field2", 0, 1, equalTo("The <b>slow<b> brown <2>fox</2>"));
}
项目:soundwave    文件:EsInstanceStore.java   
@Override
public Iterator<EsQueryResult> getRunningInstancesWithFields(String[] includeFields)
    throws Exception {

  QueryBuilder queryBuilder = QueryBuilders.filteredQuery(
      QueryBuilders.termQuery("state", "running"),
      FilterBuilders.termFilter("state", "running"));

  ScrollableResponse<List<EsQueryResult>> response = this.retrieveScrollByQuery(queryBuilder,
      includeFields, BATCHSIZE,
      str -> insertMapper.readValue(str, EsQueryResult.class));

  EsIterator<EsQueryResult>
      iterator =
      new EsIterator<>(response, r -> scrollNext(r.getContinousToken(),
          str -> insertMapper.readValue(str, EsQueryResult.class)));

  return iterator;
}
项目:elasticsearch_my    文件:MultiSearchIT.java   
public void testSimpleMultiSearch() {
    createIndex("test");
    ensureGreen();
    client().prepareIndex("test", "type", "1").setSource("field", "xxx").execute().actionGet();
    client().prepareIndex("test", "type", "2").setSource("field", "yyy").execute().actionGet();
    refresh();
    MultiSearchResponse response = client().prepareMultiSearch()
            .add(client().prepareSearch("test").setQuery(QueryBuilders.termQuery("field", "xxx")))
            .add(client().prepareSearch("test").setQuery(QueryBuilders.termQuery("field", "yyy")))
            .add(client().prepareSearch("test").setQuery(QueryBuilders.matchAllQuery()))
            .execute().actionGet();

    for (MultiSearchResponse.Item item : response) {
       assertNoFailures(item.getResponse());
    }
    assertThat(response.getResponses().length, equalTo(3));
    assertHitCount(response.getResponses()[0].getResponse(), 1L);
    assertHitCount(response.getResponses()[1].getResponse(), 1L);
    assertHitCount(response.getResponses()[2].getResponse(), 2L);
    assertFirstHit(response.getResponses()[0].getResponse(), hasId("1"));
    assertFirstHit(response.getResponses()[1].getResponse(), hasId("2"));
}
项目:elasticsearch_my    文件:MatchedQueriesIT.java   
public void testWildcardQuerySupportsName() {
    createIndex("test1");
    ensureGreen();

    client().prepareIndex("test1", "type1", "1").setSource("title", "title1").get();
    refresh();

    SearchResponse searchResponse = client().prepareSearch()
            .setQuery(QueryBuilders.wildcardQuery("title", "titl*").queryName("wildcard")).get();
    assertHitCount(searchResponse, 1L);

    for (SearchHit hit : searchResponse.getHits()) {
        if (hit.getId().equals("1")) {
            assertThat(hit.getMatchedQueries().length, equalTo(1));
            assertThat(hit.getMatchedQueries(), hasItemInArray("wildcard"));
        } else {
            fail("Unexpected document returned with id " + hit.getId());
        }
    }
}
项目:elasticsearch_my    文件:HighlighterSearchIT.java   
public void testResetTwice() throws Exception {
    assertAcked(prepareCreate("test")
            .setSettings(Settings.builder()
                    .put(indexSettings())
                    .put("analysis.analyzer.my_analyzer.type", "pattern")
                    .put("analysis.analyzer.my_analyzer.pattern", "\\s+")
                    .build())
            .addMapping("type", "text", "type=text,analyzer=my_analyzer"));
    ensureGreen();
    client().prepareIndex("test", "type", "1")
            .setSource("text", "elasticsearch test").get();
    refresh();

    for (String type : UNIFIED_AND_NULL) {
        SearchResponse response = client().prepareSearch("test")
            .setQuery(QueryBuilders.matchQuery("text", "test").type(MatchQuery.Type.BOOLEAN))
            .highlighter(new HighlightBuilder().field("text").highlighterType(type)).execute().actionGet();
        // PatternAnalyzer will throw an exception if it is resetted twice
        assertHitCount(response, 1L);
    }
}
项目:database-transform-tool    文件:ElasticsearchExtendHighRestFactory.java   
public String selectTermAll(String indexs,String types,String field,String value){
    try {
        if(StringUtil.isEmpty(indexs))indexs="_all";
        if(xclient==null){
            init();
        }
        SearchSourceBuilder search = new SearchSourceBuilder();
        if(!StringUtil.isEmpty(field)&&!StringUtil.isEmpty(value)&&!(field.matches(regex)||field.matches(value))){
            search.query(QueryBuilders.termQuery(field, value));
        }
        search.aggregation(AggregationBuilders.terms("data").field(field+".keyword"));
        search.explain(false);
        SearchRequest request = new SearchRequest();
        request.searchType(SearchType.DFS_QUERY_THEN_FETCH);
        request.source(search);
        request.indices(indexs.split(","));
        request.types(types.split(","));
        SearchResponse response = xclient.search(request);
        return response.toString();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return null;
}
项目:SpringBootStudy    文件:CityServiceImpl.java   
@Override
public List<City> searchCity(Integer pageNumber,
                             Integer pageSize,
                             String searchContent) {
    // 分页参数
    Pageable pageable = new PageRequest(pageNumber, pageSize);

    // Function Score Query
    FunctionScoreQueryBuilder functionScoreQueryBuilder = QueryBuilders.functionScoreQuery()
            .add(QueryBuilders.boolQuery().should(QueryBuilders.matchQuery("cityname", searchContent)),
                    ScoreFunctionBuilders.weightFactorFunction(1000))
            .add(QueryBuilders.boolQuery().should(QueryBuilders.matchQuery("description", searchContent)),
                    ScoreFunctionBuilders.weightFactorFunction(100));

    // 创建搜索 DSL 查询
    SearchQuery searchQuery = new NativeSearchQueryBuilder()
            .withPageable(pageable)
            .withQuery(functionScoreQueryBuilder).build();

    LOGGER.info("\n searchCity(): searchContent [" + searchContent + "] \n DSL  = \n " + searchQuery.getQuery().toString());

    Page<City> searchPageResults = cityRepository.search(searchQuery);
    return searchPageResults.getContent();
}
项目:elasticsearch_my    文件:MoreLikeThisIT.java   
public void testSimpleMoreLikeThisIds() throws Exception {
    logger.info("Creating index test");
    assertAcked(prepareCreate("test").addMapping("type1",
            jsonBuilder().startObject().startObject("type1").startObject("properties")
                    .startObject("text").field("type", "text").endObject()
                    .endObject().endObject().endObject()));

    logger.info("Running Cluster Health");
    assertThat(ensureGreen(), equalTo(ClusterHealthStatus.GREEN));

    logger.info("Indexing...");
    List<IndexRequestBuilder> builders = new ArrayList<>();
    builders.add(client().prepareIndex("test", "type1").setSource("text", "lucene").setId("1"));
    builders.add(client().prepareIndex("test", "type1").setSource("text", "lucene release").setId("2"));
    builders.add(client().prepareIndex("test", "type1").setSource("text", "apache lucene").setId("3"));
    indexRandom(true, builders);

    logger.info("Running MoreLikeThis");
    MoreLikeThisQueryBuilder queryBuilder = QueryBuilders.moreLikeThisQuery(new String[] {"text"}, null, ids("1")).include(true).minTermFreq(1).minDocFreq(1);
    SearchResponse mltResponse = client().prepareSearch().setTypes("type1").setQuery(queryBuilder).execute().actionGet();
    assertHitCount(mltResponse, 3L);
}
项目:elasticsearch_my    文件:GeoShapeQueryTests.java   
public void testShapeFilterWithRandomGeoCollection() throws Exception {
    // Create a random geometry collection.
    GeometryCollectionBuilder gcb = RandomShapeGenerator.createGeometryCollection(random());

    logger.info("Created Random GeometryCollection containing {} shapes", gcb.numShapes());

    client().admin().indices().prepareCreate("test").addMapping("type", "location", "type=geo_shape,tree=quadtree")
            .execute().actionGet();

    XContentBuilder docSource = gcb.toXContent(jsonBuilder().startObject().field("location"), null).endObject();
    client().prepareIndex("test", "type", "1").setSource(docSource).setRefreshPolicy(IMMEDIATE).get();

    ShapeBuilder filterShape = (gcb.getShapeAt(randomIntBetween(0, gcb.numShapes() - 1)));

    GeoShapeQueryBuilder filter = QueryBuilders.geoShapeQuery("location", filterShape);
    filter.relation(ShapeRelation.INTERSECTS);
    SearchResponse result = client().prepareSearch("test").setTypes("type").setQuery(QueryBuilders.matchAllQuery())
            .setPostFilter(filter).get();
    assertSearchResponse(result);
    assertHitCount(result, 1);
}
项目:crawling-framework    文件:EsHttpUrlOperations.java   
public List<HttpUrl> findUrlsByStatusAndSource(Enum status, String source, int count) {
    BoolQueryBuilder filter = QueryBuilders.boolQuery()
            .must(QueryBuilders.termQuery("status", String.valueOf(status)))
            .must(QueryBuilders.termQuery("source", source));

    SearchResponse response = getConnection().getClient()
            .prepareSearch(getIndex())
            .setTypes(getType())
            .setSearchType(SearchType.DEFAULT)
            .setPostFilter(filter)
            .addSort("created", SortOrder.DESC)
            .setSize(count)
            .setFetchSource(true)
            .setExplain(false)
            .execute()
            .actionGet();

    SearchHits hits = response.getHits();
    return Arrays.stream(hits.getHits())
            .map(SearchHit::getSource)
            .map(s -> {
                HttpUrl httpUrl = new HttpUrl();
                httpUrl.setUrl(Objects.toString(s.get("url"), null));
                httpUrl.setPublished(Objects.toString(s.get("published"), null));
                httpUrl.setDiscovered(EsDataParser.nullOrDate(s.get("created")));
                httpUrl.setSource(source);
                return httpUrl;
            })
            .collect(Collectors.toList());
}
项目:elasticsearch_my    文件:TransportTwoNodesSearchIT.java   
public void testFailedMultiSearchWithWrongQuery() throws Exception {
    prepareData();

    logger.info("Start Testing failed multi search with a wrong query");

    MultiSearchResponse response = client().prepareMultiSearch()
            .add(client().prepareSearch("test").setQuery(new MatchQueryBuilder("foo", "biz")))
            .add(client().prepareSearch("test").setQuery(QueryBuilders.termQuery("nid", 2)))
            .add(client().prepareSearch("test").setQuery(QueryBuilders.matchAllQuery()))
            .execute().actionGet();
    assertThat(response.getResponses().length, equalTo(3));
    assertThat(response.getResponses()[0].getFailureMessage(), notNullValue());

    assertThat(response.getResponses()[1].getFailureMessage(), nullValue());
    assertThat(response.getResponses()[1].getResponse().getHits().getHits().length, equalTo(1));

    assertThat(response.getResponses()[2].getFailureMessage(), nullValue());
    assertThat(response.getResponses()[2].getResponse().getHits().getHits().length, equalTo(10));

    logger.info("Done Testing failed search");
}
项目:elasticsearch_my    文件:FiltersAggregatorTests.java   
public void testEmpty() throws Exception {
    Directory directory = newDirectory();
    RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory);
    indexWriter.close();
    IndexReader indexReader = DirectoryReader.open(directory);
    IndexSearcher indexSearcher = newSearcher(indexReader, true, true);
    int numFilters = randomIntBetween(1, 10);
    QueryBuilder[] filters = new QueryBuilder[numFilters];
    for (int i = 0; i < filters.length; i++) {
        filters[i] = QueryBuilders.termQuery("field", randomAsciiOfLength(5));
    }
    FiltersAggregationBuilder builder = new FiltersAggregationBuilder("test", filters);
    builder.otherBucketKey("other");
    InternalFilters response = search(indexSearcher, new MatchAllDocsQuery(), builder, fieldType);
    assertEquals(response.getBuckets().size(), numFilters);
    for (InternalFilters.InternalBucket filter : response.getBuckets()) {
        assertEquals(filter.getDocCount(), 0);
    }
    indexReader.close();
    directory.close();
}
项目:tasfe-framework    文件:LogConfigFactory.java   
public static LogcenterConfig createConfig(JSONObject jsonInput) throws LogConsumerException {
    String productLine = String.valueOf(jsonInput.get("productLine"));
    String appName = String.valueOf(jsonInput.get("appName"));
    QueryBuilder qb = QueryBuilders.queryStringQuery("productLine:'" + productLine + "' AND appName:'" + appName + "'");
    SearchResponse response = ElasticsearchClient.getClient()
            .prepareSearch(Constants.METADATA_INDEX)
            .setTypes(Constants.METADATA_TYPE)
            .setQuery(qb)
            .get();
    JSONObject jsonObject = JSON.parseObject(response.toString());
    JSONArray hitArray = (JSONArray) jsonObject.getJSONObject("hits").get("hits");
    if (hitArray.size() == 0) {
        throw new LogConsumerException("index does not exist,please check the configuration of the .logcenter index");
    }
    JSONObject document = (JSONObject) hitArray.get(0);
    String jsonStr = document.get("_source").toString();

    return JSONObject.parseObject(jsonStr, LogcenterConfig.class);
}
项目:elasticsearch    文件:App.java   
private static void deleteES(Client client) {
  BulkRequestBuilder bulkRequest = client.prepareBulk();
  SearchResponse response = client.prepareSearch(index).setTypes(type)
      .setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
      .setQuery(QueryBuilders.matchAllQuery())
      .setFrom(0).setSize(20).setExplain(true).execute().actionGet();
  System.out.println("length: " + response.getHits().getHits().length);
  if (response.getHits().getHits().length != 0) {
    for (SearchHit hit : response.getHits()) {
      String id = hit.getId();
      System.out.println("id: " + id);
      bulkRequest.add(client.prepareDelete(index, type, id).request());
    }
    BulkResponse bulkResponse = bulkRequest.get();
    if (bulkResponse.hasFailures()) {
      for (BulkItemResponse item : bulkResponse.getItems()) {
        System.out.println(item.getFailureMessage());
      }
    } else {
      System.out.println("delete ok");
    }
  } else {
    System.out.println("delete ok");
  }
}
项目:backside-servlet-ks    文件:ElasticQueryDSL.java   
/**
 * Return all hits which begin with <code>prefix</code>, e.g. "Wh"
 * @param prefix
 * @param fieldName
 * @param start
 * @param count
 * @return
 */
public String prefixTextQuerySingleField(String prefix, String fieldName, int start, int count) {
    QueryBuilder qb  = QueryBuilders.prefixQuery(fieldName, prefix);
    SearchSourceBuilder ssb = new SearchSourceBuilder();
    ssb.query(qb);
    ssb.from(start);
    if (count > -1)
        ssb.size(count);
    return ssb.toString();

}
项目:yacy_grid_mcp    文件:ElasticsearchClient.java   
public long count(final String index, final String histogram_timefield, final long millis) {
    try {
        SearchResponse response = elasticsearchClient.prepareSearch(index)
            .setSize(0)
            .setQuery(millis <= 0 ? QueryBuilders.constantScoreQuery(QueryBuilders.matchAllQuery()) : QueryBuilders.rangeQuery(histogram_timefield).from(new Date(System.currentTimeMillis() - millis)))
            .execute()
            .actionGet();
        return response.getHits().getTotalHits();
    } catch (Throwable e) {
        Data.logger.warn("", e);
        return 0;
    }
}
项目:yacy_grid_mcp    文件:ElasticsearchClient.java   
public List<Map<String, Object>> queryWithConstraints(final String indexName, final String fieldName, final String fieldValue, final Map<String, String> constraints, boolean latest) throws IOException {
    SearchRequestBuilder request = this.elasticsearchClient.prepareSearch(indexName)
            .setSearchType(SearchType.QUERY_THEN_FETCH)
            .setFrom(0);

    BoolQueryBuilder bFilter = QueryBuilders.boolQuery();
    bFilter.filter(QueryBuilders.constantScoreQuery(QueryBuilders.constantScoreQuery(QueryBuilders.termQuery(fieldName, fieldValue))));
    for (Object o : constraints.entrySet()) {
        @SuppressWarnings("rawtypes")
        Map.Entry entry = (Map.Entry) o;
        bFilter.filter(QueryBuilders.constantScoreQuery(QueryBuilders.termQuery((String) entry.getKey(), ((String) entry.getValue()).toLowerCase())));
    }
    request.setQuery(bFilter);

    // get response
    SearchResponse response = request.execute().actionGet();

    // evaluate search result
    ArrayList<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
    SearchHit[] hits = response.getHits().getHits();
    for (SearchHit hit: hits) {
        Map<String, Object> map = hit.getSourceAsMap();
        result.add(map);
    }

    return result;
}
项目:yacy_grid_mcp    文件:ElasticsearchHashMap.java   
@Override
public Set<Map.Entry<String, String>> entrySet() {
    Query query = this.elastic.query(this.index, QueryBuilders.matchAllQuery(), null, null, 0, 0, Integer.MAX_VALUE, 0);
    List<Map<String, Object>> result = query.result;
    Set<Map.Entry<String, String>> set = new HashSet<>();
    for (Map<String, Object> r: result) {
        set.add(new AbstractMap.SimpleEntry<>((String) r.get("key"), (String) r.get("value")));
    }
    return set;
}
项目:es-sql    文件:QueryMaker.java   
public static BoolQueryBuilder explan(Where where,boolean isQuery) throws SqlParseException {
    BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
    while (where.getWheres().size() == 1) {
        where = where.getWheres().getFirst();
    }
    new QueryMaker().explanWhere(boolQuery, where);
    if(isQuery){
        return boolQuery;
    }
    return QueryBuilders.boolQuery().filter(boolQuery);
}
项目:es-crud    文件:ElasticSearchCrudImpl.java   
/**
 * This method Search the available Document
 */
@Override
public void searchDocument() {
    SearchHits hits = null;
    try {
        client = ESclient.getInstant();
        SearchResponse response = client.prepareSearch("school", "college")
                .setTypes("tenth", "be")
                .setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
                .setQuery(QueryBuilders.termQuery("name", "sundar"))
                .setPostFilter(QueryBuilders.rangeQuery("age").from(15).to(24))
                .setFrom(0).setSize(60).setExplain(true)
                .get();

        if (response != null) {
            hits = response.getHits();
        }
        if (hits != null) {

            while (hits.iterator().hasNext()) {
                hits.iterator().next();
            }
        }
    } catch (Exception ex) {
        log.error("Excption occurred while Search Document : " + ex);
    }
}
项目:Elasticsearch-Tutorial-zh-CN    文件:BaseDemo.java   
/**
 * 查询 match 多个字段
 *
 * @param transportClient
 * @throws IOException
 */
private static void queryByMatchMoreParam(TransportClient transportClient) throws IOException {
    SearchResponse searchResponse = transportClient.prepareSearch("product_index").setTypes("product")
            .setQuery(QueryBuilders.multiMatchQuery("飞利浦", "product_name", "product_desc"))
            .get();

    for (SearchHit searchHit : searchResponse.getHits().getHits()) {
        logger.info("--------------------------------:" + searchHit.getSourceAsString());
    }
}
项目:elasticsearch_my    文件:ChildQuerySearchIT.java   
public void testHasChildInnerQueryType() {
    assertAcked(prepareCreate("test").addMapping("parent-type").addMapping("child-type", "_parent", "type=parent-type"));
    client().prepareIndex("test", "child-type", "child-id").setParent("parent-id").setSource("{}", XContentType.JSON).get();
    client().prepareIndex("test", "parent-type", "parent-id").setSource("{}", XContentType.JSON).get();
    refresh();
    //make sure that when we explicitly set a type, the inner query is executed in the context of the child type instead
    SearchResponse searchResponse = client().prepareSearch("test").setTypes("parent-type").setQuery(
            QueryBuilders.hasChildQuery("child-type", new IdsQueryBuilder().addIds("child-id"), ScoreMode.None)).get();
    assertSearchHits(searchResponse, "parent-id");
}