logo
down
shadow

Elasticsearch - combining query_string and bool query in filter


Elasticsearch - combining query_string and bool query in filter

By : Massimo Spadotto
Date : November 17 2020, 04:28 AM
like below fixes the issue bool is meant to be used to club various queries together into a single bool query. You can use bool to combine multiple queries in this manner -
code :
{
  "query": {
    "bool": {
      "must": [
        {
          "query_string": {
            "query": "field:text"
          }
        },
        {
          "match": {
            "field": "text"
          }
        }
      ]
    }
  }
}
{
  "query": {
    "bool": {
      "must": [
        {
          "bool": {
            "must": [
              {
                "query_string": {
                  "query": "field:text"
                }
              },
              {
                "match": {
                  "field": "value"
                }
              }
            ]
          }
        },
        {
          "match": {
            "field": "text"
          }
        }
      ]
    }
  }
}


Share : facebook icon twitter icon
Elasticsearch bool query with filter

Elasticsearch bool query with filter


By : Gaurav Goyal
Date : March 29 2020, 07:55 AM
I hope this helps you . Seems like you are not on Elasticsearch 2.x.
For Elasticsearch 1.x use a FilteredQuery: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-filtered-query.html
code :
{
  "filtered": {
    "query": {
      "bool": {
        "should": [
          {
            "term": {
              "attr2": "on"
            }
          },
          {
            "term": {
              "attr3": "on"
            }
          }
        ]
      }
    },
    "filter": {
      "bool": {
        "must": [
          {
            "match": {
              "attr1": "on"
            }
          },
          {
            "match": {
              "category": "7eb84c804a8c824fd608e05f78d42f10"
            }
          }
        ]
      }
    }
  }
}
Combining query_string with bool filters in Elasticsearch 2.x

Combining query_string with bool filters in Elasticsearch 2.x


By : Enis Dorini
Date : March 29 2020, 07:55 AM
wish of those help You're almost there! query_string simply needs to go inside the bool/must as well:
code :
'query': {
     'bool': {
         'must': [
              {
                'query_string': {
                    'query': "Blah Blah",
                }
              },
              {
                'terms': {
                    'newsline': ['blah']
                  }
              }
         ]
    }
},
PHP Elasticsearch, bool query with filter not getting any result

PHP Elasticsearch, bool query with filter not getting any result


By : William
Date : March 29 2020, 07:55 AM
I hope this helps you . The following query works for me, i.e. you need to use a terms query for versions and have an array with each version.
code :
 $params = [
        'index' => 'notes',
        'type' => 'release',
        'body' => [
        'query' => [
            'bool' => [
                    'must' => [
                        'match' => [ 'name' => 'ssh' ]
                    ],
                    'filter' => [
                        'terms' => [ 
                            'versions' => ['12.1.2', '12.1.1', '12.1.0']
                        ]   
                    ]
                ]
            ]
        ]
  ];
Elasticsearch bool query with filter and should

Elasticsearch bool query with filter and should


By : hamozoo
Date : March 29 2020, 07:55 AM
I wish this helpful for you Your problem is that you are wrapping all the quires\filters inside should, which means that all the statements with be combined by OR logic. If you need AND you need to wrap statements inside must.
By the way, you are not using filter, you are using term query. There is huge difference between query and filter in elasticsearch. To use filter you need to use bool query with filter.
code :
{
 "query": {
    "bool": {
      "must": [
        {
          "bool": {
            "must": [
              {
                "query_string": {
                  "query": searchText
                }
              }
            ]
          }
        }        
      ],
      "filter": {
          "bool": {
              "must": [
                  {
                    "terms": {
                        "loadingPort":loadingPortArray
                    }
                  },
                  {
                    "terms": {
                        "unloadingPort":loadingPortArray
                    }
                  }
              ]
          }
      }
    }
  }
}
Nest Elasticsearch, combining bool query of Must and Should

Nest Elasticsearch, combining bool query of Must and Should


By : Arun
Date : March 29 2020, 07:55 AM
will help you I could find where was my mistake! I used Querycontainer for format in the same command as what I used for Year and then used Must in my query. Here is the changed part of the code:
shadow
Privacy Policy - Terms - Contact Us © ourworld-yourmove.org