Java API to use Filters

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":
"AAMkADdmMDM4ZDZjLThkYjItNGZkYi05MDMyLWE3YzFkNWJkZWY1MABGAAAAAAAzlc4LRfuqSp
TimVdZOAM5BwDVH
+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": "t...@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.UpdateSettingsRequestBuilde
r;
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());
}

}

do a response.getHits() and check the size of the collection returned.

You should also try to build the query by hand with JSON and send it to
your ES instance.

On Sunday, May 13, 2012 9:06:00 AM UTC-4, ama sindhuc wrote:

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":
"AAMkADdmMDM4ZDZjLThkYjItNGZkYi05MDMyLWE3YzFkNWJkZWY1MABGAAAAAAAzlc4LRfuqSp

TimVdZOAM5BwDVH
+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": "t...@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.UpdateSettingsRequestBuilde

r;
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());
}

}

When you use a term filter, make sure that the data for the indexed
document did not end up being analyzed to a different terms. You can
control that using mappings for example by setting the relevant field index
to be not_analyzed.

When having problems with something you can't find, start broadening the
query / filters (for example, don't do the term filter) and see if you do
manage to find data. Once you find the one that removes results that you
expect, then its simpler to understand why it does not work.

On Sun, May 13, 2012 at 4:06 PM, ama sindhuc amasindhu@gmail.com wrote:

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":
"AAMkADdmMDM4ZDZjLThkYjItNGZkYi05MDMyLWE3YzFkNWJkZWY1MABGAAAAAAAzlc4LRfuqSp
TimVdZOAM5BwDVH
+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": "t...@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.UpdateSettingsRequestBuilde
r;
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());
}

}