Query question


(曾岩) #1

Hi,

I'm new to Elasticsearch and try to integrate it into our project but met a
problem. In our data source, it has two fields: mode and schedule which are
all integer. Through UI, it should can query records based on these two
fields like:
SELECT * FROM doc WHERE mode = 1 AND (schedule = 1 OR schedule = 3)

I tried below query JSONs but none return the expected results, anyone can
help? Thank you!

{

  • "query": {*
  • "bool": {*
  •  "must": [*
    
  •    { "match": { "mode": 1 } }*
    
  •  ],*
    
  •  "should": [*
    
  • { "match": { "schedule": 1 } },*
  •    { "match": { "schedule": 3 } }*
    
  •  ]*
    
  • }*
  • }*
    }

{ "query": { "filtered": { "query": { "match_all": {} },
"filter": { "and" : [ { "term"
: { "mode" : "1" } } ] }, "filter":
{ "and" : [ { "term" : {
"schedule" : "1" } }, { "term" : {
"schedule" : "3" } } ] } } }}

--
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/45bd7de6-ffe9-4d9f-bef6-be11e19b051f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


(Ivan Brusic) #2

Your two clauses, mode and schedule, are joined via an AND, so those two
clauses should be part of the *must *section. The schedule clauses is then
an OR between two clauses, so it should be a nested bool filter using
should. Hopefully that made sense. :slight_smile:

Since you are using term queries on what are hopefully non-analyzed fields
(numeric fields are always non-analyzed), I will use a match all query with
filters since it should be more efficient. The query should looking
something like:

{
"query": {
"filtered": {
"query": {
"match_all": {}
},
"filter": {
"bool": {
"must": [
{
"term": { "mode": "1" }
},
{
"bool": {
"should": [
{
"term": { "schedule": "1" }
},
{
"term": { "schedule": "3" }
}
]
}
}
]
}
}
}
}
}

--
Ivan

On Mon, May 5, 2014 at 3:36 AM, 曾岩 eater2046@gmail.com wrote:

Hi,

I'm new to Elasticsearch and try to integrate it into our project but met
a problem. In our data source, it has two fields: mode and schedule which
are all integer. Through UI, it should can query records based on these two
fields like:
SELECT * FROM doc WHERE mode = 1 AND (schedule = 1 OR schedule = 3)

I tried below query JSONs but none return the expected results, anyone can
help? Thank you!

{

  • "query": {*
  • "bool": {*
  •  "must": [*
    
  •    { "match": { "mode": 1 } }*
    
  •  ],*
    
  •  "should": [*
    
  • { "match": { "schedule": 1 } },*
  •    { "match": { "schedule": 3 } }*
    
  •  ]*
    
  • }*
  • }*
    }

{ "query": { "filtered": { "query": { "match_all": {} },
"filter": { "and" : [ { "term"
: { "mode" : "1" } } ] }, "filter":
{ "and" : [ { "term" : {
"schedule" : "1" } }, { "term" : {
"schedule" : "3" } } ] } } }}

--
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/45bd7de6-ffe9-4d9f-bef6-be11e19b051f%40googlegroups.comhttps://groups.google.com/d/msgid/elasticsearch/45bd7de6-ffe9-4d9f-bef6-be11e19b051f%40googlegroups.com?utm_medium=email&utm_source=footer
.
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/CALY%3DcQC1qjqZBgKXQujiXpp8TCKS0s4QRv97YHXO7_48_Ld8ZA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


(曾岩) #3

Thank you very much!

--
Eateral

在 2014年5月8日星期四UTC+8上午12时10分42秒,Ivan Brusic写道:

Your two clauses, mode and schedule, are joined via an AND, so those two
clauses should be part of the *must *section. The schedule clauses is
then an OR between two clauses, so it should be a nested bool filter using
should. Hopefully that made sense. :slight_smile:

Since you are using term queries on what are hopefully non-analyzed fields
(numeric fields are always non-analyzed), I will use a match all query with
filters since it should be more efficient. The query should looking
something like:

{
"query": {
"filtered": {
"query": {
"match_all": {}
},
"filter": {
"bool": {
"must": [
{
"term": { "mode": "1" }
},
{
"bool": {
"should": [
{
"term": { "schedule": "1" }
},
{
"term": { "schedule": "3" }
}
]
}
}
]
}
}
}
}
}

--
Ivan

On Mon, May 5, 2014 at 3:36 AM, 曾岩 <eate...@gmail.com <javascript:>>wrote:

Hi,

I'm new to Elasticsearch and try to integrate it into our project but met
a problem. In our data source, it has two fields: mode and schedule which
are all integer. Through UI, it should can query records based on these two
fields like:
SELECT * FROM doc WHERE mode = 1 AND (schedule = 1 OR schedule = 3)

I tried below query JSONs but none return the expected results, anyone
can help? Thank you!

{

  • "query": {*
  • "bool": {*
  •  "must": [*
    
  •    { "match": { "mode": 1 } }*
    
  •  ],*
    
  •  "should": [*
    
  • { "match": { "schedule": 1 } },*
  •    { "match": { "schedule": 3 } }*
    
  •  ]*
    
  • }*
  • }*
    }

{ "query": { "filtered": { "query": { "match_all": {} },
"filter": { "and" : [ { "term"
: { "mode" : "1" } } ] }, "filter":
{ "and" : [ { "term" : {
"schedule" : "1" } }, { "term" : {
"schedule" : "3" } } ] } } }}

--
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 elasticsearc...@googlegroups.com <javascript:>.
To view this discussion on the web visit
https://groups.google.com/d/msgid/elasticsearch/45bd7de6-ffe9-4d9f-bef6-be11e19b051f%40googlegroups.comhttps://groups.google.com/d/msgid/elasticsearch/45bd7de6-ffe9-4d9f-bef6-be11e19b051f%40googlegroups.com?utm_medium=email&utm_source=footer
.
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/0e8e2332-d5b7-416b-a16e-f965884cc42f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


(system) #4