And/Or Filter using Java API


(ama sindhuc) #1

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


}

}


(Igor Motov) #2

The field names in elasticsearch are case sensitive. You are using
startTime in the query and StartTime in the document. Speaking of the
document, make sure that StartTime is mapped correctly and uses appropriate date
formathttp://www.elasticsearch.org/guide/reference/mapping/date-format.html
.

On Sunday, May 13, 2012 2:55:26 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": 

"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()); 


} 

}


(mohammad) #3

Hello everyone ,
i am also trying to do something like this:
my intended query is like this:
select from range (dateA to dateB);

AndFilterBuilder myFilters = FilterBuilders.andFilter();
myFilters.add(FilterBuilders.rangeFilter("dateFormatted").gte(dateDebut).lte(dateFin));

howeve this returns to nothing:
the values dateDebut and dateFin are in the following format:
yyyy/MM/dd

and the field dateFormatted is in the following format:

--
You received this message because you are subscribed to the Google Groups "elasticsearch" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elasticsearch+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/2b014627-03d6-4cb6-abd2-a2780aa15e0d%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


(Sang Dang) #4

I suggest you convert your dateA to long & dateB to long & then using
RangeFilter to do it.

On Tuesday, February 4, 2014 4:32:57 PM UTC+7, Mohammad Shafraz Subdurally
wrote:

Hello everyone ,
i am also trying to do something like this:
my intended query is like this:
select from range (dateA to dateB);

AndFilterBuilder myFilters = FilterBuilders.andFilter();
myFilters.add(FilterBuilders.rangeFilter("dateFormatted").gte(dateDebut).lte(dateFin));

howeve this returns to nothing:
the values dateDebut and dateFin are in the following format:
yyyy/MM/dd

and the field dateFormatted is in the following format:

--
You received this message because you are subscribed to the Google Groups "elasticsearch" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elasticsearch+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/471511cd-fda9-4d10-9358-30fcd4663a12%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


(David Pilato) #5

It depends on your mapping I guess.

What does it look like?

--
David :wink:
Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs

Le 5 avr. 2014 à 10:43, kidkid zkidkid@gmail.com a écrit :

I suggest you convert your dateA to long & dateB to long & then using RangeFilter to do it.

On Tuesday, February 4, 2014 4:32:57 PM UTC+7, Mohammad Shafraz Subdurally wrote:

Hello everyone ,
i am also trying to do something like this:
my intended query is like this:
select from range (dateA to dateB);

AndFilterBuilder myFilters = FilterBuilders.andFilter();
myFilters.add(FilterBuilders.rangeFilter("dateFormatted").gte(dateDebut).lte(dateFin));

howeve this returns to nothing:
the values dateDebut and dateFin are in the following format:
yyyy/MM/dd

and the field dateFormatted is in the following format:

--
You received this message because you are subscribed to the Google Groups "elasticsearch" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elasticsearch+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/471511cd-fda9-4d10-9358-30fcd4663a12%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "elasticsearch" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elasticsearch+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/67D9B39D-4760-430F-BB75-D71F2A8CF225%40pilato.fr.
For more options, visit https://groups.google.com/d/optout.


(system) #6