Logic of search in list of documents


(elvis-2) #1

Hello!

Could you give me an advice? I am a novice in Elasticsearch.

I have these documents:
{
document1: [{location: foo, type: 3}, {location: baz, type: 1}],
document2: [{location: foo, type: 1}, {location: bar, type: 2}]
}

I want to find document with location "foo" and type 2 and I expect to
get only document2, but when I use bool query (with "must") I receive
both of docmunents.

What methods do you know to receive only document2?


(Clinton Gormley) #2

Hi Elvis

Could you give me an advice? I am a novice in Elasticsearch.

I have these documents:
{
document1: [{location: foo, type: 3}, {location: baz, type: 1}],
document2: [{location: foo, type: 1}, {location: bar, type: 2}]
}

I want to find document with location "foo" and type 2 and I expect to
get only document2, but when I use bool query (with "must") I receive
both of docmunents.

curl -XGET 'http://127.0.0.1:9200/_all/_search?pretty=1' -d '
{
"query" : {
"constant_score" : {
"filter" : {
"and" : {
"filters" : [
{
"term" : {
"type" : 2
}
},
{
"term" : {
"location" : "foo"
}
}
]
}
}
}
}
}
'


(elvis-2) #3

Thank you very much!
It helps me.

But why Bool filter and And filter have different behaviours?


(Clinton Gormley) #4

On Wed, 2011-05-11 at 01:25 -0700, Elvis wrote:

Thank you very much!
It helps me.

But why Bool filter and And filter have different behaviours?

I think you must have formatted your query incorrectly. The and/or
filters perform better than the bool filter, which is why I used it.

Here is the same thing with the bool filter:

curl -XGET 'http://127.0.0.1:9200/_all/_search?pretty=1' -d '
{
"query" : {
"constant_score" : {
"filter" : {
"bool" : {
"must" : [
{
"term" : {
"type" : 2
}
},
{
"term" : {
"location" : "foo"
}
}
]
}
}
}
}
}
'
clint


(elvis-2) #5

Sorry, but you advice was helpless.
I have two docs:

doc1: {family_members: [{name: Anna, twin: true}]}
doc2: {family_members: [{name: Anna, twin: false}, {name: Olga, twin:
true}]}

And I try to find twin Anna. My query is:

{
"query":{
"constant_score":{
"filter":{
"and":[
{
"query":{
"query_string":{
"query":"Anna",
"default_operator":"AND",
"default_field":"family_members.name"
}
}
},
{
"query":{
"query_string":{
"query":"True",
"default_operator":"AND",
"default_field":"family_members.twin"
}
}
}
]
}
}
}
}

But it returns doc1 and doc2. I expect to get only doc1. What is my
mistake?


(Clinton Gormley) #6

Hi Elvis

doc1: {family_members: [{name: Anna, twin: true}]}
doc2: {family_members: [{name: Anna, twin: false}, {name: Olga, twin:
true}]}

ES doesn't relate the values like you expect. So the above two docs are
actually stored more like this:

doc1:
family_members:
name: ['Anna']
twin: [true]
doc2:
family_members:
name: ['Anna','Olga']
twin: [true,false]

which means that your query won't work.

You could store the docs as:

doc1:
family_members:
singles: [],
twins: ['Anna']
doc2:
family_members:
singles: ['Anna'],
twins: ['Olga']

then search for:

curl -XGET 'http://127.0.0.1:9200/_all/_search?pretty=1' -d '
{
"query" : {
"field" : {
"family_members.twins" : "Anna"
}
}
}
'

clint


(system) #7