use of com.rbmhtechnology.vind.api.query.FulltextSearch in project vind by RBMHTechnology.
the class ParentChildrenTest method testGetNumberOfChildren.
@Test
public void testGetNumberOfChildren() {
FulltextSearch search = Search.fulltext().orChildrenSearch(child);
SearchResult result = server.execute(search, parent);
assertEquals(4, result.getNumOfResults());
assertEquals(Integer.valueOf(2), result.getResults().get(2).getChildCount());
}
use of com.rbmhtechnology.vind.api.query.FulltextSearch in project vind by RBMHTechnology.
the class SearchApplication method main.
public static void main(String[] args) {
// get an instance of a server (in this case a embedded solr server)
SearchServer server = SearchServer.getInstance();
final MonitoringWriter writer = new LogWriter();
final InterfaceApplication application = new InterfaceApplication("Application name", "0.0.0", new Interface("sugar-love", "0.0.0"));
final MonitoringSearchServer monitoringSearchServer = new MonitoringSearchServer(server, application, writer);
monitoringSearchServer.setSession(new UserSession("session-ID-1234567", new User("user 2", "user-ID-2")));
// index 2 news items
NewsItem i1 = new NewsItem("1", "New Vind instance needed", ZonedDateTime.now().minusMonths(3), "article", "coding");
NewsItem i2 = new NewsItem("2", "Vind instance available", ZonedDateTime.now(), "blog", "coding", "release");
server.indexBean(i1);
server.indexBean(i2);
server.commit();
// this search should retrieve news items that should match the search term best
FulltextSearch search = Search.fulltext("vind release");
BeanSearchResult<NewsItem> result = monitoringSearchServer.execute(search, NewsItem.class);
// lets log the results
System.out.println("\n--- Search 1: Fulltext ---");
result.getResults().forEach(System.out::println);
System.out.println();
// now we want to have also the facets for category and kind.
// additionally we change the query
search.text("vind");
search.facet("category", "kind");
result = monitoringSearchServer.execute(search, NewsItem.class);
System.out.println("\n--- Search 2.1: Category Facets ---");
result.getFacetResults().getTermFacet("category", String.class).getValues().forEach(System.out::println);
System.out.println();
System.out.println("--- Search 2.2: Kind Facets ---");
result.getFacetResults().getTermFacet("kind", String.class).getValues().forEach(System.out::println);
System.out.println();
monitoringSearchServer.setSession(new UserSession("session-ID-12345678", new User("user 3", "user-ID-3")));
// new we define a search order based on the 'created ' field
search.sort(desc("created"));
result = monitoringSearchServer.execute(search, NewsItem.class);
System.out.println("\n--- Search 3: Sort by created descending ---");
result.getResults().forEach(System.out::println);
System.out.println();
// now we want to filter for all items with the kind 'blog'.
result = monitoringSearchServer.execute(Search.fulltext().filter(eq("kind", "blog")), NewsItem.class);
System.out.println("\n--- Search 4: Filtered by kind=blog ---");
result.getResults().forEach(System.out::println);
System.out.println();
// this search should retrieve news items
// we set the page to 1 and the pagesize to 1
result = monitoringSearchServer.execute(Search.fulltext().page(1, 1), NewsItem.class);
// lets log the results
System.out.println("\n--- Search 5.1: Paging (Page 1) ---");
result.getResults().forEach(System.out::println);
System.out.println();
// the result itself supports paging, so we can loop the pages
while (((BeanPageResult) result).hasNextPage()) {
result = ((BeanPageResult) result).nextPage();
System.out.println("\n--- Search 5.2: Paging (Page " + ((BeanPageResult) result).getPage() + ") ---");
result.getResults().forEach(System.out::println);
System.out.println();
}
// suggest
SuggestionResult suggestions = monitoringSearchServer.execute(Search.suggest("c").fields("category"), NewsItem.class);
System.out.println("\n--- Suggestions: ---");
System.out.println(suggestions.get("category").getValues());
System.out.println();
// close the server
server.close();
}
use of com.rbmhtechnology.vind.api.query.FulltextSearch in project vind by RBMHTechnology.
the class MonitoringSearchServerTest method testQueryMonitoringWithSessionAndLogger.
@Test
public void testQueryMonitoringWithSessionAndLogger() throws IOException {
TestMonitoringWriter logger = new TestMonitoringWriter();
MonitoringSearchServer server = new MonitoringSearchServer(testSearchServer.getSearchServer(), new SimpleApplication("app"), new SimpleSession("123"), logger);
// index 2 news items
NewsItem i1 = new NewsItem("1", "New Vind instance needed", ZonedDateTime.now().minusMonths(3), "article", "coding");
NewsItem i2 = new NewsItem("2", "Vind instance available", ZonedDateTime.now(), "blog", "coding", "release");
server.indexBean(i1);
server.indexBean(i2);
server.commit();
// this search should retrieve news items that should match the search term best
FulltextSearch search = Search.fulltext("vind release");
search.facet("category", "kind");
BeanSearchResult<NewsItem> result = server.execute(search, NewsItem.class);
assertEquals(1, logger.logs.size());
}
use of com.rbmhtechnology.vind.api.query.FulltextSearch in project vind by RBMHTechnology.
the class SearchService method news.
public SearchResult news(String query) {
FulltextSearch search = Search.fulltext(query);
// special sort filter allows to combine a date with scoring, so
// that best fitting and latest documents are ranked to top
search.sort(desc(scoredDate(created)));
// complex facets (range in this case) and simple ones can be issued in one query
search.facet(category);
search.facet(range("dates", created, ZonedDateTime.now().minus(Duration.ofDays(100)), ZonedDateTime.now(), Duration.ofDays(10)));
// specific field types allow special filters, in this case a between datetime filter
search.filter(created.between(ZonedDateTime.now().minusDays(7), ZonedDateTime.now()));
// also numeric fields support special filters
search.filter(ranking.greaterThan(1));
return monitoringSearchServer.execute(search, newsItems);
}
use of com.rbmhtechnology.vind.api.query.FulltextSearch in project vind by RBMHTechnology.
the class TestServerTest method testContextSearch.
// MBDN-458
@Test
public void testContextSearch() {
final SingleValueFieldDescriptor<Float> numberField = new FieldDescriptorBuilder().setFacet(true).buildNumericField("numberone", Float.class);
final FieldDescriptor<String> entityID = new FieldDescriptorBuilder().setFacet(true).setLanguage(Language.English).buildTextField("entityID");
final SingleValueFieldDescriptor<Date> dateField = new FieldDescriptorBuilder().buildUtilDateField("datefield");
final MultiValueFieldDescriptor.TextFieldDescriptor<String> multiTextField = new FieldDescriptorBuilder().setFacet(true).setSuggest(true).buildMultivaluedTextField("textMulti");
final DocumentFactory assets = new DocumentFactoryBuilder("asset").addField(numberField).addField(dateField).addField(entityID).addField(multiTextField).setUpdatable(true).build();
final Document d1 = assets.createDoc("1").setValue(numberField, 0f).setContextualizedValue(numberField, "numberContext", 24f).setContextualizedValue(numberField, "singleContext", 3f).setValue(entityID, "123").setValue(dateField, new Date());
final Document d2 = assets.createDoc("2").setValue(numberField, 2f).setValue(entityID, "123").setContextualizedValues(multiTextField, "multicontext", "text1", "text2").setValue(dateField, new Date());
final SearchServer server = testSearchServer.getSearchServer();
server.index(d1);
server.index(d2);
server.commit();
final SuggestionResult suggestion = server.execute(Search.suggest("text1").context("multicontext").addField(multiTextField), assets);
assertEquals("One contextualize suggestion expected", 1, suggestion.size());
FulltextSearch searchAll = Search.fulltext();
SearchResult searchResult = server.execute(searchAll, assets);
assertEquals("Number of results", 2, searchResult.getNumOfResults());
assertEquals("Number_one field", 0f, searchResult.getResults().get(0).getValue("numberone"));
searchAll = Search.fulltext().context("numberContext").filter(and(eq(entityID, "123"), eq(numberField, 24f))).facet(entityID);
searchResult = server.execute(searchAll, assets);
assertEquals("Number of results", 1, searchResult.getNumOfResults());
assertEquals("Number_one field", 24f, searchResult.getResults().get(0).getContextualizedValue("numberone", "numberContext"));
assertEquals("Number_one field", null, searchResult.getResults().get(0).getContextualizedValue("numberone", "singleContext"));
searchAll = Search.fulltext().context("singleContext");
searchResult = server.execute(searchAll, assets);
assertEquals("Number of results", 2, searchResult.getNumOfResults());
assertEquals("Number_one field", 3f, searchResult.getResults().get(0).getContextualizedValue("numberone", "singleContext"));
assertEquals("Number_one field", null, searchResult.getResults().get(0).getContextualizedValue("numberone", "numberContext"));
searchAll = Search.fulltext().context("multicontext");
searchResult = server.execute(searchAll, assets);
assertEquals("Number of results", 2, searchResult.getNumOfResults());
assertThat("textMulti multi text field", (List<String>) searchResult.getResults().get(1).getContextualizedValue("textMulti", "multicontext"), containsInAnyOrder("text1", "text2"));
Delete deleteInContext = new Delete(multiTextField.isNotEmpty()).context("multicontext");
server.execute(deleteInContext, assets);
server.commit();
searchResult = server.execute(searchAll, assets);
assertEquals("Number of results", 1, searchResult.getResults().size());
server.execute(Search.update("1").set(numberField, 1f), assets);
server.commit();
searchResult = server.execute(searchAll, assets);
assertEquals("Number_one field", 1f, searchResult.getResults().get(0).getValue("numberone"));
server.execute(Search.update("1").set("singleContext", numberField, 4f), assets);
server.commit();
searchResult = server.execute(searchAll.context("singleContext"), assets);
assertEquals("Number_one field", 4f, searchResult.getResults().get(0).getContextualizedValue("numberone", "singleContext"));
server.execute(Search.update("1").remove("singleContext", numberField, null), assets);
server.commit();
searchResult = server.execute(searchAll.context("singleContext"), assets);
assertEquals("Number_one field in single context has been removed", null, searchResult.getResults().get(0).getContextualizedValue("numberone", "singleContext"));
}
Aggregations