How to combine queries of must & suggest in search request?


#1

Hi,
How to combine queries of must & suggest with only matching id? I'll explain detail in example,

PUT map_index
{
"mappings": {
"composite": {
"properties": {
"id": {
"type": "keyword"
},
"title_suggest": {
"type": "completion"
}
}
}
}
}

PUT map_index/composite/doc1
{
"id": "s1",
"title_suggest": "kissan_trigger_450_ml"
}

PUT map_index/composite/doc2
{
"id": "s2",
"title_suggest": "kissan_trigge"
}

PUT map_index/composite/doc3
{
"id": "s1",
"title_suggest": "trigge"
}

So when I search for,

POST map_index/composite/_search
{

"query": {
"bool": {
  "must": [
    {
      "term": {"id":"s1"}
    }
  ]
}

},
"suggest": {
"title-suggest": {
"prefix": "kissan",
"completion": {
"field": "title_suggest",
"skip_duplicates":true
}
}
}
}

result as,

{
"took": 1,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 0.2876821,
"hits": [
{
"_index": "map_index",
"_type": "composite",
"_id": "doc1",
"_score": 0.2876821,
"_source": {
"id": "s1",
"title_suggest": "kissan_trigger_450_ml"
}
},
{
"_index": "map_index",
"_type": "composite",
"_id": "doc3",
"_score": 0.2876821,
"_source": {
"id": "s1",
"title_suggest": "trigge"
}
}
]
},
"suggest": {
"title-suggest": [
{
"text": "kissan",
"offset": 0,
"length": 6,
"options": [
{
"text": "kissan_trigge",
"_index": "map_index",
"_type": "composite",
"_id": "doc2",
"_score": 1,
"_source": {
"id": "s2",
"title_suggest": "kissan_trigge"
}
},
{
"text": "kissan_trigger_450_ml",
"_index": "map_index",
"_type": "composite",
"_id": "doc1",
"_score": 1,
"_source": {
"id": "s1",
"title_suggest": "kissan_trigger_450_ml"
}
}
]
}
]
}
}

The response contains results doc1 & doc3 matched for 'must' query and 'suggest' contains doc1 & do2. So my question is, is there way to combine these queries like (id + title_suggest) so that i'll get doc1 as only result because it only matches both id('s1') & title_suggest('kissan').


(system) #2

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.