10up / ElasticPress

A fast and flexible search and query engine for WordPress.
https://elasticpress.io
GNU General Public License v2.0
1.24k stars 312 forks source link

Trouble with filters #252

Closed crebacz closed 9 years ago

crebacz commented 9 years ago

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:

<?php 
$search = new WP_Query(
                                    array(
                                    's' => get_search_query(),
                                    'search_fields' => array(
                                            'post_title',
                                            'post_content',
                                            'post_excerpt',
                                        ),
                                    'post_type' => array('post','page','bulletin','policy'),
                                    'posts_per_page' => 50,
                                    'paged' => $paged,
                                    'category' => 1
                                    )
                                );
?>

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?

psorensen commented 9 years ago

Your syntax looks correct. Where are you placing this code?

crebacz commented 9 years ago

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.

crebacz commented 9 years ago

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:

  1. There are two queries echoed when I perform a search. The first query seems to be the standard search output from Wordpress (i.e., it has the keyword, but all other fields are set to the default); the second query contains some of the arguments I passed in my query (e.g., size and post types).
  2. However, neither query being sent to ElasticSearch includes the category filter, even though I set it as seen above.

Here are the two queries:

  1. The first query has the search term but all else seem to be defaults (i.e. all post types and the default size of 10)
{
    "from":0,
    "size":10,
    "sort":[{
            "_score":{ "order":"desc" }
        }
    ],
    "query":{
        "bool":{
            "should":[
                {
                    "multi_match":{
                        "query":"p&amp;p",
                        "fields":[
                            "post_title", "post_excerpt", "post_content"
                        ],
                        "boost":2
                    }
                },
                {
                    "fuzzy_like_this":{
                        "fields":[
                            "post_title","post_excerpt","post_content"
                        ],
                        "like_text":"p&amp;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&amp;p",
                        "fields":[
                            "post_title","post_content","post_excerpt"
                        ],
                        "boost":2
                    }
                },
                {"fuzzy_like_this":{
                    "fields":[
                        "post_title","post_content","post_excerpt"
                    ],
                    "like_text":
                        "p&amp;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?

psorensen commented 9 years ago

Personally, I use a 'pre_get_posts' filter. I'd also be interested to hear how others implement this.

tlovett1 commented 9 years ago

Hey guys, sorry the documentation was incorrect. You need to use tax_query instead of category as the query param.

crebacz commented 9 years ago

Thank you! The updated documentation worked beautifully for me. Appreciate your help with this. Thanks psorenson for your input.