ExperimentRequestPreferencesSolrQueryFactory { public static SolrQuery createSolrQuery(String experimentAccession, BaselineRequestPreferences<?> reqPreferences) { AnalyticsCollectionProxy.AnalyticsSchemaField expressionLevelField = reqPreferences.getUnit() == ExpressionUnit.Absolute.Rna.FPKM ? EXPRESSION_LEVEL_FPKM : EXPRESSION_LEVEL; SolrQueryBuilder<AnalyticsCollectionProxy> solrQueryBuilder = new SolrQueryBuilder<>(); solrQueryBuilder .addFilterFieldByTerm(EXPERIMENT_ACCESSION, experimentAccession) .addFilterFieldByRangeMin(expressionLevelField, reqPreferences.getCutoff()); Optional<String> assayGroupIds = reqPreferences.getSelectedColumnIds().isEmpty() ? Optional.empty() : Optional.of( "(" + createOrBooleanQuery(ASSAY_GROUP_ID, reqPreferences.getSelectedColumnIds()) + ")"); Optional<String> geneQuery = reqPreferences.getGeneQuery().isEmpty() ? Optional.empty() : Optional.of( "(" + AnalyticsCollectionProxy.asAnalyticsGeneQuery(reqPreferences.getGeneQuery()).entrySet() .stream() .map(entry -> createOrBooleanQuery(entry.getKey(), entry.getValue())) .collect(joining(" OR ")) + ")"); String query = Stream.concat(mapToStream(assayGroupIds), mapToStream(geneQuery)).collect(joining(" AND ")); return solrQueryBuilder.build().setQuery(query.isEmpty() ? "*:*" : query); } protected  ExperimentRequestPreferencesSolrQueryFactory(); static SolrQuery createSolrQuery(String experimentAccession, BaselineRequestPreferences<?> reqPreferences);  }
@Test void testDefaultQuery() { RnaSeqBaselineRequestPreferences requestPreferences = new RnaSeqBaselineRequestPreferences(); SolrQuery solrQuery = ExperimentRequestPreferencesSolrQueryFactory.createSolrQuery(E_MTAB_513, requestPreferences); assertThat(solrQuery) .hasFieldOrPropertyWithValue( "filterQueries", new String[]{ "experiment_accession:(\"" + escapeQueryChars(E_MTAB_513) + "\")", "expression_level:[" + requestPreferences.getDefaultCutoff() + " TO *]"}) .hasFieldOrPropertyWithValue( "query", "(keyword_gene_biotype:(\"protein_coding\"))") .hasFieldOrPropertyWithValue( "fields", "*") .hasFieldOrPropertyWithValue( "rows", SolrQueryBuilder.DEFAULT_ROWS); }
@Test void testEmptyGeneQuery() { RnaSeqBaselineRequestPreferences requestPreferences = new RnaSeqBaselineRequestPreferences(); requestPreferences.setGeneQuery(SemanticQuery.create()); SolrQuery solrQuery = ExperimentRequestPreferencesSolrQueryFactory.createSolrQuery(E_MTAB_513, requestPreferences); assertThat(solrQuery) .hasFieldOrPropertyWithValue( "filterQueries", new String[]{ "experiment_accession:(\"" + escapeQueryChars(E_MTAB_513) + "\")", "expression_level:[" + requestPreferences.getDefaultCutoff() + " TO *]"}) .hasFieldOrPropertyWithValue( "query", "*:*") .hasFieldOrPropertyWithValue( "fields", "*") .hasFieldOrPropertyWithValue( "rows", SolrQueryBuilder.DEFAULT_ROWS); }
@Test void testQueriesAreJoinedWithAnd() { int numAssayGroups = ThreadLocalRandom.current().nextInt(1, 100); Set<String> assayGroups = new HashSet<>(numAssayGroups); while (assayGroups.size() < numAssayGroups) { assayGroups.add("g" + ThreadLocalRandom.current().nextInt(1, 1000)); } RnaSeqBaselineRequestPreferences requestPreferences = new RnaSeqBaselineRequestPreferences(); requestPreferences.setSelectedColumnIds(assayGroups); SolrQuery solrQuery = ExperimentRequestPreferencesSolrQueryFactory.createSolrQuery(E_MTAB_513, requestPreferences); List<String> explodedQuery = Arrays.asList(solrQuery.getQuery().split(" AND ")); assertThat(explodedQuery) .hasSize(2) .allMatch(query -> query.matches("\\(\\w+:.+\\)")); assertThat( explodedQuery.stream() .filter(query -> query.startsWith("(assay_group_id")) .findFirst() .orElse("") .split(":")[1].split(" OR ")) .hasSize(numAssayGroups); }