org.elasticsearch.index.query.QueryBuilder Java Examples

The following examples show how to use org.elasticsearch.index.query.QueryBuilder. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: QuickReplyRepositoryImpl.java    From youkefu with Apache License 2.0 8 votes vote down vote up
@SuppressWarnings("deprecation")
@Override
public Page<QuickReply> getByCon(BoolQueryBuilder boolQueryBuilder, final int p , final int ps) {

	Page<QuickReply> pages  = null ;
	
	QueryBuilder beginFilter = QueryBuilders.boolQuery().should(QueryBuilders.missingQuery("begintime")).should(QueryBuilders.rangeQuery("begintime").from(new Date().getTime())) ;
	QueryBuilder endFilter = QueryBuilders.boolQuery().should(QueryBuilders.missingQuery("endtime")).should(QueryBuilders.rangeQuery("endtime").to(new Date().getTime())) ;
	
    NativeSearchQueryBuilder searchQueryBuilder = new NativeSearchQueryBuilder().withQuery(boolQueryBuilder).withFilter(QueryBuilders.boolQuery().must(beginFilter).must(endFilter)).withSort(new FieldSortBuilder("createtime").unmappedType("date").order(SortOrder.DESC));
    
    SearchQuery searchQuery = searchQueryBuilder.build().setPageable(new PageRequest(p, ps)) ;
    if(elasticsearchTemplate.indexExists(QuickReply.class)){
    	pages = elasticsearchTemplate.queryForPage(searchQuery, QuickReply.class);
    }
    return pages ; 
}
 
Example #2
Source File: FilterJoinBenchmark.java    From siren-join with GNU Affero General Public License v3.0 6 votes vote down vote up
public long runQuery(String name, int testNum, String index, long expectedHits, QueryBuilder query) {
    SearchResponse searchResponse = new CoordinateSearchRequestBuilder(client)
    .setIndices(index)
            .setQuery(query)
            .execute().actionGet();

    if (searchResponse.getFailedShards() > 0) {
        log("Search Failures " + Arrays.toString(searchResponse.getShardFailures()));
    }

    long hits = searchResponse.getHits().totalHits();
    if (hits != expectedHits) {
        log("[" + name + "][#" + testNum + "] Hits Mismatch:  expected [" + expectedHits + "], got [" + hits + "]");
    }

    return searchResponse.getTookInMillis();
}
 
Example #3
Source File: KbsTopicRepositoryImpl.java    From youkefu with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("deprecation")
@Override
public Page<KbsTopic> getTopicByCon(BoolQueryBuilder boolQueryBuilder, final int p , final int ps) {

	Page<KbsTopic> pages  = null ;
	
	QueryBuilder beginFilter = QueryBuilders.boolQuery().should(QueryBuilders.missingQuery("begintime")).should(QueryBuilders.rangeQuery("begintime").from(new Date().getTime())) ;
	QueryBuilder endFilter = QueryBuilders.boolQuery().should(QueryBuilders.missingQuery("endtime")).should(QueryBuilders.rangeQuery("endtime").to(new Date().getTime())) ;
	
    NativeSearchQueryBuilder searchQueryBuilder = new NativeSearchQueryBuilder().withQuery(boolQueryBuilder).withFilter(QueryBuilders.boolQuery().must(beginFilter).must(endFilter)).withSort(new FieldSortBuilder("createtime").unmappedType("date").order(SortOrder.DESC));
    
    SearchQuery searchQuery = searchQueryBuilder.build().setPageable(new PageRequest(p, ps)) ;
    if(elasticsearchTemplate.indexExists(KbsTopic.class)){
    	pages = elasticsearchTemplate.queryForPage(searchQuery, KbsTopic.class);
    }
    return pages ; 
}
 
Example #4
Source File: ElasticsearchSearchDao.java    From metron with Apache License 2.0 6 votes vote down vote up
/**
 * Defers to a provided {@link org.elasticsearch.index.query.QueryBuilder} for the query.
 * @param groupRequest The request defining the parameters of the grouping
 * @param queryBuilder The actual query to be run. Intended for if the SearchRequest requires wrapping
 * @return The results of the query
 * @throws InvalidSearchException When the query is malformed or the current state doesn't allow search
 */
protected GroupResponse group(GroupRequest groupRequest, QueryBuilder queryBuilder)
    throws InvalidSearchException {
  org.elasticsearch.action.search.SearchRequest esRequest;
  org.elasticsearch.action.search.SearchResponse esResponse;

  if (client == null) {
    throw new InvalidSearchException("Uninitialized Dao!  You must call init() prior to use.");
  }
  if (groupRequest.getGroups() == null || groupRequest.getGroups().size() == 0) {
    throw new InvalidSearchException("At least 1 group must be provided.");
  }

  esRequest = buildGroupRequest(groupRequest, queryBuilder);
  esResponse = requestSubmitter.submitSearch(esRequest);
  GroupResponse response = buildGroupResponse(groupRequest, esResponse);

  return response;
}
 
Example #5
Source File: ProfileThreadSnapshotQueryEsDAO.java    From skywalking with Apache License 2.0 6 votes vote down vote up
@Override
public List<ProfileThreadSnapshotRecord> queryRecords(String segmentId, int minSequence, int maxSequence) throws IOException {
    // search traces
    SearchSourceBuilder sourceBuilder = SearchSourceBuilder.searchSource();

    BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
    sourceBuilder.query(boolQueryBuilder);
    List<QueryBuilder> mustQueryList = boolQueryBuilder.must();

    mustQueryList.add(QueryBuilders.termQuery(ProfileThreadSnapshotRecord.SEGMENT_ID, segmentId));
    mustQueryList.add(QueryBuilders.rangeQuery(ProfileThreadSnapshotRecord.SEQUENCE).gte(minSequence).lt(maxSequence));
    sourceBuilder.size(maxSequence - minSequence);

    SearchResponse response = getClient().search(ProfileThreadSnapshotRecord.INDEX_NAME, sourceBuilder);

    List<ProfileThreadSnapshotRecord> result = new ArrayList<>(maxSequence - minSequence);
    for (SearchHit searchHit : response.getHits().getHits()) {
        ProfileThreadSnapshotRecord record = builder.map2Data(searchHit.getSourceAsMap());

        result.add(record);
    }
    return result;
}
 
Example #6
Source File: OrientPyPiHostedHandlers.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Handle request for search.
 */
public Handler search() {
  return context -> {
    Payload payload = checkNotNull(context.getRequest().getPayload());
    try (InputStream is = payload.openInputStream()) {
      QueryBuilder query = parseSearchRequest(context.getRepository().getName(), is);
      List<PyPiSearchResult> results = new ArrayList<>();
      for (SearchHit hit : searchQueryService.browse(unrestricted(query))) {
        Map<String, Object> source = hit.getSource();
        Map<String, Object> formatAttributes = (Map<String, Object>) source.getOrDefault(
            MetadataNodeEntityAdapter.P_ATTRIBUTES, Collections.emptyMap());
        Map<String, Object> pypiAttributes = (Map<String, Object>) formatAttributes.getOrDefault(PyPiFormat.NAME,
            Collections.emptyMap());
        String name = Strings.nullToEmpty((String) pypiAttributes.get(PyPiAttributes.P_NAME));
        String version = Strings.nullToEmpty((String) pypiAttributes.get(PyPiAttributes.P_VERSION));
        String summary = Strings.nullToEmpty((String) pypiAttributes.get(PyPiAttributes.P_SUMMARY));
        results.add(new PyPiSearchResult(name, version, summary));
      }
      String response = buildSearchResponse(results);
      return HttpResponses.ok(new StringPayload(response, ContentTypes.APPLICATION_XML));
    }
  };
}
 
Example #7
Source File: PrefixLikeQueryParser.java    From elasticsearch-sql with MIT License 6 votes vote down vote up
@Override
public AtomicQuery parse(ElasticsearchParser.LikeClauseContext expression) {
    SqlConditionOperator operator = expression.not == null ? SqlConditionOperator.Like : SqlConditionOperator.NotLike;
    return parseCondition(expression, operator, null, (fieldName, operator1, rightParams) -> {
        String pattern = StringManager.removeStringSymbol(expression.pattern.getText());
        QueryBuilder prefixQuery = QueryBuilders.prefixQuery(fieldName, pattern);
        switch (operator1) {
            case Like: {
                return prefixQuery;
            }
            default:
            case NotLike: {
                return QueryBuilders.boolQuery().mustNot(prefixQuery);
            }
        }
    });
}
 
Example #8
Source File: EsQueryBuilder.java    From es-service-parent with Apache License 2.0 6 votes vote down vote up
/**
 * 前缀查询
 * 
 * @param boolQ
 * @param conditions
 * @param conditionType
 */
private void doPrefix(BoolQueryBuilder boolQ, List<Condition> conditions,
        ConditionType conditionType) {
    QueryBuilder prefix;
    BoolQueryBuilder subBoolQ;
    for (Condition condition : conditions) {
        if (condition.isSpaceSplit()) {
            subBoolQ = QueryBuilders.boolQuery();
            for (String word : KeyWordUtil.processKeyWord(condition.getValue())) {
                subBoolQ.should(QueryBuilders.prefixQuery(condition.getFiled(), word));
            }
            prefix = subBoolQ;
        } else {
            prefix = QueryBuilders.prefixQuery(condition.getFiled(), condition.getValue());
        }
        mergeBuilder(boolQ, prefix, conditionType);
    }
}
 
Example #9
Source File: MetadataBackendKV.java    From heroic with Apache License 2.0 6 votes vote down vote up
@Override
public AsyncFuture<CountSeries> countSeries(final CountSeries.Request filter) {
    return doto(c -> {
        final OptionalLimit limit = filter.getLimit();

        if (limit.isZero()) {
            return async.resolved(new CountSeries());
        }

        final QueryBuilder f = filter(filter.getFilter());

        SearchRequest request = c.getIndex().count(METADATA_TYPE);
        SearchSourceBuilder sourceBuilder = request.source();
        limit.asInteger().ifPresent(sourceBuilder::terminateAfter);
        sourceBuilder.query(new BoolQueryBuilder().must(f));

        final ResolvableFuture<SearchResponse> future = async.future();
        c.execute(request, bind(future));

        return future.directTransform(
            r -> new CountSeries(r.getHits().getTotalHits().value, false));

    });
}
 
Example #10
Source File: BsFileConfigCB.java    From fess with Apache License 2.0 6 votes vote down vote up
@Override
public SearchRequestBuilder build(SearchRequestBuilder builder) {
    if (_conditionQuery != null) {
        QueryBuilder queryBuilder = _conditionQuery.getQuery();
        if (queryBuilder != null) {
            builder.setQuery(queryBuilder);
        }
        _conditionQuery.getFieldSortBuilderList().forEach(sort -> {
            builder.addSort(sort);
        });
    }

    if (_conditionAggregation != null) {
        _conditionAggregation.getAggregationBuilderList().forEach(builder::addAggregation);
    }

    if (_specification != null) {
        builder.setFetchSource(_specification.columnList.toArray(new String[_specification.columnList.size()]), null);
    }

    return builder;
}
 
Example #11
Source File: ElasticSearchCleanupComponentBrowse.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public PagedResponse<Component> browseByPage(final CleanupPolicy policy,
                                             final Repository repository,
                                             final QueryOptions options)
{
  checkNotNull(options.getStart());
  checkNotNull(options.getLimit());

  StorageTx tx = UnitOfWork.currentTx();

  QueryBuilder query = convertPolicyToQuery(policy, options);

  log.debug("Searching for components to cleanup using policy {}", policy);

  SearchResponse searchResponse = invokeSearchByPage(policy, repository, options, query);

  List<Component> components = stream(searchResponse.getHits().spliterator(), false)
      .map(searchHit -> tx.findComponent(new DetachedEntityId(searchHit.getId())))
      .filter(Objects::nonNull)
      .collect(toList());

  return new PagedResponse<>(searchResponse.getHits().getTotalHits(), components);
}
 
Example #12
Source File: ElasticsearchHammingDistanceScoringStrategy.java    From vertexium with Apache License 2.0 6 votes vote down vote up
@Override
public QueryBuilder updateElasticsearchQuery(
    Graph graph,
    Elasticsearch5SearchIndex searchIndex,
    QueryBuilder query,
    QueryParameters queryParameters
) {
    List<String> fieldNames = getFieldNames(graph, searchIndex, queryParameters, getField());
    if (fieldNames == null) {
        return query;
    }

    HashMap<String, Object> scriptParams = new HashMap<>();
    scriptParams.put("hash", getHash());
    scriptParams.put("fieldNames", fieldNames);
    Script script = new Script(ScriptType.INLINE, "painless", scriptSrc, scriptParams);
    return QueryBuilders.functionScoreQuery(query, new ScriptScoreFunctionBuilder(script));
}
 
Example #13
Source File: ElasticsearchQueryUtilsTest.java    From aws-athena-query-federation with Apache License 2.0 6 votes vote down vote up
@Test
public void getRangePredicateTest()
{
    logger.info("getRangePredicateTest - enter");

    constraintsMap.put("year", SortedRangeSet.copyOf(Types.MinorType.INT.getType(),
            ImmutableList.of(
                    Range.lessThan(allocator, Types.MinorType.INT.getType(), 1950),
                    Range.equal(allocator, Types.MinorType.INT.getType(), 1952),
                    Range.range(allocator, Types.MinorType.INT.getType(),
                            1955, false, 1972, true),
                    Range.equal(allocator, Types.MinorType.INT.getType(), 1996),
                    Range.greaterThanOrEqual(allocator, Types.MinorType.INT.getType(), 2010)),
            false));
    String expectedPredicate = "(_exists_:year) AND year:((<1950) OR (>1955 AND <=1972) OR (>=2010) OR 1952 OR 1996)";

    // Get the actual predicate and compare to the expected one.
    QueryBuilder builder = ElasticsearchQueryUtils.getQuery(constraintsMap);
    String actualPredicate = builder.queryName();

    logger.info("Predicates - Expected: {}, Actual: {}", expectedPredicate, actualPredicate);
    assertEquals("Predicates do not match", expectedPredicate, actualPredicate);

    logger.info("getRangePredicateTest - exit");
}
 
Example #14
Source File: BsRoleCB.java    From fess with Apache License 2.0 6 votes vote down vote up
@Override
public SearchRequestBuilder build(SearchRequestBuilder builder) {
    if (_conditionQuery != null) {
        QueryBuilder queryBuilder = _conditionQuery.getQuery();
        if (queryBuilder != null) {
            builder.setQuery(queryBuilder);
        }
        _conditionQuery.getFieldSortBuilderList().forEach(sort -> {
            builder.addSort(sort);
        });
    }

    if (_conditionAggregation != null) {
        _conditionAggregation.getAggregationBuilderList().forEach(builder::addAggregation);
    }

    if (_specification != null) {
        builder.setFetchSource(_specification.columnList.toArray(new String[_specification.columnList.size()]), null);
    }

    return builder;
}
 
Example #15
Source File: QuotaQueryConverterTest.java    From james-project with Apache License 2.0 5 votes vote down vote up
@Test
void fromShouldReturnMatchAllWhenEmptyClauses() {
    QuotaQuery query = QuotaQuery.builder().build();
    QueryBuilder expected = matchAllQuery();

    QueryBuilder actual = testee.from(query);

    assertThat(actual).isEqualToComparingFieldByField(expected);
}
 
Example #16
Source File: QueryGeneratorIT.java    From molgenis with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
void generateOneQueryRuleEqualsCategorical() {
  String value = "id";
  Query<Entity> q = new QueryImpl<>().eq(categoricalAttrName, value);
  QueryBuilder query = queryGenerator.createQueryBuilder(q, entityType);
  QueryBuilder expectedQuery =
      constantScoreQuery(
          nestedQuery(
              categoricalAttrName,
              termQuery(categoricalAttrName + ".xid." + FIELD_NOT_ANALYZED, value),
              ScoreMode.Avg));
  assertQueryBuilderEquals(expectedQuery, query);
}
 
Example #17
Source File: ESConnection.java    From canal with Apache License 2.0 5 votes vote down vote up
public ESSearchRequest setQuery(QueryBuilder queryBuilder) {
    if (mode == ESClientMode.TRANSPORT) {
        searchRequestBuilder.setQuery(queryBuilder);
    } else {
        sourceBuilder.query(queryBuilder);
    }
    return this;
}
 
Example #18
Source File: QuestionElasticSearchIndexBuilder.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
/**
 * This is a new search that accepts additionalSearchInformation. We need it for our complex question searches.
 * We have duplicated the methods that need this parameter, like prepareSearchRequest
 */
public SearchResponse search(String searchTerms, List<String> references, List<String> siteIds, int start, int end, Map<String,String> additionalSearchInformation) {
    final Pair<SearchRequestBuilder,QueryBuilder> searchBuilders = prepareSearchRequest(searchTerms, references, siteIds, start, end, additionalSearchInformation);
    final SearchRequestBuilder searchRequestBuilder = searchBuilders.getLeft();
    final QueryBuilder queryBuilder = searchBuilders.getRight();

    getLog().debug("Search request from index builder [" + getName() + "]: " + searchRequestBuilder.toString());

    ValidateQueryRequest validateQueryRequest = new ValidateQueryRequest(indexName);
    QuerySourceBuilder querySourceBuilder = new QuerySourceBuilder().setQuery(queryBuilder);
    validateQueryRequest.source(querySourceBuilder);
    validateQueryRequest.explain(true);


    try {
        ActionFuture<ValidateQueryResponse> future = client.admin().indices().validateQuery(validateQueryRequest); // the client is org.elasticsearch.client.Client
        ValidateQueryResponse responseV = future.get(); // typical java future as response

        if (responseV.isValid()) {
            SearchResponse response = searchRequestBuilder.execute().actionGet();
            getLog().debug("Search request from index builder [" + getName() + "] took: " + response.getTook().format());
            eventTrackingService.post(eventTrackingService.newEvent(SearchService.EVENT_SEARCH,
                    SearchService.EVENT_SEARCH_REF + queryBuilder.toString(), true,
                    NotificationService.PREF_IMMEDIATE));

            return response;
        }else{
            return null;
        }
    }catch(Exception ex){
        return null;
    }

}
 
Example #19
Source File: QueryGeneratorIT.java    From molgenis with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
void generateOneQueryRuleGreaterLong() {
  Long value = 1L;
  Query<Entity> q = new QueryImpl<>().gt(longAttrName, value);
  QueryBuilder query = queryGenerator.createQueryBuilder(q, entityType);
  QueryBuilder expectedQuery = constantScoreQuery(rangeQuery(longAttrName).gt(value));
  assertQueryBuilderEquals(expectedQuery, query);
}
 
Example #20
Source File: ChnlThemeSearchService.java    From albert with MIT License 5 votes vote down vote up
protected QueryBuilder createQuery(ThemeSearchParam param){
//		BoolQueryBuilder builder = QueryBuilders.boolQuery();
		
//		boolean title = addTitleQuery(param.getTitle(), builder);
//		
//		if(title){
//			return builder;
//		}
		
		return QueryBuilders.matchAllQuery();
	}
 
Example #21
Source File: ESSearchServiceBuilder.java    From mapr-music with Apache License 2.0 5 votes vote down vote up
@Override
public JsonNode search(JsonNode jsonQuery) {

    if (jsonQuery == null) {
        throw new IllegalArgumentException("JSON Query can not be null");
    }

    if (this.client == null) {
        throw new IllegalStateException("ElasticSearch client is closed");
    }

    QueryBuilder query = QueryBuilders.wrapperQuery(jsonQuery.toString());

    SearchResponse response = client.prepareSearch(indexNames.toArray(new String[indexNames.size()]))
            .setTypes(typeNames.toArray(new String[typeNames.size()]))
            .setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
            .setQuery(query)
            .get();

    JsonNode result = null;
    try {
        result = mapper.readTree(response.toString());
    } catch (IOException e) {
        log.warn("Can not parse ES response '{}' as JSON. Exception: {}", response.toString(), e);
    }

    return result;
}
 
Example #22
Source File: ElasticSearchUtil.java    From ranger with Apache License 2.0 5 votes vote down vote up
public QueryBuilder setDateRange(String fieldName, Date fromDate, Date toDate) {
    RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery(fieldName).format(dateFormateStr);
    if (fromDate != null) {
        rangeQueryBuilder.from(dateFormat.format(fromDate));
    }
    if (toDate != null) {
        rangeQueryBuilder.to(dateFormat.format(toDate));
    }
    return rangeQueryBuilder;
}
 
Example #23
Source File: QueryGeneratorIT.java    From molgenis with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
void generateOneQueryRuleSearchOneFieldDate() throws ParseException {
  String value = "2015-05-22";
  Query<Entity> q = new QueryImpl<>().search(dateAttrName, value);
  QueryBuilder query = queryGenerator.createQueryBuilder(q, entityType);
  QueryBuilder expectedQuery = matchQuery(dateAttrName, value);
  assertQueryBuilderEquals(expectedQuery, query);
}
 
Example #24
Source File: ElasticsearchRepository.java    From staccato with Apache License 2.0 5 votes vote down vote up
protected String buildEsSearchString(Collection<String> indices, QueryBuilder queryBuilder,
                                   com.planet.staccato.dto.api.SearchRequest searchRequest) {
    int limit = QueryBuilderHelper.getLimit(searchRequest.getLimit());
    SearchSourceBuilder searchSourceBuilder =
            buildSearchSourceBuilder(queryBuilder, limit, searchRequest.getNext());
    configureSort(searchSourceBuilder, searchRequest.getSortby());
    setIncludeExcludeFields(searchSourceBuilder, searchRequest);

    SearchRequest esSearchRequest = buildSearchRequest(searchSourceBuilder, indices);
    String searchString = esSearchRequest.source().toString();

    log.debug("Searching ES indices '" + indices.toString() + "' with the following request: \n" + searchString);
    return searchString;
}
 
Example #25
Source File: ElasticSearchCleanupComponentBrowseTest.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
private void assertQuery(final String key, final String expectedQuery) {
  ArgumentCaptor<QueryBuilder> queryBuilderCaptor = ArgumentCaptor.forClass(QueryBuilder.class);
  verify(searchQueryService).browse(queryBuilderCaptor.capture());

  RepositoryQueryBuilder actualQuery = repositoryQuery(queryBuilderCaptor.getValue());

  assertThat(actualQuery.toString(), is(equalTo(expectedQuery)));
  assertThat(actualQuery.getRepositoryNames().iterator().next(), is(equalTo(REPO_NAME)));
}
 
Example #26
Source File: ElasticSearchUtilImpl.java    From metacat with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public <T> List<T> getQualifiedNamesByMarkerByNames(final String type,
                                                    final List<QualifiedName> qualifiedNames,
                                                    final Instant marker,
                                                    final List<QualifiedName> excludeQualifiedNames,
                                                    final Class<T> valueType) {
    final List<T> result = Lists.newArrayList();
    final List<String> names = qualifiedNames.stream().map(QualifiedName::toString).collect(Collectors.toList());
    final List<String> excludeNames = excludeQualifiedNames.stream().map(QualifiedName::toString)
        .collect(Collectors.toList());
    //
    // Run the query and get the response.
    final QueryBuilder queryBuilder = QueryBuilders.boolQuery()
        .must(QueryBuilders.termsQuery("name.qualifiedName.tree", names))
        .must(QueryBuilders.termQuery("deleted_", false))
        .must(QueryBuilders.rangeQuery(ElasticSearchDoc.Field.TIMESTAMP).lte(marker.getMillis()))
        .mustNot(QueryBuilders.termsQuery("name.qualifiedName.tree", excludeNames))
        .mustNot(QueryBuilders.termQuery("refreshMarker_", marker.toString()));
    final SearchRequestBuilder request = client.prepareSearch(esIndex)
        .setTypes(type)
        .setSearchType(SearchType.QUERY_THEN_FETCH)
        .setQuery(queryBuilder)
        .setSize(Integer.MAX_VALUE);
    final SearchResponse response = request.execute().actionGet(esCallTimeout);
    if (response.getHits().getHits().length != 0) {
        result.addAll(parseResponse(response, valueType));
    }
    return result;
}
 
Example #27
Source File: QueryClauseFuzzyMatchGenerator.java    From molgenis with GNU Lesser General Public License v3.0 5 votes vote down vote up
QueryBuilder mapQueryRule(QueryRule queryRule, EntityType entityType) {
  List<Attribute> attributePath =
      getAttributePathExpanded(queryRule.getField(), entityType, true);
  Attribute attr = attributePath.get(attributePath.size() - 1);
  Object queryValue = getQueryValue(attr, queryRule.getValue());

  String fieldName = getQueryFieldName(attributePath);
  AttributeType attrType = attr.getDataType();

  switch (attrType) {
    case DATE:
    case DATE_TIME:
    case DECIMAL:
    case EMAIL:
    case ENUM:
    case HTML:
    case HYPERLINK:
    case INT:
    case LONG:
    case SCRIPT:
    case STRING:
    case TEXT:
      return nestedQueryBuilder(
          entityType,
          attributePath,
          QueryBuilders.queryStringQuery(fieldName + ":(" + queryValue + ")"));
    case BOOL:
    case COMPOUND:
      throw new MolgenisQueryException(
          format(
              "Illegal data type [%s] for operator [%s]",
              attrType, QueryRule.Operator.FUZZY_MATCH));
    default:
      throw new UnexpectedEnumException(attrType);
  }
}
 
Example #28
Source File: MatchQueryFunction.java    From presto-connectors with Apache License 2.0 5 votes vote down vote up
@ScalarFunction("match_query")
@Description("es match_query")
@SqlType(StandardTypes.VARCHAR)
@SqlNullable
public static Slice matchQuery(
        @SqlType(StandardTypes.VARCHAR) Slice filter)
{
    if (filter == null) {
        return null;
    }
    String filterStr = filter.toStringUtf8();

    QueryBuilder builder = QueryBuilders.matchQuery(MATCH_COLUMN_SEP, filterStr);
    return Slices.utf8Slice(builder.toString());
}
 
Example #29
Source File: ElasticsearchRepository.java    From staccato with Apache License 2.0 5 votes vote down vote up
/**
 * Retries a specific item by index, providers and Id.
 *
 * @param indices Elasticsearch index.
 * @param id      Id for the desired entity.
 * @return A Map representation of the Entity.
 */
public Mono<Item> searchById(List<String> indices, String id) {
    QueryBuilder queryBuilder = QueryBuilders.termQuery(FieldName.ID, id);
    com.planet.staccato.dto.api.SearchRequest searchRequest = new com.planet.staccato.dto.api.SearchRequest()
            .limit(1)
            //.next("0")
            ;
    return searchItemFlux(indices, queryBuilder, searchRequest)
            .switchIfEmpty(Mono.error(new StaccatoRuntimeException("Item with ID '" + id + "' not matched.", 404    )))
            .single();
}
 
Example #30
Source File: ElasticsearchSearchVertexQuery.java    From vertexium with Apache License 2.0 5 votes vote down vote up
private QueryBuilder getDirectionOutEdgeFilter() {
    QueryBuilder outVertexIdFilter = QueryBuilders.termQuery(Elasticsearch7SearchIndex.OUT_VERTEX_ID_FIELD_NAME, sourceVertex.getId());
    if (otherVertexId != null) {
        QueryBuilder inVertexIdFilter = QueryBuilders.termQuery(Elasticsearch7SearchIndex.IN_VERTEX_ID_FIELD_NAME, otherVertexId);
        return QueryBuilders.boolQuery()
            .must(outVertexIdFilter)
            .must(inVertexIdFilter);
    }
    return outVertexIdFilter;
}