10up / ElasticPress

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

Woocomerce Product Search return irrelevant search results #737

Closed ivankristianto closed 7 years ago

ivankristianto commented 7 years ago

Query Args:

array (size=73)
  's' => string 'Red Harvest' (length=11)
  'post_type' => string 'product' (length=7)
  'error' => string '' (length=0)
  'm' => string '' (length=0)
  'p' => int 0
  'post_parent' => string '' (length=0)
  'subpost' => string '' (length=0)
  'subpost_id' => string '' (length=0)
  'attachment' => string '' (length=0)
  'attachment_id' => int 0
  'name' => string '' (length=0)
  'static' => string '' (length=0)
  'pagename' => string '' (length=0)
  'page_id' => int 0
  'second' => string '' (length=0)
  'minute' => string '' (length=0)
  'hour' => string '' (length=0)
  'day' => int 0
  'monthnum' => int 0
  'year' => int 0
  'w' => int 0
  'category_name' => string '' (length=0)
  'tag' => string '' (length=0)
  'cat' => string '' (length=0)
  'tag_id' => string '' (length=0)
  'author' => string '' (length=0)
  'author_name' => string '' (length=0)
  'feed' => string '' (length=0)
  'tb' => string '' (length=0)
  'paged' => int 0
  'meta_key' => string '' (length=0)
  'meta_value' => string '' (length=0)
  'preview' => string '' (length=0)
  'sentence' => string '' (length=0)
  'title' => string '' (length=0)
  'fields' => string '' (length=0)
  'menu_order' => string '' (length=0)
  'embed' => string '' (length=0)
  'category__in' => 
    array (size=0)
      empty
  'category__not_in' => 
    array (size=0)
      empty
  'category__and' => 
    array (size=0)
      empty
  'post__in' => 
    array (size=0)
      empty
  'post__not_in' => 
    array (size=0)
      empty
  'post_name__in' => 
    array (size=0)
      empty
  'tag__in' => 
    array (size=0)
      empty
  'tag__not_in' => 
    array (size=0)
      empty
  'tag__and' => 
    array (size=0)
      empty
  'tag_slug__in' => 
    array (size=0)
      empty
  'tag_slug__and' => 
    array (size=0)
      empty
  'post_parent__in' => 
    array (size=0)
      empty
  'post_parent__not_in' => 
    array (size=0)
      empty
  'author__in' => 
    array (size=0)
      empty
  'author__not_in' => 
    array (size=0)
      empty
  'cache_results' => boolean false
  'orderby' => boolean false
  'order' => string 'ASC' (length=3)
  'meta_query' => 
    array (size=1)
      'visibility' => 
        array (size=3)
          'key' => string '_visibility' (length=11)
          'value' => 
            array (size=2)
              0 => string 'visible' (length=7)
              1 => string 'search' (length=6)
          'compare' => string 'IN' (length=2)
  'tax_query' => 
    array (size=0)
      empty
  'posts_per_page' => int 10
  'wc_query' => string 'product_query' (length=13)
  'search_fields' => 
    array (size=6)
      0 => string 'post_title' (length=10)
      1 => string 'post_content' (length=12)
      2 => string 'post_excerpt' (length=12)
      3 => string 'author_name' (length=11)
      'taxonomies' => 
        array (size=4)
          0 => string 'category' (length=8)
          1 => string 'post_tag' (length=8)
          2 => string 'product_tag' (length=11)
          3 => string 'product_cat' (length=11)
      'meta' => 
        array (size=1)
          0 => string '_sku' (length=4)
  'suppress_filters' => boolean false
  'ep_integrate' => boolean true
  'ignore_sticky_posts' => boolean false
  'update_post_term_cache' => boolean true
  'lazy_load_term_meta' => boolean true
  'update_post_meta_cache' => boolean true
  'nopaging' => boolean false
  'comments_per_page' => string '50' (length=2)
  'no_found_rows' => boolean false
  'search_terms_count' => int 2
  'search_terms' => 
    array (size=2)
      0 => string 'Red' (length=3)
      1 => string 'Harvest' (length=7)
  'search_orderby_title' => 
    array (size=2)
      0 => string 'wp_posts.post_title LIKE '%Red%'' (length=32)
      1 => string 'wp_posts.post_title LIKE '%Harvest%'' (length=36)

Query Body:

{
    "from": 0,
    "size": 10,
    "sort": [
        {
            "_score": {
                "order": "asc"
            }
        }
    ],
    "query": {
        "function_score": {
            "query": {
                "bool": {
                    "should": [
                        {
                            "multi_match": {
                                "query": "Red Harvest",
                                "type": "phrase",
                                "fields": [
                                    "post_title",
                                    "post_content",
                                    "post_excerpt",
                                    "author_name",
                                    "terms.category.name",
                                    "terms.post_tag.name",
                                    "terms.product_tag.name",
                                    "terms.product_cat.name",
                                    "meta._sku.value",
                                    "post_author.login"
                                ],
                                "boost": 4
                            }
                        },
                        {
                            "multi_match": {
                                "query": "Red Harvest",
                                "fields": [
                                    "post_title",
                                    "post_content",
                                    "post_excerpt",
                                    "author_name",
                                    "terms.category.name",
                                    "terms.post_tag.name",
                                    "terms.product_tag.name",
                                    "terms.product_cat.name",
                                    "meta._sku.value",
                                    "post_author.login"
                                ],
                                "boost": 2,
                                "fuzziness": 0,
                                "operator": "and"
                            }
                        },
                        {
                            "multi_match": {
                                "fields": [
                                    "post_title",
                                    "post_content",
                                    "post_excerpt",
                                    "author_name",
                                    "terms.category.name",
                                    "terms.post_tag.name",
                                    "terms.product_tag.name",
                                    "terms.product_cat.name",
                                    "meta._sku.value",
                                    "post_author.login"
                                ],
                                "query": "Red Harvest",
                                "fuzziness": 1
                            }
                        }
                    ]
                }
            },
            "exp": {
                "post_date_gmt": {
                    "scale": "14d",
                    "decay": 0.25,
                    "offset": "7d"
                }
            },
            "score_mode": "avg",
            "boost_mode": "sum"
        }
    },
    "post_filter": {
        "bool": {
            "must": [
                {
                    "bool": {
                        "must": [
                            {
                                "terms": {
                                    "meta._visibility.raw": [
                                        "visible",
                                        "search"
                                    ]
                                }
                            }
                        ]
                    }
                },
                {
                    "term": {
                        "post_type.raw": "product"
                    }
                },
                {
                    "term": {
                        "post_status": "publish"
                    }
                }
            ]
        }
    },
    "explain": true
}

The first 10 results are nothing relevant with the search keyword.

But in Admin Area, it return the correct results: screen shot 2017-02-27 at 23 18 17

ENV: WP 4.2.2 EP 2.2 ES 5.2.0

ivankristianto commented 7 years ago

Turns out the default sorting of Woocommerce Product Search is order "ASC" This will tell elasticsearch to sort it by product ID asc, instead of the most relevant results. @tlovett1 should we modified the query args for WC product search using our filter?