Is it possible to use the AND operator for multiple fields match?

In this case the first word is found on the first field and the second on
the second. I know I can do this with string() but I'd like to stay way
from it because it's susceptible to parse errors depending on what the user
inputs.

I get no results using AND.

INDEX

curl -X POST "http://localhost:9200/search/document/" -d '{
"first":"Barbara","last":"Smith"
}'

SEARCH

curl -X GET 'http://localhost:9200/search/_search?pretty' -d '{
"query": {
"multi_match": {
"query": "Barbara Smith",
"operator": "AND",
"fields": [
"first",
"last"
]
}
}
}'

RESULT

{
"took": 1,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"failed": 0
},
"hits": {
"total": 0,
"max_score": null,
"hits": []
}
}

I get a result using OR.

INDEX

curl -X POST "http://localhost:9200/search/document/" -d '{
"first":"Barbara","last":"Smith"
}'

SEARCH

curl -X GET 'http://localhost:9200/search/_search?pretty' -d '{
"query": {
"multi_match": {
"query": "Barbara Smith",
"operator": "OR",
"fields": [
"first",
"last"
]
}
}
}'

RESULT

{
"took": 3,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.4898842,
"hits": [
{
"_index": "search",
"_type": "document",
"_id": "Z0nxJ5H6R1ONDwU1xfOKIA",
"_score": 0.4898842,
"_source": {
"first": "Barbara",
"last": "Smith"
}
}
]
}
}

What are my options?

Thank you

--
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.
For more options, visit https://groups.google.com/groups/opt_out.

Hello,

one option is to use the _all field, if you don't need explicit fields.

multi_match sample:
{
"query": {
"multi_match": {
"query": "Barbara Smith",
"operator": "AND",
"fields": [
"_all"
]
}
}
}

  1. Bool sample:
    {
    "query": {
    "bool": {
    "must": [
    {
    "query_string": {
    "default_field": "_all",
    "query": "Barbara Smith"
    }
    }
    ]
    }
    },
    "from": 0,
    "size": 50
    }

--
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.
For more options, visit https://groups.google.com/groups/opt_out.

Bruno,

I wrote a Java driver that emits the queries in JSON and can emit the
response documents in JSON, or in a number of other formats. So these are
curl-able but not issued via curl. (Constructing queries in Java?
Wonderful! Constructing queries in JSON? No! But I digress...)

A single-word query that matches two records:

FULL QUERY:
{
"from" : 0,
"size" : 20,
"query" : {
"match" : {
"text" : {
"query" : "lives",
"type" : "boolean"
}
}
},
"version" : true,
"explain" : false,
"fields" : [ "_ttl", "_source" ]
}

RESULTS:
{ "_index" : "mortal" , "_type" : "elf" , "_id" : "1" , "_version" : 1 ,
"_score" : 1.4914339780807495 , "_source" : { "cn" : "Celeborn" , "text" :
"Lives forever" } }
{ "_index" : "mortal" , "_type" : "elf" , "_id" : "2" , "_version" : 1 ,
"_score" : 1.4914339780807495 , "_source" : { "cn" : "Galadriel" , "text" :
"Lives forever" } }

And now for an AND across two fields. But a BoolQuery and not a MultiMatch
(not sure what's up with the MultiMatch):

FULL QUERY:
{
"from" : 0,
"size" : 20,
"query" : {
"bool" : {
"must" : [ {
"match" : {
"text" : {
"query" : "lives",
"type" : "boolean"
}
}
}, {
"match" : {
"cn" : {
"query" : "galadriel",
"type" : "boolean"
}
}
} ]
}
},
"version" : true,
"explain" : false,
"fields" : [ "_ttl", "_source" ]
}

RESULTS:
{ "_index" : "mortal" , "_type" : "elf" , "_id" : "2" , "_version" : 1 ,
"_score" : 1.4914339780807495 , "_source" : { "cn" : "Galadriel" , "text" :
"Lives forever" } }

But also an AND of a phrase match and a trucated (that is to say, prefix)
match:

FULL QUERY:
{
"from" : 0,
"size" : 20,
"query" : {
"bool" : {
"must" : [ {
"match" : {
"text" : {
"query" : "lives forever",
"type" : "phrase",
"slop" : 0
}
}
}, {
"prefix" : {
"cn" : "gala"
}
} ]
}
},
"version" : true,
"explain" : false,
"fields" : [ "_ttl", "_source" ]
}

RESULTS:
{ "_index" : "mortal" , "_type" : "elf" , "_id" : "2" , "_version" : 1 ,
"_score" : 1.4914339780807495 , "_source" : { "cn" : "Galadriel" , "text" :
"Lives forever" } }

Brian

On Friday, June 21, 2013 3:46:22 PM UTC-4, Bruno Miranda wrote:

In this case the first word is found on the first field and the second on
the second. I know I can do this with string() but I'd like to stay way
from it because it's susceptible to parse errors depending on what the user
inputs.

I get no results using AND.

INDEX

curl -X POST "http://localhost:9200/search/document/" -d '{
"first":"Barbara","last":"Smith"
}'

SEARCH

curl -X GET 'http://localhost:9200/search/_search?pretty' -d '{
"query": {
"multi_match": {
"query": "Barbara Smith",
"operator": "AND",
"fields": [
"first",
"last"
]
}
}
}'

RESULT

{
"took": 1,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"failed": 0
},
"hits": {
"total": 0,
"max_score": null,
"hits":
}
}

I get a result using OR.

INDEX

curl -X POST "http://localhost:9200/search/document/" -d '{
"first":"Barbara","last":"Smith"
}'

SEARCH

curl -X GET 'http://localhost:9200/search/_search?pretty' -d '{
"query": {
"multi_match": {
"query": "Barbara Smith",
"operator": "OR",
"fields": [
"first",
"last"
]
}
}
}'

RESULT

{
"took": 3,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.4898842,
"hits": [
{
"_index": "search",
"_type": "document",
"_id": "Z0nxJ5H6R1ONDwU1xfOKIA",
"_score": 0.4898842,
"_source": {
"first": "Barbara",
"last": "Smith"
}
}
]
}
}

What are my options?

Thank you

--
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.
For more options, visit https://groups.google.com/groups/opt_out.