use of org.apache.maven.index.expr.SourcedSearchExpression in project archiva by apache.
the class MavenRepositorySearch method search.
/**
* @see RepositorySearch#search(String, SearchFields, SearchResultLimits)
*/
@SuppressWarnings("deprecation")
@Override
public SearchResults search(String principal, SearchFields searchFields, SearchResultLimits limits) throws RepositorySearchException {
if (searchFields.getRepositories() == null) {
throw new RepositorySearchException("Repositories cannot be null.");
}
List<String> indexingContextIds = addIndexingContexts(searchFields.getRepositories());
// olamy: IMHO doesn't make sense
if (!searchFields.getRepositories().isEmpty() && (indexingContextIds == null || indexingContextIds.isEmpty())) {
return new SearchResults();
}
BooleanQuery.Builder qb = new BooleanQuery.Builder();
if (StringUtils.isNotBlank(searchFields.getGroupId())) {
qb.add(indexer.constructQuery(MAVEN.GROUP_ID, searchFields.isExactSearch() ? new SourcedSearchExpression(searchFields.getGroupId()) : new UserInputSearchExpression(searchFields.getGroupId())), BooleanClause.Occur.MUST);
}
if (StringUtils.isNotBlank(searchFields.getArtifactId())) {
qb.add(indexer.constructQuery(MAVEN.ARTIFACT_ID, searchFields.isExactSearch() ? new SourcedSearchExpression(searchFields.getArtifactId()) : new UserInputSearchExpression(searchFields.getArtifactId())), BooleanClause.Occur.MUST);
}
if (StringUtils.isNotBlank(searchFields.getVersion())) {
qb.add(indexer.constructQuery(MAVEN.VERSION, searchFields.isExactSearch() ? new SourcedSearchExpression(searchFields.getVersion()) : new SourcedSearchExpression(searchFields.getVersion())), BooleanClause.Occur.MUST);
}
if (StringUtils.isNotBlank(searchFields.getPackaging())) {
qb.add(indexer.constructQuery(MAVEN.PACKAGING, searchFields.isExactSearch() ? new SourcedSearchExpression(searchFields.getPackaging()) : new UserInputSearchExpression(searchFields.getPackaging())), BooleanClause.Occur.MUST);
}
if (StringUtils.isNotBlank(searchFields.getClassName())) {
qb.add(indexer.constructQuery(MAVEN.CLASSNAMES, new UserInputSearchExpression(searchFields.getClassName())), BooleanClause.Occur.MUST);
}
if (StringUtils.isNotBlank(searchFields.getBundleSymbolicName())) {
qb.add(indexer.constructQuery(OSGI.SYMBOLIC_NAME, new UserInputSearchExpression(searchFields.getBundleSymbolicName())), BooleanClause.Occur.MUST);
}
if (StringUtils.isNotBlank(searchFields.getBundleVersion())) {
qb.add(indexer.constructQuery(OSGI.VERSION, new UserInputSearchExpression(searchFields.getBundleVersion())), BooleanClause.Occur.MUST);
}
if (StringUtils.isNotBlank(searchFields.getBundleExportPackage())) {
qb.add(indexer.constructQuery(OSGI.EXPORT_PACKAGE, new UserInputSearchExpression(searchFields.getBundleExportPackage())), Occur.MUST);
}
if (StringUtils.isNotBlank(searchFields.getBundleExportService())) {
qb.add(indexer.constructQuery(OSGI.EXPORT_SERVICE, new UserInputSearchExpression(searchFields.getBundleExportService())), Occur.MUST);
}
if (StringUtils.isNotBlank(searchFields.getBundleImportPackage())) {
qb.add(indexer.constructQuery(OSGI.IMPORT_PACKAGE, new UserInputSearchExpression(searchFields.getBundleImportPackage())), Occur.MUST);
}
if (StringUtils.isNotBlank(searchFields.getBundleName())) {
qb.add(indexer.constructQuery(OSGI.NAME, new UserInputSearchExpression(searchFields.getBundleName())), Occur.MUST);
}
if (StringUtils.isNotBlank(searchFields.getBundleImportPackage())) {
qb.add(indexer.constructQuery(OSGI.IMPORT_PACKAGE, new UserInputSearchExpression(searchFields.getBundleImportPackage())), Occur.MUST);
}
if (StringUtils.isNotBlank(searchFields.getBundleRequireBundle())) {
qb.add(indexer.constructQuery(OSGI.REQUIRE_BUNDLE, new UserInputSearchExpression(searchFields.getBundleRequireBundle())), Occur.MUST);
}
if (StringUtils.isNotBlank(searchFields.getClassifier())) {
qb.add(indexer.constructQuery(MAVEN.CLASSIFIER, searchFields.isExactSearch() ? new SourcedSearchExpression(searchFields.getClassifier()) : new UserInputSearchExpression(searchFields.getClassifier())), Occur.MUST);
} else if (searchFields.isExactSearch()) {
// TODO improvement in case of exact search and no classifier we must query for classifier with null value
// currently it's done in DefaultSearchService with some filtering
}
BooleanQuery qu = qb.build();
if (qu.clauses() == null || qu.clauses().size() <= 0) {
throw new RepositorySearchException("No search fields set.");
}
if (qu.clauses() != null) {
log.debug("CLAUSES ", qu.clauses());
for (BooleanClause cl : qu.clauses()) {
log.debug("Clause ", cl);
}
}
return search(limits, qu, indexingContextIds, Collections.<ArtifactInfoFilter>emptyList(), searchFields.getRepositories(), searchFields.isIncludePomArtifacts());
}
use of org.apache.maven.index.expr.SourcedSearchExpression in project archiva by apache.
the class ArchivaIndexingTaskExecutor method executeTask.
/**
* depending on current {@link Task} you have.
* If {@link org.apache.archiva.scheduler.indexing.ArtifactIndexingTask.Action#FINISH} && isExecuteOnEntireRepo:
* repository will be scanned.
*
* @param task
* @throws TaskExecutionException
*/
@Override
public void executeTask(Task task) throws TaskExecutionException {
ArtifactIndexingTask indexingTask = (ArtifactIndexingTask) task;
ManagedRepository repository = indexingTask.getRepository();
ArchivaIndexingContext archivaContext = indexingTask.getContext();
IndexingContext context = null;
try {
context = archivaContext.getBaseContext(IndexingContext.class);
} catch (UnsupportedBaseContextException e) {
throw new TaskExecutionException("Bad repository type.", e);
}
if (ArtifactIndexingTask.Action.FINISH.equals(indexingTask.getAction()) && indexingTask.isExecuteOnEntireRepo()) {
long start = System.currentTimeMillis();
try {
context.updateTimestamp();
DefaultScannerListener listener = new DefaultScannerListener(context, indexerEngine, true, null);
ScanningRequest request = new ScanningRequest(context, listener);
ScanningResult result = scanner.scan(request);
if (result.hasExceptions()) {
log.error("Exceptions occured during index scan of " + context.getId());
result.getExceptions().stream().map(e -> e.getMessage()).distinct().limit(5).forEach(s -> log.error("Message: " + s));
}
} catch (IOException e) {
log.error("Error during context scan {}: {}", context.getId(), context.getIndexDirectory());
}
long end = System.currentTimeMillis();
log.info("indexed maven repository: {}, onlyUpdate: {}, time {} ms", repository.getId(), indexingTask.isOnlyUpdate(), (end - start));
log.debug("Finishing indexing task on repo: {}", repository.getId());
finishIndexingTask(indexingTask, repository, context);
} else {
// create context if not a repo scan request
if (!indexingTask.isExecuteOnEntireRepo()) {
try {
//
log.debug(//
"Creating indexing context on resource: {}", (indexingTask.getResourceFile() == null ? "none" : indexingTask.getResourceFile()));
archivaContext = repository.getIndexingContext();
context = archivaContext.getBaseContext(IndexingContext.class);
} catch (UnsupportedBaseContextException e) {
log.error("Error occurred while creating context: {}", e.getMessage());
throw new TaskExecutionException("Error occurred while creating context: " + e.getMessage(), e);
}
}
if (context == null || context.getIndexDirectory() == null) {
throw new TaskExecutionException("Trying to index an artifact but the context is already closed");
}
try {
Path artifactFile = indexingTask.getResourceFile();
if (artifactFile == null) {
log.debug("no artifact pass in indexing task so skip it");
} else {
ArtifactContext ac = artifactContextProducer.getArtifactContext(context, artifactFile.toFile());
if (ac != null) {
// TODO make that configurable?
if (artifactFile.getFileName().toString().endsWith(".pom")) {
ac.getArtifactInfo().setFileExtension("pom");
ac.getArtifactInfo().setPackaging("pom");
ac.getArtifactInfo().setClassifier("pom");
}
if (indexingTask.getAction().equals(ArtifactIndexingTask.Action.ADD)) {
// IndexSearcher s = context.getIndexSearcher();
// String uinfo = ac.getArtifactInfo().getUinfo();
// TopDocs d = s.search( new TermQuery( new Term( ArtifactInfo.UINFO, uinfo ) ), 1 );
BooleanQuery.Builder qb = new BooleanQuery.Builder();
qb.add(indexer.constructQuery(MAVEN.GROUP_ID, new SourcedSearchExpression(ac.getArtifactInfo().getGroupId())), BooleanClause.Occur.MUST);
qb.add(indexer.constructQuery(MAVEN.ARTIFACT_ID, new SourcedSearchExpression(ac.getArtifactInfo().getArtifactId())), BooleanClause.Occur.MUST);
qb.add(indexer.constructQuery(MAVEN.VERSION, new SourcedSearchExpression(ac.getArtifactInfo().getVersion())), BooleanClause.Occur.MUST);
if (ac.getArtifactInfo().getClassifier() != null) {
qb.add(indexer.constructQuery(MAVEN.CLASSIFIER, new SourcedSearchExpression(ac.getArtifactInfo().getClassifier())), BooleanClause.Occur.MUST);
}
if (ac.getArtifactInfo().getPackaging() != null) {
qb.add(indexer.constructQuery(MAVEN.PACKAGING, new SourcedSearchExpression(ac.getArtifactInfo().getPackaging())), BooleanClause.Occur.MUST);
}
FlatSearchRequest flatSearchRequest = new FlatSearchRequest(qb.build(), context);
FlatSearchResponse flatSearchResponse = indexer.searchFlat(flatSearchRequest);
if (flatSearchResponse.getResults().isEmpty()) {
log.debug("Adding artifact '{}' to index..", ac.getArtifactInfo());
indexerEngine.index(context, ac);
} else {
log.debug("Updating artifact '{}' in index..", ac.getArtifactInfo());
// TODO check if update exists !!
indexerEngine.update(context, ac);
}
context.updateTimestamp();
context.commit();
} else {
log.debug("Removing artifact '{}' from index..", ac.getArtifactInfo());
indexerEngine.remove(context, ac);
}
}
}
// close the context if not a repo scan request
if (!indexingTask.isExecuteOnEntireRepo()) {
log.debug("Finishing indexing task on resource file : {}", indexingTask.getResourceFile() != null ? indexingTask.getResourceFile() : " none ");
finishIndexingTask(indexingTask, repository, context);
}
} catch (IOException e) {
log.error("Error occurred while executing indexing task '{}': {}", indexingTask, e.getMessage(), e);
throw new TaskExecutionException("Error occurred while executing indexing task '" + indexingTask + "'", e);
}
}
}
use of org.apache.maven.index.expr.SourcedSearchExpression in project archiva by apache.
the class ArchivaIndexingTaskExecutorTest method testRemoveArtifactFromIndex.
@Test
public void testRemoveArtifactFromIndex() throws Exception {
Path basePath = repo.getLocalPath();
Path artifactFile = basePath.resolve("org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar");
ArtifactIndexingTask task = new ArtifactIndexingTask(repo, artifactFile, ArtifactIndexingTask.Action.ADD, repo.getIndexingContext());
// add artifact to index
indexingExecutor.executeTask(task);
BooleanQuery.Builder qb = new BooleanQuery.Builder();
qb.add(indexer.constructQuery(MAVEN.GROUP_ID, new SourcedSearchExpression("org.apache.archiva")), BooleanClause.Occur.SHOULD);
// q.add(
// indexer.constructQuery( MAVEN.ARTIFACT_ID, new SourcedSearchExpression( "archiva-index-methods-jar-test" ) ),
// Occur.SHOULD );
IndexingContext ctx = repo.getIndexingContext().getBaseContext(IndexingContext.class);
FlatSearchRequest flatSearchRequest = new FlatSearchRequest(qb.build(), ctx);
FlatSearchResponse response = indexer.searchFlat(flatSearchRequest);
assertTrue(Files.exists(basePath.resolve(".indexer")));
assertTrue(Files.exists(basePath.resolve(".index")));
// should return 1 hit
assertEquals(1, response.getTotalHitsCount());
// remove added artifact from index
task = new ArtifactIndexingTask(repo, artifactFile, ArtifactIndexingTask.Action.DELETE, repo.getIndexingContext());
indexingExecutor.executeTask(task);
task = new ArtifactIndexingTask(repo, artifactFile, ArtifactIndexingTask.Action.FINISH, repo.getIndexingContext());
indexingExecutor.executeTask(task);
qb = new BooleanQuery.Builder();
qb.add(indexer.constructQuery(MAVEN.GROUP_ID, new SourcedSearchExpression("org.apache.archiva")), BooleanClause.Occur.SHOULD);
qb.add(indexer.constructQuery(MAVEN.ARTIFACT_ID, new SourcedSearchExpression("archiva-index-methods-jar-test")), BooleanClause.Occur.SHOULD);
assertTrue(Files.exists(basePath.resolve(".indexer")));
assertTrue(Files.exists(basePath.resolve(".index")));
flatSearchRequest = new FlatSearchRequest(qb.build(), getIndexingContext());
response = indexer.searchFlat(flatSearchRequest);
// artifact should have been removed from the index!
// .totalHits );
assertEquals(0, response.getTotalHitsCount());
// TODO: test it was removed from the packaged index also
}
Aggregations