Hi
I have indexed the following document (as indicated below) in elastic
search and attempting to apply a filter query using Java API (code
below)
However I am not receiving any results .
Code is indicated below. Please assist to let me know if I am missing
anything.
NOTE: StartTime and EndTime is mapped as DATE type
{
"schedules":
[
{
"Classification" : "schedule",
"type" : "outlookcalendar",
"Id":
"AAMkADdmMDM4ZDZjLThkYjItNGZkYi05MDMyLWE3YzFkNWJkZWY1MABGAAAAAAAzlc4LRfuqSpTimVdZOAM5BwDVH
+l4CJ/IRKcSEvbCqPNfAQtPns0bAADVH+l4CJ/IRKcSEvbCqPNfAQtPo/jDAAA=",
"Subject":"Meet JK in Mountain View",
"Location":"Red Rock Cafe",
"StartTime": "Thu Jan 05 01:00:00 PST 2012",
"EndTime": "Thu Jan 05 02:00:00 PST 2012",
"Organizer": "test@test.com",
"Body": "",
"RequiredAttendees": "",
"OptionalAttendees": "",
}
}
The intended query is (classification = "schedule" AND type = "email)
AND (range form Date(2012-5-01) to DATE(2012-7-01))
Code
package com.o1logic.services;
import
org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequestBuilder;
import
org.elasticsearch.action.admin.indices.settings.UpdateSettingsRequestBuilder;
import
org.elasticsearch.action.admin.indices.settings.UpdateSettingsResponse;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.joda.time.DateTime;
import org.elasticsearch.common.joda.time.format.DateTimeFormat;
import org.elasticsearch.common.joda.time.format.DateTimeFormatter;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.index.query.*;
import org.elasticsearch.node.Node;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static
org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
import static org.elasticsearch.index.query.FilterBuilders.andFilter;
import static org.elasticsearch.index.query.FilterBuilders.orFilter;
import static
org.elasticsearch.index.query.FilterBuilders.rangeFilter;
import static
org.elasticsearch.index.query.QueryBuilders.filteredQuery;
import static
org.elasticsearch.index.query.QueryBuilders.matchAllQuery;
import static org.elasticsearch.node.NodeBuilder.nodeBuilder;
public class TestIndex
{
final static Logger logger =
LoggerFactory.getLogger(TestIndex.class);
public static void main(String[] args) {
Node node = null;
Client client = null;
String elasticServer = "127.0.0.1";
Integer elasticServerPort = 9300;
logger.info("Initializing Search Server - Bootstrap");
node = nodeBuilder().node();
client = new TransportClient()
.addTransportAddress(new
InetSocketTransportAddress(elasticServer, elasticServerPort));
XContentBuilder indexSettings = null;
FilterBuilder typeFilter = FilterBuilders.andFilter(
FilterBuilders.termsFilter("classification","schedule"),
FilterBuilders.termsFilter("type",
"outlookcalendar"))
.filterName("classify");
DateTimeFormatter dtf = DateTimeFormat.forPattern("YYYY-MM-
dd");
DateTime dt1 = dtf.parseDateTime("2012-5-01");
DateTime dt2 = dtf.parseDateTime("2012-7-01");
AndFilterBuilder queryFilters = FilterBuilders.andFilter();
queryFilters.add(FilterBuilders.termFilter("classification",
"scehdule"));
queryFilters.add(FilterBuilders.termFilter("type",
"excalendar"));
RangeFilterBuilder rangeFilterBuilder =
FilterBuilders.rangeFilter("startTime");
rangeFilterBuilder.from(dt1).to(dt2);
FilterBuilder aggFilter =
FilterBuilders.andFilter(queryFilters,rangeFilterBuilder);
QueryBuilder queryBuilder =
QueryBuilders.filteredQuery(QueryBuilders.queryString("coffee"),
aggFilter);
SearchResponse response = client.prepareSearch("mike")
.setSearchType(SearchType.QUERY_AND_FETCH)
.setQuery(queryBuilder)
.setFilter(aggFilter)
.setFrom(0).setSize(50).setExplain(true)
.setScroll(new
TimeValue(600000))
.setExplain(false)
.execute()
.actionGet();
logger.info(response.toString());
}
}