# Any chance to get rid of this query\_string?

**URL:** <https://discuss.elastic.co/t/any-chance-to-get-rid-of-this-query-string/21906>\
**Category:** Elasticsearch\
**Created:** [January 29, 2015, 5:02pm UTC](https://discuss.elastic.co/t/any-chance-to-get-rid-of-this-query-string/21906 "2015-01-29T17:02:05Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![menschguenther](https://avatars.discourse-cdn.com/v4/letter/m/9f8e36/32.png) [@menschguenther](https://discuss.elastic.co/u/menschguenther)\
**Post date:** [January 29, 2015, 5:02pm UTC](https://discuss.elastic.co/t/any-chance-to-get-rid-of-this-query-string/21906/1 "2015-01-29T17:02:05Z")

</div>

Hi all,

i need to write a search query which collects documents from 3 types in my  
index.  
basically i would use a multi\_match like

{  
"multi\_match" : {  
"query": "SearchQuery",  
"fields": ["account.name", "group.title", "post.content"]  
}  
}

but the result of this query needs to be filtered by an additional  
"ID-field" and "public-field" which should only affect the post documents!.

so i created a string query like:

{  
"query\_string": {  
"query": "(content:SearchQuery AND (viewableIDs:1 OR public:true)) OR (title:SearchQuery) OR (name:SearchQuery)"  
}  
}

which does the job. (show only post documents which belongs to User with ID 1 or is public)

is it possible to create a multi\_match query or any other kind of query to achieve this result?

thx 🙂

--  
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](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/cc8a1bdc-156e-4124-9aac-d055cf903ac1%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/cc8a1bdc-156e-4124-9aac-d055cf903ac1%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![vineeth\_mohan\_2](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vineeth_mohan_2/32/747_2.png) [@vineeth\_mohan\_2](https://discuss.elastic.co/u/vineeth_mohan_2)\
**Post date:** [January 30, 2015, 5:23am UTC](https://discuss.elastic.co/t/any-chance-to-get-rid-of-this-query-string/21906/2 "2015-01-30T05:23:01Z")

</div>

Hello ,

You need to use a bool query  
[http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html)  
or a filtered query  
[http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-filtered-query.html](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-filtered-query.html)  
for this purpose.  
In bool , you can mix and match any number of queries and implement the  
boolean logic there.  
Filtered query gives you provision to add a query part and a filter part.

As the additional conditions are filters which gives a Yes or No match  
rather than score , i feel it would be best to offload them to filters  
So below would be the best -

{  
"query": {  
"filtered": {  
"filter": {  
"bool": {  
"must": [  
{  
"term": {  
"viewableIDs": 1  
}  
},  
{  
"term": {  
"public": true  
}  
}  
]  
}  
},  
"query": {  
"multi\_match": {  
"query": "SearchQuery",  
"fields": [  
"account.name",  
"group.title",  
"post.content"  
]  
}  
}  
}  
}  
}

Thanks  
Vineeth Mohan,  
Elasticsearch consultant,  
[qbox.io](http://qbox.io) ( Elasticsearch service provider [http://qbox.io](http://qbox.io))

On Thu, Jan 29, 2015 at 10:32 PM, [menschguenther@googlemail.com](mailto:menschguenther@googlemail.com) wrote:

> Hi all,
> 
> i need to write a search query which collects documents from 3 types in my  
> index.  
> basically i would use a multi\_match like
> 
> {  
> "multi\_match" : {  
> "query": "SearchQuery",  
> "fields": ["account.name", "group.title", "post.content"]  
> }  
> }
> 
> but the result of this query needs to be filtered by an additional  
> "ID-field" and "public-field" which should only affect the post  
> documents!.
> 
> so i created a string query like:
> 
> {  
> "query\_string": {  
> "query": "(content:SearchQuery AND (viewableIDs:1 OR public:true)) OR (title:SearchQuery) OR (name:SearchQuery)"  
> }  
> }
> 
> which does the job. (show only post documents which belongs to User with ID 1 or is public)
> 
> is it possible to create a multi\_match query or any other kind of query to achieve this result?
> 
> thx 🙂
> 
> --  
> 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](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> To view this discussion on the web visit  
> [https://groups.google.com/d/msgid/elasticsearch/cc8a1bdc-156e-4124-9aac-d055cf903ac1%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/cc8a1bdc-156e-4124-9aac-d055cf903ac1%40googlegroups.com)  
> [https://groups.google.com/d/msgid/elasticsearch/cc8a1bdc-156e-4124-9aac-d055cf903ac1%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/cc8a1bdc-156e-4124-9aac-d055cf903ac1%40googlegroups.com?utm_medium=email&utm_source=footer)  
> .  
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
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](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/CAGdPd5mu9P3\_Z5t7Spbfr994poBUzfO7b7t27nnvDzQJ8vRSJg%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAGdPd5mu9P3_Z5t7Spbfr994poBUzfO7b7t27nnvDzQJ8vRSJg%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![menschguenther](https://avatars.discourse-cdn.com/v4/letter/m/9f8e36/32.png) [@menschguenther](https://discuss.elastic.co/u/menschguenther)\
**Post date:** [January 30, 2015, 10:46am UTC](https://discuss.elastic.co/t/any-chance-to-get-rid-of-this-query-string/21906/3 "2015-01-30T10:46:53Z")

</div>

hey vinneth,

thx for your answer.  
but this approach doesn't work.

"viewableIDs" and "public" field are only available on my post type

so this filter would swallow all my groups and accounts.

i tried it with

"must": [  
{  
"term": {  
"post.viewableIDs": 1  
}  
},  
{  
"term": {  
"post.public": true  
}  
}  
]

but this also does not only filter the post field.

i guess i have to create a "public" field for account and group and change  
"must" to "should" ?

Am Freitag, 30. Januar 2015 06:23:13 UTC+1 schrieb vineeth mohan:

> Hello ,
> 
> You need to use a bool query  
> [http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html)  
> or a filtered query  
> [http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-filtered-query.html](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-filtered-query.html)  
> for this purpose.  
> In bool , you can mix and match any number of queries and implement the  
> boolean logic there.  
> Filtered query gives you provision to add a query part and a filter part.
> 
> As the additional conditions are filters which gives a Yes or No match  
> rather than score , i feel it would be best to offload them to filters  
> So below would be the best -
> 
> {  
> "query": {  
> "filtered": {  
> "filter": {  
> "bool": {  
> "must": [  
> {  
> "term": {  
> "viewableIDs": 1  
> }  
> },  
> {  
> "term": {  
> "public": true  
> }  
> }  
> ]  
> }  
> },  
> "query": {  
> "multi\_match": {  
> "query": "SearchQuery",  
> "fields": [  
> "account.name",  
> "group.title",  
> "post.content"  
> ]  
> }  
> }  
> }  
> }  
> }
> 
> Thanks  
> Vineeth Mohan,  
> Elasticsearch consultant,  
> [qbox.io](http://qbox.io) ( Elasticsearch service provider [http://qbox.io](http://qbox.io))
> 
> On Thu, Jan 29, 2015 at 10:32 PM, \<[menschg...@googlemail.com](mailto:menschg...@googlemail.com) \<javascript:\>
> 
> > wrote:
> 
> > Hi all,
> > 
> > i need to write a search query which collects documents from 3 types in  
> > my index.  
> > basically i would use a multi\_match like
> > 
> > {  
> > "multi\_match" : {  
> > "query": "SearchQuery",  
> > "fields": ["account.name", "group.title", "post.content"]  
> > }  
> > }
> > 
> > but the result of this query needs to be filtered by an additional  
> > "ID-field" and "public-field" which should only affect the post  
> > documents!.
> > 
> > so i created a string query like:
> > 
> > {  
> > "query\_string": {  
> > "query": "(content:SearchQuery AND (viewableIDs:1 OR public:true)) OR (title:SearchQuery) OR (name:SearchQuery)"  
> > }  
> > }
> > 
> > which does the job. (show only post documents which belongs to User with ID 1 or is public)
> > 
> > is it possible to create a multi\_match query or any other kind of query to achieve this result?
> > 
> > thx 🙂
> > 
> > --  
> > 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 [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com) \<javascript:\>.  
> > To view this discussion on the web visit  
> > [https://groups.google.com/d/msgid/elasticsearch/cc8a1bdc-156e-4124-9aac-d055cf903ac1%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/cc8a1bdc-156e-4124-9aac-d055cf903ac1%40googlegroups.com)  
> > [https://groups.google.com/d/msgid/elasticsearch/cc8a1bdc-156e-4124-9aac-d055cf903ac1%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/cc8a1bdc-156e-4124-9aac-d055cf903ac1%40googlegroups.com?utm_medium=email&utm_source=footer)  
> > .  
> > For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
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](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/1eea7b20-16c6-4281-ba66-b6e1b5e392c5%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/1eea7b20-16c6-4281-ba66-b6e1b5e392c5%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 6, 2017, 12:35am UTC](https://discuss.elastic.co/t/any-chance-to-get-rid-of-this-query-string/21906/4 "2017-07-06T00:35:42Z")

</div>

Closed on July 6, 2017, 12:35am UTC


