use of com.rbmhtechnology.vind.api.query.suggestion.ExecutableSuggestionSearch in project vind by RBMHTechnology.
the class SolrSearchServer method buildSolrQuery.
protected SolrQuery buildSolrQuery(ExecutableSuggestionSearch search, DocumentFactory assets, DocumentFactory childFactory) {
final String searchContext = search.getSearchContext();
final SolrQuery query = new SolrQuery();
query.setRequestHandler("/suggester");
if (search.isStringSuggestion()) {
StringSuggestionSearch s = (StringSuggestionSearch) search;
query.setParam("suggestion.field", s.getSuggestionFields().stream().map(name -> {
if (Objects.nonNull(childFactory)) {
final FieldDescriptor<?> field = Objects.nonNull(assets.getField(name)) ? assets.getField(name) : childFactory.getField(name);
if (Objects.isNull(field)) {
log.warn("No field descriptor found for field name {} in factories: {}, {}", name, assets.getType(), childFactory.getType());
}
return getFieldname(field, UseCase.Suggest, searchContext);
} else {
if (Objects.isNull(assets.getField(name))) {
log.warn("No field descriptor found for field name {} in factory: {}", name, assets.getType());
}
return getFieldname(assets.getField(name), UseCase.Suggest, searchContext);
}
}).filter(Objects::nonNull).toArray(String[]::new));
} else {
DescriptorSuggestionSearch s = (DescriptorSuggestionSearch) search;
query.setParam("suggestion.field", s.getSuggestionFields().stream().map(descriptor -> getFieldname(descriptor, UseCase.Suggest, searchContext)).filter(Objects::nonNull).toArray(String[]::new));
}
query.setParam("q", search.getInput());
// TODO: somehow this is still needed here, it should by configuration
query.setParam("suggestion.df", SUGGESTION_DF_FIELD);
query.setParam("suggestion.limit", String.valueOf(search.getLimit()));
String parentTypeFilter = "_type_:" + assets.getType();
if (Objects.nonNull(childFactory)) {
parentTypeFilter = "(" + parentTypeFilter + " OR _type_:" + childFactory.getType() + ")";
}
query.add(CommonParams.FQ, parentTypeFilter);
// filters
if (search.hasFilter()) {
SolrUtils.Query.buildFilterString(search.getFilter(), assets, childFactory, query, searchContext, false);
new SolrChildrenSerializerVisitor(assets, childFactory, searchContext, false);
}
// suggestion deep search
if (Objects.nonNull(childFactory)) {
if (search.hasFilter()) {
// TODO clean up!
final String parentFilterQuery = "(" + String.join(" AND ", query.getFilterQueries()) + ")";
final String childrenFilterQuery = search.getFilter().accept(new SolrChildrenSerializerVisitor(assets, childFactory, searchContext, false));
final String childrenBJQ = "{!child of=\"_type_:" + assets.getType() + "\" v='" + childrenFilterQuery + "'}";
query.set(CommonParams.FQ, String.join(" ", parentFilterQuery, "OR", childrenBJQ));
}
}
return query;
}
use of com.rbmhtechnology.vind.api.query.suggestion.ExecutableSuggestionSearch in project vind by RBMHTechnology.
the class SuggestionTest method testSpellcheck.
@Test
public void testSpellcheck() {
SolrClient client = Mockito.mock(SolrClient.class);
SolrSearchServer server = new SolrSearchServer(client, false);
FieldDescriptor descriptor = Mockito.mock(FieldDescriptor.class);
when(descriptor.getType()).thenReturn(String.class);
when(descriptor.isSuggest()).thenReturn(true);
DocumentFactory factory = Mockito.mock(DocumentFactory.class);
when(factory.getField(any())).thenReturn(descriptor);
ExecutableSuggestionSearch search = Search.suggest("abc").fields("field");
SolrQuery query = server.buildSolrQuery(search, factory, null);
assertEquals("abc", query.get("q"));
assertEquals("dynamic_single_suggest_string_null", query.get("suggestion.field"));
assertEquals(SolrSearchServer.SUGGESTION_DF_FIELD, query.get("suggestion.df"));
assertEquals("10", query.get("suggestion.limit"));
search.setLimit(100);
query = server.buildSolrQuery(search, factory, null);
assertEquals("100", query.get("suggestion.limit"));
ExecutableSuggestionSearch search2 = Search.suggest("abc").fields(descriptor).clearFilter();
query = server.buildSolrQuery(search2, factory, null);
assertEquals("10", query.get("suggestion.limit"));
}
Aggregations