Closed crebacz closed 9 years ago
Your syntax looks correct. Where are you placing this code?
In search.php. the elasticsearch is returning results (I can tell because the fuzzy search is working beautifully). But for some reason, it returns all results rather than only those within a certain category or post type.
So I decided to look at the query that is being sent to ElasticSearch (I basically var_dumped the $args that are being encoded into json within the search method in the EP-API class). A couple of things that I noticed:
Here are the two queries:
{
"from":0,
"size":10,
"sort":[{
"_score":{ "order":"desc" }
}
],
"query":{
"bool":{
"should":[
{
"multi_match":{
"query":"p&p",
"fields":[
"post_title", "post_excerpt", "post_content"
],
"boost":2
}
},
{
"fuzzy_like_this":{
"fields":[
"post_title","post_excerpt","post_content"
],
"like_text":"p&p",
"min_similarity":0.75
}
}
]
}
},
"filter":{
"and":[{
"terms": {
"post_type.raw": [
"post","page","attachment","bulletin","policy","diagram","video"
]
}
}]
}
}
And here is the second query that has the post_type and size arguments included, but nothing related to the category:
{
"from":0,
"size":50,
"sort":[
{
"_score":
{
"order":"desc"
}
}
],
"query":{
"bool":{
"should":[
{"multi_match":
{
"query":"p&p",
"fields":[
"post_title","post_content","post_excerpt"
],
"boost":2
}
},
{"fuzzy_like_this":{
"fields":[
"post_title","post_content","post_excerpt"
],
"like_text":
"p&p",
"min_similarity":0.75
}
}
]
}
},
"filter":{
"and":[
{
"terms":{
"post_type.raw":[
"post","page","bulletin","policy"
]
}
}
]
}
}
Based on psorenson's question above, I'm wondering if it is best practice to put the query inside a function and then use a 'pre_get_posts' filter on the query. It probably would remove the duplicated query. Or is it best practice to keep this type of query within the search.php file?
Personally, I use a 'pre_get_posts' filter. I'd also be interested to hear how others implement this.
Hey guys, sorry the documentation was incorrect. You need to use tax_query
instead of category
as the query param.
Thank you! The updated documentation worked beautifully for me. Appreciate your help with this. Thanks psorenson for your input.
Hello--
I'm not sure if this is the right forum; however, I'm having trouble with having filters taking effect in my queries. Specifically, I am trying to limit search results by category. I have tried the following:
I have also tried using a tax_query with category as the taxonomy. It still isn't filtering. I'm not quite sure how to begin troubleshooting this. Any pointers on where to start?