use of com.bakdata.conquery.util.search.QuickSearch in project conquery by bakdata.
the class FilterSearch method createSourceSearch.
/**
* Create interactive Search for the selected filter based on its Template.
* @param filter
*/
public static void createSourceSearch(AbstractSelectFilter<?> filter, CSVConfig parserConfig) {
FilterTemplate template = filter.getTemplate();
List<String> templateColumns = new ArrayList<>(template.getColumns());
templateColumns.add(template.getColumnValue());
File file = new File(template.getFilePath());
String autocompleteKey = String.join("_", templateColumns) + "_" + file.getName();
QuickSearch<FilterSearchItem> search = FilterSearch.search.get(autocompleteKey);
if (search != null) {
log.info("Reference list '{}' already exists ...", file.getAbsolutePath());
filter.setSourceSearch(search);
return;
}
log.info("Processing reference list '{}' ...", file.getAbsolutePath());
final long time = System.currentTimeMillis();
search = new QuickSearch.QuickSearchBuilder().withUnmatchedPolicy(QuickSearch.UnmatchedPolicy.IGNORE).withMergePolicy(QuickSearch.MergePolicy.UNION).withKeywordMatchScorer(FilterSearchType.CONTAINS::score).build();
final CsvParser parser = parserConfig.createParser();
try {
IterableResult<String[], ParsingContext> it = parser.iterate(In.file(file).withUTF8().asReader());
String[] header = it.getContext().parsedHeaders();
for (String[] row : it) {
FilterSearchItem item = new FilterSearchItem();
for (int i = 0; i < header.length; i++) {
String column = header[i];
if (!templateColumns.contains(column)) {
continue;
}
item.setLabel(template.getValue());
item.setOptionValue(template.getOptionValue());
item.getTemplateValues().put(column, row[i]);
if (column.equals(template.getColumnValue())) {
item.setValue(row[i]);
}
search.addItem(item, row[i]);
}
}
filter.setSourceSearch(search);
FilterSearch.search.put(autocompleteKey, search);
final long duration = System.currentTimeMillis() - time;
log.info("Processed reference list '{}' in {} ms ({} Items in {} Lines)", file.getAbsolutePath(), duration, search.getStats().getItems(), it.getContext().currentLine());
} catch (Exception e) {
log.error("Failed to process reference list '" + file.getAbsolutePath() + "'", e);
} finally {
parser.stopParsing();
}
}
use of com.bakdata.conquery.util.search.QuickSearch in project conquery by bakdata.
the class ConceptsProcessor method autocompleteTextFilter.
/**
* Autocompletion for search terms. For values of {@link AbstractSelectFilter<?>}.
* Is used by the serach cache to load missing items
*/
private static List<FEValue> autocompleteTextFilter(AbstractSelectFilter<?> filter, String text) {
if (Strings.isNullOrEmpty(text)) {
// If no text provided, we just list them
// Filter might not have a source search (since none might be defined).
// TODO unify these code paths, they are quite the mess, maybe also create source search for key-value also
final Stream<FEValue> fromSearch = filter.getSourceSearch() == null ? Stream.empty() : filter.getSourceSearch().listItems().stream().map(item -> new FEValue(item.getLabel(), item.getValue(), item.getTemplateValues(), item.getOptionValue()));
final Stream<FEValue> fromLabels = filter.getLabels().entrySet().stream().map(entry -> new FEValue(entry.getValue(), entry.getKey()));
return Stream.concat(fromLabels, fromSearch).sorted().collect(Collectors.toList());
}
List<FEValue> result = new LinkedList<>();
QuickSearch<FilterSearchItem> search = filter.getSourceSearch();
if (search != null) {
result = createSourceSearchResult(filter.getSourceSearch(), Collections.singletonList(text), OptionalInt.empty(), FilterSearch.FilterSearchType.CONTAINS::score);
}
String value = filter.getValueFor(text);
if (value != null) {
result.add(new FEValue(text, value));
}
return result;
}
Aggregations