Search in sources :

Example 16 with FulltextSearch

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());
}
Also used : SearchResult(com.rbmhtechnology.vind.api.result.SearchResult) FulltextSearch(com.rbmhtechnology.vind.api.query.FulltextSearch) Test(org.junit.Test)

Example 17 with FulltextSearch

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();
}
Also used : InterfaceApplication(com.rbmhtechnology.vind.monitoring.model.application.InterfaceApplication) User(com.rbmhtechnology.vind.monitoring.model.user.User) MonitoringSearchServer(com.rbmhtechnology.vind.monitoring.MonitoringSearchServer) MonitoringSearchServer(com.rbmhtechnology.vind.monitoring.MonitoringSearchServer) SearchServer(com.rbmhtechnology.vind.api.SearchServer) BeanPageResult(com.rbmhtechnology.vind.api.result.BeanPageResult) LogWriter(com.rbmhtechnology.vind.monitoring.log.writer.LogWriter) SuggestionResult(com.rbmhtechnology.vind.api.result.SuggestionResult) MonitoringWriter(com.rbmhtechnology.vind.monitoring.logger.MonitoringWriter) UserSession(com.rbmhtechnology.vind.monitoring.model.session.UserSession) Interface(com.rbmhtechnology.vind.monitoring.model.Interface.Interface) FulltextSearch(com.rbmhtechnology.vind.api.query.FulltextSearch) NewsItem(com.rbmhtechnology.vind.demo.step2.model.NewsItem)

Example 18 with FulltextSearch

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());
}
Also used : SimpleApplication(com.rbmhtechnology.vind.monitoring.model.application.SimpleApplication) SimpleSession(com.rbmhtechnology.vind.monitoring.model.session.SimpleSession) FulltextSearch(com.rbmhtechnology.vind.api.query.FulltextSearch) NewsItem(com.rbmhtechnology.vind.monitoring.model.NewsItem) Test(org.junit.Test)

Example 19 with FulltextSearch

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);
}
Also used : FulltextSearch(com.rbmhtechnology.vind.api.query.FulltextSearch)

Example 20 with FulltextSearch

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"));
}
Also used : Delete(com.rbmhtechnology.vind.api.query.delete.Delete) SearchServer(com.rbmhtechnology.vind.api.SearchServer) SearchResult(com.rbmhtechnology.vind.api.result.SearchResult) Document(com.rbmhtechnology.vind.api.Document) SolrInputDocument(org.apache.solr.common.SolrInputDocument) SuggestionResult(com.rbmhtechnology.vind.api.result.SuggestionResult) MultiValueFieldDescriptor(com.rbmhtechnology.vind.model.MultiValueFieldDescriptor) FulltextSearch(com.rbmhtechnology.vind.api.query.FulltextSearch) Test(org.junit.Test)

Aggregations

FulltextSearch (com.rbmhtechnology.vind.api.query.FulltextSearch)35 Test (org.junit.Test)31 SearchResult (com.rbmhtechnology.vind.api.result.SearchResult)26 SearchServer (com.rbmhtechnology.vind.api.SearchServer)21 Document (com.rbmhtechnology.vind.api.Document)20 SolrInputDocument (org.apache.solr.common.SolrInputDocument)20 ZonedDateTime (java.time.ZonedDateTime)11 PageResult (com.rbmhtechnology.vind.api.result.PageResult)8 MultiValueFieldDescriptor (com.rbmhtechnology.vind.model.MultiValueFieldDescriptor)8 Interval (com.rbmhtechnology.vind.api.query.facet.Interval)7 DateMathExpression (com.rbmhtechnology.vind.api.query.datemath.DateMathExpression)6 Delete (com.rbmhtechnology.vind.api.query.delete.Delete)6 SuggestionResult (com.rbmhtechnology.vind.api.result.SuggestionResult)6 LatLng (com.rbmhtechnology.vind.model.value.LatLng)5 ByteBuffer (java.nio.ByteBuffer)5 SolrClient (org.apache.solr.client.solrj.SolrClient)5 SolrQuery (org.apache.solr.client.solrj.SolrQuery)5 Language (com.rbmhtechnology.vind.annotations.language.Language)4 Search (com.rbmhtechnology.vind.api.query.Search)4 TimeUnit (com.rbmhtechnology.vind.api.query.datemath.DateMathExpression.TimeUnit)4