se.l4.silo.search.SearchIndexQueryImpl Maven / Gradle / Ivy
Show all versions of silo-search-api Show documentation
package se.l4.silo.search;
import java.util.Locale;
import java.util.function.Supplier;
import se.l4.silo.query.QueryFetchResult;
import se.l4.silo.query.QueryRunner;
import se.l4.silo.search.facet.FacetQueryType;
import se.l4.silo.search.query.QueryReceiver;
public class SearchIndexQueryImpl
implements SearchIndexQuery
{
private final QueryRunner> runner;
private SearchIndexQueryRequest request;
public SearchIndexQueryImpl(QueryRunner> runner)
{
this.runner = runner;
request = new SearchIndexQueryRequest();
}
@Override
public SearchIndexQuery fromLocale(Locale locale)
{
request.setLanguage(locale.toLanguageTag());
return this;
}
@Override
public SearchIndexQuery waitForLatest()
{
request.setWaitForLatest(true);
return this;
}
@Override
public SearchIndexQuery offset(long offset)
{
request.setOffset(offset);
return this;
}
@Override
public SearchIndexQuery limit(long limit)
{
request.setLimit(limit);
return this;
}
@Override
public void parent(SearchIndexQuery path, QueryReceiver receiver)
{
throw new UnsupportedOperationException();
}
@Override
public void addQuery(QueryItem item)
{
request.addQueryItem(item);
}
@Override
public SearchIndexQuery done()
{
return this;
}
@Override
public >> P query(P q)
{
q.parent(this, this);
return q;
}
@Override
public >> C withFacet(String id, Supplier facetType)
{
C builder = facetType.get();
builder.setReceiver(id, c -> {
request.addFacetItem(c);
return this;
});
return builder;
}
@Override
public SearchIndexQuery addSort(String sort, boolean sortAscending)
{
request.addSortItem(sort, sortAscending, null);
return this;
}
@Override
public >> C addSort(String field, Supplier scoring)
{
C builder = scoring.get();
builder.setReceiver((ascending, params) -> {
request.addSortItem(field, ascending, params);
return this;
});
return builder;
}
@Override
public >> C setScoring(Supplier scoring)
{
C builder = scoring.get();
builder.setReceiver(c -> {
request.setScoring(c);
return this;
});
return builder;
}
@Override
public >> C withFacet(String id, FacetQueryType, C> type)
{
return type.create(id, c -> {
request.addFacetItem(c);
return this;
});
}
@Override
public SearchResult run()
{
QueryFetchResult> qr = runner.fetchResults(request, (f) -> {
float score = f.getMetadata("score", 0f);
return new SearchHitImpl<>(f.getData(), score);
});
Facets facets = qr.getMetadata("facets");
if(facets == null)
{
facets = new FacetsImpl();
}
return new SearchResultImpl<>(qr, facets);
}
}