Search in sources :

Example 1 with DateMathExpression

use of com.rbmhtechnology.vind.api.query.datemath.DateMathExpression in project vind by RBMHTechnology.

the class TestServerTest method testDateMathIntervals.

// MBDN-450
@Test
public void testDateMathIntervals() {
    ZonedDateTime now = ZonedDateTime.now(ZoneId.of("UTC"));
    ZonedDateTime yesterday = ZonedDateTime.now(ZoneId.of("UTC")).minus(1, ChronoUnit.DAYS);
    FieldDescriptor<String> title = new FieldDescriptorBuilder().setFullText(true).setFacet(true).buildTextField("title");
    SingleValueFieldDescriptor.DateFieldDescriptor<ZonedDateTime> created = new FieldDescriptorBuilder().setFacet(true).setStored(true).buildDateField("created");
    SingleValueFieldDescriptor.UtilDateFieldDescriptor<Date> modified = new FieldDescriptorBuilder().setFacet(true).buildUtilDateField("modified");
    NumericFieldDescriptor<Long> category = new FieldDescriptorBuilder().setFacet(true).buildMultivaluedNumericField("category", Long.class);
    DocumentFactory assets = new DocumentFactoryBuilder("asset").addField(title).addField(created).addField(category).addField(modified).build();
    Document d1 = assets.createDoc("1").setValue(title, "Hello World").setValue(created, yesterday).setValue(modified, new Date()).setValues(category, Arrays.asList(1L, 2L));
    Document d2 = assets.createDoc("2").setValue(title, "Hello Friends").setValue(created, now).setValue(modified, new Date()).addValue(category, 4L);
    SearchServer server = testSearchServer.getSearchServer();
    server.index(d1);
    server.index(d2);
    server.commit();
    Interval.DateMathInterval i1 = Interval.dateInterval("past_24_hours", new DateMathExpression().sub(1, DAY), new DateMathExpression());
    Interval.DateMathInterval i2 = Interval.dateInterval("past_week", new DateMathExpression().sub(7, DAY), new DateMathExpression());
    FulltextSearch search = Search.fulltext("hello").filter(created.before(new DateMathExpression())).facet(query("anotherQuery", and(category.between(7, 10), created.after(new DateMathExpression().sub(1, DAY))))).facet(range("dates", created, new DateMathExpression().sub(1, DAY), new DateMathExpression(), Duration.ofHours(1))).facet(range("mod a", modified, new Date(), new Date(), 1L, TimeUnit.HOURS, "cats")).facet(interval("quality", category, Interval.numericInterval("low", 0L, 2L), Interval.numericInterval("high", 3L, 4L))).facet(interval("time", created, i1, i2)).facet(interval("time2", modified, Interval.dateInterval("early", new Date(0), new Date(10000)), Interval.dateInterval("late", new Date(10000), new Date(20000)))).facet(interval("time3", modified, Interval.dateInterval("early", new DateMathExpression(), new DateMathExpression().add(10000, MILLISECONDS)), Interval.dateInterval("late", new DateMathExpression().add(10000, MILLISECONDS), new DateMathExpression().add(20000, MILLISECONDS)))).page(1, 25).sort(desc(created));
    PageResult result = (PageResult) server.execute(search, assets);
    assertEquals(2, result.getNumOfResults());
    assertEquals(2, result.getResults().size());
    assertEquals("2", result.getResults().get(0).getId());
    assertEquals("asset", result.getResults().get(0).getType());
    assertEquals("2", result.getResults().get(0).getId());
    assertEquals("asset", result.getResults().get(0).getType());
    assertTrue(now.equals(result.getResults().get(0).getValue(created)));
    assertTrue(now.equals(result.getResults().get(0).getValue("created")));
    assertEquals(2, result.getFacetResults().getIntervalFacet("quality").getValues().size());
    assertEquals(2, result.getFacetResults().getIntervalFacet("time").getValues().size());
    System.out.println(result);
    PageResult next = (PageResult) result.nextPage();
    SearchResult prev = next.previousPage();
    TermFacetResult<Long> facet = result.getFacetResults().getTermFacet(category);
    RangeFacetResult<ZonedDateTime> dates = result.getFacetResults().getRangeFacet("dates", ZonedDateTime.class);
    ZonedDateTime rangeDate = dates.getValues().get(0).getValue();
}
Also used : 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) PageResult(com.rbmhtechnology.vind.api.result.PageResult) ZonedDateTime(java.time.ZonedDateTime) DateMathExpression(com.rbmhtechnology.vind.api.query.datemath.DateMathExpression) FulltextSearch(com.rbmhtechnology.vind.api.query.FulltextSearch) Interval(com.rbmhtechnology.vind.api.query.facet.Interval) Test(org.junit.Test)

Example 2 with DateMathExpression

use of com.rbmhtechnology.vind.api.query.datemath.DateMathExpression in project vind by RBMHTechnology.

the class TestServerTest method timeZoneSearchTest.

// MBDN-441
@Test
public void timeZoneSearchTest() {
    ZonedDateTime now = ZonedDateTime.now(ZoneId.of("UTC"));
    ZonedDateTime yesterday = ZonedDateTime.now(ZoneId.of("UTC")).minus(1, ChronoUnit.DAYS);
    ZonedDateTime oneHourAgo = ZonedDateTime.now(ZoneId.systemDefault()).minus(1, ChronoUnit.HOURS);
    FieldDescriptor<String> title = new FieldDescriptorBuilder().setFullText(true).buildTextField("title");
    SingleValueFieldDescriptor.DateFieldDescriptor<ZonedDateTime> created = new FieldDescriptorBuilder().setFacet(true).buildDateField("created");
    SingleValueFieldDescriptor.UtilDateFieldDescriptor<Date> modified = new FieldDescriptorBuilder().setFacet(true).buildUtilDateField("modified");
    NumericFieldDescriptor<Long> category = new FieldDescriptorBuilder().setFacet(true).buildMultivaluedNumericField("category", Long.class);
    DocumentFactory assets = new DocumentFactoryBuilder("asset").addField(title).addField(created).addField(category).addField(modified).build();
    Document d1 = assets.createDoc("1").setValue(title, "Hello World").setValue(created, yesterday).setValue(modified, new Date()).setValues(category, Arrays.asList(1L, 2L));
    Document d2 = assets.createDoc("2").setValue(title, "Hello Friends").setValue(created, now).setValue(modified, new Date()).addValue(category, 4L);
    SearchServer server = testSearchServer.getSearchServer();
    server.index(d1);
    server.index(d2);
    server.commit();
    FulltextSearch searchAll = Search.fulltext().filter(before("created", new DateMathExpression().sub(1, HOUR))).timeZone("America/Havana");
    final SearchResult searchResult = server.execute(searchAll, assets);
    assertEquals("No of results", 1, searchResult.getNumOfResults());
    final Document result = searchResult.getResults().get(0);
    assertThat("Doc-Title", result.getValue(title), Matchers.equalTo("Hello World"));
}
Also used : 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) ZonedDateTime(java.time.ZonedDateTime) DateMathExpression(com.rbmhtechnology.vind.api.query.datemath.DateMathExpression) FulltextSearch(com.rbmhtechnology.vind.api.query.FulltextSearch) Test(org.junit.Test)

Example 3 with DateMathExpression

use of com.rbmhtechnology.vind.api.query.datemath.DateMathExpression in project vind by RBMHTechnology.

the class FacetMapper method intervalDateFacetMapper.

public static <T> Interval<DateMathExpression> intervalDateFacetMapper(String name, String query) {
    final Matcher dateMatcher = INTERVAL_DATE_FACET.matcher(query);
    if (dateMatcher.matches()) {
        final DateMathParser solrDateMathParser = new DateMathParser();
        DateMathExpression startDateMath = null;
        if (!dateMatcher.group("start").equals("*")) {
            try {
                String startDateMathText = dateMatcher.group("dateMathStart");
                if (startDateMathText != null) {
                    solrDateMathParser.parseMath(startDateMathText);
                }
                String startDate = dateMatcher.group("dateStart");
                if (startDate != null) {
                    final Date date = DateFormatUtil.parseDate(startDate);
                    startDateMath = new DateMathExpression(ZonedDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()));
                } else {
                    startDateMath = new DateMathExpression();
                }
            } catch (ParseException e) {
                return null;
            }
            String rootUnitStart = dateMatcher.group("rootUnitStart");
            if (rootUnitStart != null) {
                startDateMath.setRootUnit(TimeUnit.valueOf(rootUnitStart.replace("/", "")));
            }
            String mathUnitStart = dateMatcher.group("mathUnitStart");
            if (mathUnitStart != null) {
                startDateMath.setUnit(TimeUnit.valueOf(mathUnitStart.replace("/", "")));
            }
            String timeOpsStart = dateMatcher.group("timeOpsStart");
            if (timeOpsStart != null) {
                String[] timeOps = timeOpsStart.split("(?=[\\+\\-])");
                for (String op : timeOps) {
                    Long quantity = Long.valueOf(op.replaceAll("[^\\d]", ""));
                    TimeUnit unit = TimeUnit.valueOf(op.replaceAll("[^A-Z]", ""));
                    if (op.startsWith("+")) {
                        startDateMath.add(quantity, unit);
                    }
                    if (op.startsWith("-")) {
                        startDateMath.sub(quantity, unit);
                    }
                }
            }
        }
        DateMathExpression endDateMath = null;
        if (!dateMatcher.group("end").equals("*")) {
            try {
                String endMath = dateMatcher.group("dateMathEnd");
                if (endMath != null) {
                    solrDateMathParser.parseMath(endMath);
                }
                String endDate = dateMatcher.group("dateEnd");
                if (endDate != null) {
                    final Date date = DateFormatUtil.parseDate(endDate);
                    endDateMath = new DateMathExpression(ZonedDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()));
                } else {
                    endDateMath = new DateMathExpression();
                }
            } catch (ParseException e) {
                return null;
            }
            String rootUnitEnd = dateMatcher.group("rootUnitEnd");
            if (rootUnitEnd != null) {
                endDateMath.setRootUnit(TimeUnit.valueOf(rootUnitEnd.replace("/", "")));
            }
            String mathUnitEnd = dateMatcher.group("mathUnitEnd");
            if (mathUnitEnd != null) {
                endDateMath.setUnit(TimeUnit.valueOf(mathUnitEnd.replace("/", "")));
            }
            String timeOpsEnd = dateMatcher.group("timeOpsEnd");
            if (timeOpsEnd != null) {
                String[] timeOps = timeOpsEnd.split("(?=[\\+\\-])");
                for (String op : timeOps) {
                    if (!op.isEmpty()) {
                        Long quantity = Long.valueOf(op.replaceAll("[^\\d]", ""));
                        TimeUnit unit = TimeUnit.valueOf(op.replaceAll("[^A-Z]", ""));
                        if (op.startsWith("+")) {
                            endDateMath.add(quantity, unit);
                        }
                        if (op.startsWith("-")) {
                            endDateMath.sub(quantity, unit);
                        }
                    }
                }
            }
        }
        return Interval.dateInterval(name, startDateMath, endDateMath);
    } else {
        throw new RuntimeException("Invalid interval query string definition: '" + query + "'");
    }
}
Also used : Matcher(java.util.regex.Matcher) DateMathExpression(com.rbmhtechnology.vind.api.query.datemath.DateMathExpression) DateMathParser(org.apache.solr.util.DateMathParser) ParseException(java.text.ParseException) Date(java.util.Date)

Aggregations

DateMathExpression (com.rbmhtechnology.vind.api.query.datemath.DateMathExpression)3 Document (com.rbmhtechnology.vind.api.Document)2 SearchServer (com.rbmhtechnology.vind.api.SearchServer)2 FulltextSearch (com.rbmhtechnology.vind.api.query.FulltextSearch)2 SearchResult (com.rbmhtechnology.vind.api.result.SearchResult)2 ZonedDateTime (java.time.ZonedDateTime)2 SolrInputDocument (org.apache.solr.common.SolrInputDocument)2 Test (org.junit.Test)2 Interval (com.rbmhtechnology.vind.api.query.facet.Interval)1 PageResult (com.rbmhtechnology.vind.api.result.PageResult)1 ParseException (java.text.ParseException)1 Date (java.util.Date)1 Matcher (java.util.regex.Matcher)1 DateMathParser (org.apache.solr.util.DateMathParser)1