Reputation: 399
I have an index of books where I store the full text content of books (with stopwords removed, but that's not important for my question). I have the following query:
> GET /books/_search
> {
> "_source": {
> "includes": ["author", "title"]
> },
> "query": {
> "bool": {
> "should": [
> {
> "match_phrase": {
> "body": "all happy families are alike"
> }
> },
> {
> "match": {
> "body": "all happy families are alike"
> }
> }
> ]
> }
> }
> }
I get matches for all documents that have the full string with highest score and then, with lower scores, those having one or more of matching terms: first match is 'Anna Karenina' with very high score, then any book that has 'happy' , 'families' in it. What I would like to obtain:
I struggle to find how to get point 1.
Upvotes: 2
Views: 2609
Reputation: 9099
Exact and partial matches cannot be returned conditionally. You can use named queries to check at client side if match is exact/partial.
GET books/_search
{
"query": {
"bool": {
"should": [
{
"match_phrase": {
"body": {
"query": "all happy families are alike",
"_name":"exact_match" ---> name of query(can be anything)
}
}
},
{
"match": {
"body": {
"query": "all happy families are alike",
"_name":"partial_match"
}
}
}
]
}
}
}
Result:
"hits" : [
{
"_index" : "books",
"_type" : "_doc",
"_id" : "4i0MeG0BCVIM-bi3Fif1",
"_score" : 4.1589947,
"_source" : {
"title" : "Anna Karenina",
"body" : "all happy families are alike"
},
"matched_queries" : [ ---> returns name of queries where condition matched
"exact_match",
"partial_match"
]
},
{
"_index" : "books",
"_type" : "_doc",
"_id" : "4y0MeG0BCVIM-bi3aScM",
"_score" : 0.44216567,
"_source" : {
"title" : "book 1",
"body" : "happy alike"
},
"matched_queries" : [ ---> returns name of queries where condition matched
"partial_match"
]
}
]
}
Upvotes: 2