Search in sources :

Example 61 with QueryBuilder

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.index.query.QueryBuilder in project syncope by apache.

the class ElasticsearchAnySearchDAO method fillAttrQuery.

private QueryBuilder fillAttrQuery(final PlainSchema schema, final PlainAttrValue attrValue, final AttributeCond cond) {
    Object value = schema.getType() == AttrSchemaType.Date && attrValue.getDateValue() != null ? attrValue.getDateValue().getTime() : attrValue.getValue();
    QueryBuilder builder = EMPTY_QUERY_BUILDER;
    switch(cond.getType()) {
        case ISNOTNULL:
            builder = QueryBuilders.existsQuery(schema.getKey());
            break;
        case ISNULL:
            builder = QueryBuilders.boolQuery().mustNot(QueryBuilders.existsQuery(schema.getKey()));
            break;
        case ILIKE:
            builder = QueryBuilders.queryStringQuery(schema.getKey() + ":" + cond.getExpression().replace('%', '*').toLowerCase());
            break;
        case LIKE:
            builder = QueryBuilders.wildcardQuery(schema.getKey(), cond.getExpression().replace('%', '*'));
            break;
        case IEQ:
            builder = QueryBuilders.matchQuery(schema.getKey(), cond.getExpression().toLowerCase());
            break;
        case EQ:
            builder = QueryBuilders.termQuery(schema.getKey(), value);
            break;
        case GE:
            builder = QueryBuilders.rangeQuery(schema.getKey()).gte(value);
            break;
        case GT:
            builder = QueryBuilders.rangeQuery(schema.getKey()).gt(value);
            break;
        case LE:
            builder = QueryBuilders.rangeQuery(schema.getKey()).lte(value);
            break;
        case LT:
            builder = QueryBuilders.rangeQuery(schema.getKey()).lt(value);
            break;
        default:
    }
    return builder;
}
Also used : MatchNoneQueryBuilder(org.elasticsearch.index.query.MatchNoneQueryBuilder) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) DisMaxQueryBuilder(org.elasticsearch.index.query.DisMaxQueryBuilder)

Example 62 with QueryBuilder

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.index.query.QueryBuilder in project incubator-sdap-mudrod by apache.

the class SessionGenerator method combineShortSessions.

public void combineShortSessions(ESDriver es, String user, int timeThres) throws ElasticsearchException, IOException {
    BoolQueryBuilder filterSearch = new BoolQueryBuilder();
    filterSearch.must(QueryBuilders.termQuery("IP", user));
    String[] indexArr = new String[] { logIndex };
    String[] typeArr = new String[] { cleanupType };
    int docCount = es.getDocCount(indexArr, typeArr, filterSearch);
    if (docCount < 3) {
        deleteInvalid(es, user);
        return;
    }
    BoolQueryBuilder filterCheck = new BoolQueryBuilder();
    filterCheck.must(QueryBuilders.termQuery("IP", user)).must(QueryBuilders.termQuery("Referer", "-"));
    SearchResponse checkReferer = es.getClient().prepareSearch(logIndex).setTypes(this.cleanupType).setScroll(new TimeValue(60000)).setQuery(filterCheck).setSize(0).execute().actionGet();
    long numInvalid = checkReferer.getHits().getTotalHits();
    double invalidRate = numInvalid / docCount;
    if (invalidRate >= 0.8) {
        deleteInvalid(es, user);
        return;
    }
    StatsAggregationBuilder statsAgg = AggregationBuilders.stats("Stats").field("Time");
    SearchResponse srSession = es.getClient().prepareSearch(logIndex).setTypes(this.cleanupType).setScroll(new TimeValue(60000)).setQuery(filterSearch).addAggregation(AggregationBuilders.terms("Sessions").field("SessionID").size(docCount).subAggregation(statsAgg)).execute().actionGet();
    Terms sessions = srSession.getAggregations().get("Sessions");
    List<Session> sessionList = new ArrayList<>();
    for (Terms.Bucket session : sessions.getBuckets()) {
        Stats agg = session.getAggregations().get("Stats");
        Session sess = new Session(props, es, agg.getMinAsString(), agg.getMaxAsString(), session.getKey().toString());
        sessionList.add(sess);
    }
    Collections.sort(sessionList);
    DateTimeFormatter fmt = ISODateTimeFormat.dateTime();
    String last = null;
    String lastnewID = null;
    String lastoldID = null;
    String current = null;
    for (Session s : sessionList) {
        current = s.getEndTime();
        if (last != null) {
            if (Seconds.secondsBetween(fmt.parseDateTime(last), fmt.parseDateTime(current)).getSeconds() < timeThres) {
                if (lastnewID == null) {
                    s.setNewID(lastoldID);
                } else {
                    s.setNewID(lastnewID);
                }
                QueryBuilder fs = QueryBuilders.boolQuery().filter(QueryBuilders.termQuery("SessionID", s.getID()));
                SearchResponse scrollResp = es.getClient().prepareSearch(logIndex).setTypes(this.cleanupType).setScroll(new TimeValue(60000)).setQuery(fs).setSize(100).execute().actionGet();
                while (true) {
                    for (SearchHit hit : scrollResp.getHits().getHits()) {
                        if (lastnewID == null) {
                            update(es, logIndex, this.cleanupType, hit.getId(), "SessionID", lastoldID);
                        } else {
                            update(es, logIndex, this.cleanupType, hit.getId(), "SessionID", lastnewID);
                        }
                    }
                    scrollResp = es.getClient().prepareSearchScroll(scrollResp.getScrollId()).setScroll(new TimeValue(600000)).execute().actionGet();
                    if (scrollResp.getHits().getHits().length == 0) {
                        break;
                    }
                }
            }
        }
        lastoldID = s.getID();
        lastnewID = s.getNewID();
        last = current;
    }
}
Also used : StatsAggregationBuilder(org.elasticsearch.search.aggregations.metrics.stats.StatsAggregationBuilder) SearchHit(org.elasticsearch.search.SearchHit) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) Stats(org.elasticsearch.search.aggregations.metrics.stats.Stats) DateTimeFormatter(org.joda.time.format.DateTimeFormatter) TimeValue(org.elasticsearch.common.unit.TimeValue) Session(org.apache.sdap.mudrod.weblog.structure.Session)

Example 63 with QueryBuilder

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.index.query.QueryBuilder in project incubator-sdap-mudrod by apache.

the class ESDriver method searchByQuery.

@SuppressWarnings("unchecked")
public String searchByQuery(String index, String type, String query, Boolean bDetail) throws IOException, InterruptedException, ExecutionException {
    boolean exists = getClient().admin().indices().prepareExists(index).execute().actionGet().isExists();
    if (!exists) {
        return null;
    }
    QueryBuilder qb = QueryBuilders.queryStringQuery(query);
    SearchResponse response = getClient().prepareSearch(index).setTypes(type).setQuery(qb).setSize(500).execute().actionGet();
    // Map of K,V pairs where key is the field name from search result and value is the that should be returned for that field. Not always the same.
    Map<String, String> fieldsToReturn = new HashMap<>();
    fieldsToReturn.put("Dataset-ShortName", "Short Name");
    fieldsToReturn.put("Dataset-LongName", "Long Name");
    fieldsToReturn.put("DatasetParameter-Topic", "Topic");
    fieldsToReturn.put("Dataset-Description", "Dataset-Description");
    fieldsToReturn.put("DatasetCitation-ReleaseDateLong", "Release Date");
    if (bDetail) {
        fieldsToReturn.put("DatasetPolicy-DataFormat", "DataFormat");
        fieldsToReturn.put("Dataset-Doi", "Dataset-Doi");
        fieldsToReturn.put("Dataset-ProcessingLevel", "Processing Level");
        fieldsToReturn.put("DatasetCitation-Version", "Version");
        fieldsToReturn.put("DatasetSource-Sensor-ShortName", "DatasetSource-Sensor-ShortName");
        fieldsToReturn.put("DatasetProject-Project-ShortName", "DatasetProject-Project-ShortName");
        fieldsToReturn.put("DatasetParameter-Category", "DatasetParameter-Category");
        fieldsToReturn.put("DatasetLocationPolicy-BasePath", "DatasetLocationPolicy-BasePath");
        fieldsToReturn.put("DatasetParameter-Variable-Full", "DatasetParameter-Variable-Full");
        fieldsToReturn.put("DatasetParameter-Term-Full", "DatasetParameter-Term-Full");
        fieldsToReturn.put("DatasetParameter-VariableDetail", "DatasetParameter-VariableDetail");
        fieldsToReturn.put("DatasetRegion-Region", "Region");
        fieldsToReturn.put("DatasetCoverage-NorthLat", "NorthLat");
        fieldsToReturn.put("DatasetCoverage-SouthLat", "SouthLat");
        fieldsToReturn.put("DatasetCoverage-WestLon", "WestLon");
        fieldsToReturn.put("DatasetCoverage-EastLon", "EastLon");
        fieldsToReturn.put("DatasetCoverage-StartTimeLong-Long", "DatasetCoverage-StartTimeLong-Long");
        fieldsToReturn.put("Dataset-DatasetCoverage-StopTimeLong", "Dataset-DatasetCoverage-StopTimeLong");
        fieldsToReturn.put("Dataset-TemporalResolution", "Dataset-TemporalResolution");
        fieldsToReturn.put("Dataset-TemporalRepeat", "Dataset-TemporalRepeat");
        fieldsToReturn.put("Dataset-LatitudeResolution", "Dataset-LatitudeResolution");
        fieldsToReturn.put("Dataset-LongitudeResolution", "Dataset-LongitudeResolution");
        fieldsToReturn.put("Dataset-AcrossTrackResolution", "Dataset-AcrossTrackResolution");
        fieldsToReturn.put("Dataset-AlongTrackResolution", "Dataset-AlongTrackResolution");
    }
    List<Map<String, Object>> searchResults = new ArrayList<>();
    for (SearchHit hit : response.getHits().getHits()) {
        Map<String, Object> source = hit.getSource();
        Map<String, Object> searchResult = source.entrySet().stream().filter(entry -> fieldsToReturn.keySet().contains(entry.getKey())).collect(Collectors.toMap(entry -> fieldsToReturn.get(entry.getKey()), Entry::getValue));
        // searchResult is now a map where the key = value from fieldsToReturn and the value = value from search result
        // Some results require special handling/formatting:
        // Release Date formatting
        LocalDate releaseDate = Instant.ofEpochMilli(Long.parseLong(((ArrayList<String>) searchResult.get("Release Date")).get(0))).atZone(ZoneId.of("Z")).toLocalDate();
        searchResult.put("Release Date", releaseDate.format(DateTimeFormatter.ISO_DATE));
        if (bDetail) {
            // DataFormat value, translate RAW to BINARY
            if ("RAW".equals(searchResult.get("DataFormat"))) {
                searchResult.put("DataFormat", "BINARY");
            }
            // DatasetLocationPolicy-BasePath Should only contain ftp, http, or https URLs
            List<String> urls = ((List<String>) searchResult.get("DatasetLocationPolicy-BasePath")).stream().filter(url -> url.startsWith("ftp") || url.startsWith("http")).collect(Collectors.toList());
            searchResult.put("DatasetLocationPolicy-BasePath", urls);
            // Time Span Formatting
            LocalDate startDate = Instant.ofEpochMilli((Long) searchResult.get("DatasetCoverage-StartTimeLong-Long")).atZone(ZoneId.of("Z")).toLocalDate();
            LocalDate endDate = "".equals(searchResult.get("Dataset-DatasetCoverage-StopTimeLong")) ? null : Instant.ofEpochMilli(Long.parseLong(searchResult.get("Dataset-DatasetCoverage-StopTimeLong").toString())).atZone(ZoneId.of("Z")).toLocalDate();
            searchResult.put("Time Span", startDate.format(DateTimeFormatter.ISO_DATE) + " to " + (endDate == null ? "Present" : endDate.format(DateTimeFormatter.ISO_DATE)));
            // Temporal resolution can come from one of two fields
            searchResult.put("TemporalResolution", "".equals(searchResult.get("Dataset-TemporalResolution")) ? searchResult.get("Dataset-TemporalRepeat") : searchResult.get("Dataset-TemporalResolution"));
            // Special formatting for spatial resolution
            String latResolution = (String) searchResult.get("Dataset-LatitudeResolution");
            String lonResolution = (String) searchResult.get("Dataset-LongitudeResolution");
            if (!latResolution.isEmpty() && !lonResolution.isEmpty()) {
                searchResult.put("SpatialResolution", latResolution + " degrees (latitude) x " + lonResolution + " degrees (longitude)");
            } else {
                String acrossResolution = (String) searchResult.get("Dataset-AcrossTrackResolution");
                String alonResolution = (String) searchResult.get("Dataset-AlongTrackResolution");
                double dAcrossResolution = Double.parseDouble(acrossResolution) / 1000;
                double dAlonResolution = Double.parseDouble(alonResolution) / 1000;
                searchResult.put("SpatialResolution", dAlonResolution + " km (Along) x " + dAcrossResolution + " km (Across)");
            }
            // Measurement is a list of hierarchies that goes Topic -> Term -> Variable -> Variable Detail. Need to construct these hierarchies.
            List<List<String>> measurements = buildMeasurementHierarchies((List<String>) searchResult.get("Topic"), (List<String>) searchResult.get("DatasetParameter-Term-Full"), (List<String>) searchResult.get("DatasetParameter-Variable-Full"), (List<String>) searchResult.get("DatasetParameter-VariableDetail"));
            searchResult.put("Measurements", measurements);
        }
        searchResults.add(searchResult);
    }
    Map<String, List<?>> pdResults = new HashMap<>();
    pdResults.put("PDResults", searchResults);
    return new GsonBuilder().create().toJson(pdResults);
}
Also used : StringUtils(org.apache.commons.lang.StringUtils) ByteSizeUnit(org.elasticsearch.common.unit.ByteSizeUnit) MudrodConstants(org.apache.sdap.mudrod.main.MudrodConstants) MappingMetaData(org.elasticsearch.cluster.metadata.MappingMetaData) GetMappingsRequest(org.elasticsearch.action.admin.indices.mapping.get.GetMappingsRequest) LoggerFactory(org.slf4j.LoggerFactory) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) QueryBuilders(org.elasticsearch.index.query.QueryBuilders) GsonBuilder(com.google.gson.GsonBuilder) InetAddress(java.net.InetAddress) ObjectObjectCursor(com.carrotsearch.hppc.cursors.ObjectObjectCursor) DeleteRequest(org.elasticsearch.action.delete.DeleteRequest) MudrodEngine(org.apache.sdap.mudrod.main.MudrodEngine) Fuzziness(org.elasticsearch.common.unit.Fuzziness) Settings(org.elasticsearch.common.settings.Settings) CompletionSuggestionBuilder(org.elasticsearch.search.suggest.completion.CompletionSuggestionBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse) XContentFactory.jsonBuilder(org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder) ByteSizeValue(org.elasticsearch.common.unit.ByteSizeValue) SearchHit(org.elasticsearch.search.SearchHit) SearchType(org.elasticsearch.action.search.SearchType) InetSocketTransportAddress(org.elasticsearch.common.transport.InetSocketTransportAddress) AnalyzeToken(org.elasticsearch.action.admin.indices.analyze.AnalyzeResponse.AnalyzeToken) BulkResponse(org.elasticsearch.action.bulk.BulkResponse) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) ZoneId(java.time.ZoneId) Serializable(java.io.Serializable) LocalDate(java.time.LocalDate) SuggestBuilders(org.elasticsearch.search.suggest.SuggestBuilders) Entry(java.util.Map.Entry) MatchAllQueryBuilder(org.elasticsearch.index.query.MatchAllQueryBuilder) java.util(java.util) ImmutableOpenMap(org.elasticsearch.common.collect.ImmutableOpenMap) Suggest(org.elasticsearch.search.suggest.Suggest) GetMappingsResponse(org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse) TransportClient(org.elasticsearch.client.transport.TransportClient) BackoffPolicy(org.elasticsearch.action.bulk.BackoffPolicy) TimeValue(org.elasticsearch.common.unit.TimeValue) Node(org.elasticsearch.node.Node) ESTransportClient(org.apache.sdap.mudrod.utils.ESTransportClient) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) Logger(org.slf4j.Logger) GetIndexRequest(org.elasticsearch.action.admin.indices.get.GetIndexRequest) Client(org.elasticsearch.client.Client) AnalyzeResponse(org.elasticsearch.action.admin.indices.analyze.AnalyzeResponse) UpdateRequest(org.elasticsearch.action.update.UpdateRequest) IOException(java.io.IOException) SuggestBuilder(org.elasticsearch.search.suggest.SuggestBuilder) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) DateTimeFormatter(java.time.format.DateTimeFormatter) BulkRequest(org.elasticsearch.action.bulk.BulkRequest) BulkProcessor(org.elasticsearch.action.bulk.BulkProcessor) SearchHit(org.elasticsearch.search.SearchHit) GsonBuilder(com.google.gson.GsonBuilder) MatchAllQueryBuilder(org.elasticsearch.index.query.MatchAllQueryBuilder) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) LocalDate(java.time.LocalDate) SearchResponse(org.elasticsearch.action.search.SearchResponse) ImmutableOpenMap(org.elasticsearch.common.collect.ImmutableOpenMap)

Example 64 with QueryBuilder

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.index.query.QueryBuilder in project ff4j by ff4j.

the class ElasticQueryBuilder method queryGetEventQueryDefinition.

public Search queryGetEventQueryDefinition(EventQueryDefinition query, String action) {
    BoolQueryBuilder booleanQuery = new BoolQueryBuilder();
    // Optional constant for action filter
    if (action != null) {
        query.getActionFilters().add(action);
    }
    QueryBuilder typeQuery = QueryBuilders.termQuery("type", EventConstants.TARGET_FEATURE);
    // Timestamp filter
    RangeQueryBuilder timestampFilter = // 
    QueryBuilders.rangeQuery("timestamp").gt(// 
    query.getFrom().longValue()).lt(// 
    query.getTo().longValue()).includeLower(// 
    false).includeUpper(false);
    booleanQuery.must(typeQuery);
    booleanQuery.must(timestampFilter);
    // Optional filters
    addOptionalFilters(booleanQuery, query.getActionFilters(), "action");
    addOptionalFilters(booleanQuery, query.getHostFilters(), "hostName");
    addOptionalFilters(booleanQuery, query.getNamesFilter(), "name");
    addOptionalFilters(booleanQuery, query.getSourceFilters(), "source");
    // Warning : default size is set to 10 results, that's why it's
    // overridden
    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder().size(100);
    Search searchQuery = // 
    new Search.Builder(searchSourceBuilder.query(booleanQuery.toString()).toString()).addIndex(// 
    connection.getIndexName()).addType(// 
    ElasticConstants.TYPE_EVENT).build();
    return searchQuery;
}
Also used : BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) Search(io.searchbox.core.Search) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) RangeQueryBuilder(org.elasticsearch.index.query.RangeQueryBuilder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) RangeQueryBuilder(org.elasticsearch.index.query.RangeQueryBuilder) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder)

Example 65 with QueryBuilder

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.index.query.QueryBuilder in project tutorials by eugenp.

the class GeoQueriesTest method givenGeoPointData_whenExecutedGeoPolygonQuery_thenResultNonEmpty.

@Test
public void givenGeoPointData_whenExecutedGeoPolygonQuery_thenResultNonEmpty() {
    String jsonObject = "{\"name\":\"The Great Rann of Kutch\",\"location\":[69.859741,23.733732]}";
    IndexResponse response = client.prepareIndex(WONDERS_OF_WORLD, WONDERS).setSource(jsonObject).get();
    String greatRannOfKutchid = response.getId();
    client.admin().indices().prepareRefresh(WONDERS_OF_WORLD).get();
    QueryBuilder qb = QueryBuilders.geoPolygonQuery("location").addPoint(22.733, 68.859).addPoint(24.733, 68.859).addPoint(23, 70.859);
    SearchResponse searchResponse = client.prepareSearch(WONDERS_OF_WORLD).setTypes(WONDERS).setQuery(qb).execute().actionGet();
    List<String> ids = Arrays.stream(searchResponse.getHits().getHits()).map(SearchHit::getId).collect(Collectors.toList());
    assertTrue(ids.contains(greatRannOfKutchid));
}
Also used : IndexResponse(org.elasticsearch.action.index.IndexResponse) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse) Test(org.junit.Test)

Aggregations

QueryBuilder (org.elasticsearch.index.query.QueryBuilder)371 BoolQueryBuilder (org.elasticsearch.index.query.BoolQueryBuilder)245 Test (org.testng.annotations.Test)156 DynamicEntity (org.molgenis.data.support.DynamicEntity)137 SearchResponse (org.elasticsearch.action.search.SearchResponse)58 QueryImpl (org.molgenis.data.support.QueryImpl)36 Map (java.util.Map)32 ArrayList (java.util.ArrayList)30 Test (org.junit.Test)30 SearchSourceBuilder (org.elasticsearch.search.builder.SearchSourceBuilder)29 HashMap (java.util.HashMap)27 MatchAllQueryBuilder (org.elasticsearch.index.query.MatchAllQueryBuilder)27 RangeQueryBuilder (org.elasticsearch.index.query.RangeQueryBuilder)27 IOException (java.io.IOException)23 SearchHit (org.elasticsearch.search.SearchHit)22 AbstractQueryBuilder (org.elasticsearch.index.query.AbstractQueryBuilder)19 TermQueryBuilder (org.elasticsearch.index.query.TermQueryBuilder)19 List (java.util.List)17 SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)17 IndexRequestBuilder (org.elasticsearch.action.index.IndexRequestBuilder)15