php - Elasticsearch combine two different queries types in one request

558

I'm wondering if there is a possibility to combine two query types, in my case I need a queryString and filtered query, each has to operate on a different field. the query filtered get all record who's in$postids$postids:multiple value of post_id and the query string is to search a words in text Currently I need two requests to archive this: Query filtered:

GET /myindex1/tweecoms/_search
{
"query" : {
        "filtered" : {
            "filter" : {
                "terms" : {
                    "post_id" : ['.$postids.']
                }
            }
        }
}

query String:

GET /myindex1/tweecoms/_search{
 "query": {
                   "query_string": {
                        "query": "*' . $sujet . '*",
                        "lenient": true
                    }
                    }
}

and I tried to combine it

GET /myindex1/tweecoms/_search

{
  "query": {
    "bool": {
      "should": [
        {
           "query_string": {
                        "query": "hhhh",
                        "lenient": true
                    }
        },
        {
          "filtered" : {
            "filter" : {
                "terms" : {
                    "post_id" : [0,157]
                }
            }
        }
        }
      ]
    }
  }
}`

but what i try run just the query string

10

Answer

Solution:

I think you need to write query insidefiltered clause. This will help you understand. Try this

{
  "query": {
    "filtered": {
      "query": {
        "query_string": {
          "query": "keyword"
        }
      },
      "filter": {
        "terms": {
          "post_id": [0,157]
        }
      }
    }
  }
}

The problem with your query is you are using should clause which will select either documents with given query string or those with given post_ids. You can replaceshould withmust and will get expected results.

You could also use minimum_should_match parameter inside your query

I hope this helps

People are also looking for solutions to the problem: php - The class is not found from the closure

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.