Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds 'category__in' support for the post searches - fixes 2406 #2407

Closed
wants to merge 2 commits into from

Conversation

sathyapulse
Copy link
Contributor

Description of the Change

Adds category__in support for the post searches.

Benefits

Users can use the category__in query argument alternative to the tax_query.

Possible Drawbacks

NA

Verification Process

Set the category__in query argument in the pre_get_posts filter and checked the returned results. It returned unexpected results initially. After the fix has been applied the search returned the expected results. Please find the generated ES query below.

Before:

{
  "from": 0,
  "size": 10,
  "sort": [
    {
      "_score": {
        "order": "desc"
      }
    }
  ],
  "query": {
    "function_score": {
      "query": {
        "bool": {
          "should": [
            {
              "bool": {
                "must": [
                  {
                    "bool": {
                      "should": [
                        {
                          "multi_match": {
                            "query": "How to prepare the report",
                            "type": "phrase",
                            "fields": [
                              "post_title^1",
                              "post_excerpt^1",
                              "post_content^1",
                              "post_author.display_name^1",
                              "terms.category.name^1",
                              "terms.ep_custom_result.name^9999"
                            ],
                            "boost": 3
                          }
                        },
                        {
                          "multi_match": {
                            "query": "How to prepare the report",
                            "fields": [
                              "post_title^1",
                              "post_excerpt^1",
                              "post_content^1",
                              "post_author.display_name^1",
                              "terms.category.name^1",
                              "terms.ep_custom_result.name^9999"
                            ],
                            "type": "phrase",
                            "slop": 5
                          }
                        }
                      ]
                    }
                  }
                ],
                "filter": [
                  {
                    "match": {
                      "post_type.raw": "article"
                    }
                  }
                ]
              }
            }
          ]
        }
      },
      "functions": [
        {
          "gauss": {
            "post_date_gmt": {
              "scale": "360d",
              "decay": 0.9,
              "offset": "0d"
            }
          }
        },
        {
          "weight": 0.001
        }
      ],
      "score_mode": "multiply",
      "boost_mode": "multiply"
    }
  },
  "post_filter": {
    "bool": {
      "must": [
        {
          "bool": {
            "must": [
              {
                "terms": {
                  "terms.category.slug": [
                    "analyse"
                  ]
                }
              },
              {
                "terms": {
                  "terms.category.term_id": [
                    3497
                  ]
                }
              }
            ]
          }
        },
        {
          "terms": {
            "post_type.raw": [
              "article"
            ]
          }
        },
        {
          "terms": {
            "post_status": [
              "publish"
            ]
          }
        }
      ]
    }
  },
  "track_total_hits": true
}

After:

{
  "from": 0,
  "size": 10,
  "sort": [
    {
      "_score": {
        "order": "desc"
      }
    }
  ],
  "query": {
    "function_score": {
      "query": {
        "bool": {
          "should": [
            {
              "bool": {
                "must": [
                  {
                    "bool": {
                      "should": [
                        {
                          "multi_match": {
                            "query": "How to prepare the report",
                            "type": "phrase",
                            "fields": [
                              "post_title^1",
                              "post_excerpt^1",
                              "post_content^1",
                              "post_author.display_name^1",
                              "terms.category.name^1",
                              "terms.ep_custom_result.name^9999"
                            ],
                            "boost": 3
                          }
                        },
                        {
                          "multi_match": {
                            "query": "How to prepare the report",
                            "fields": [
                              "post_title^1",
                              "post_excerpt^1",
                              "post_content^1",
                              "post_author.display_name^1",
                              "terms.category.name^1",
                              "terms.ep_custom_result.name^9999"
                            ],
                            "type": "phrase",
                            "slop": 5
                          }
                        }
                      ]
                    }
                  }
                ],
                "filter": [
                  {
                    "match": {
                      "post_type.raw": "article"
                    }
                  }
                ]
              }
            }
          ]
        }
      },
      "functions": [
        {
          "gauss": {
            "post_date_gmt": {
              "scale": "360d",
              "decay": 0.9,
              "offset": "0d"
            }
          }
        },
        {
          "weight": 0.001
        }
      ],
      "score_mode": "multiply",
      "boost_mode": "multiply"
    }
  },
  "post_filter": {
    "bool": {
      "must": [
        {
          "bool": {
            "must": {
              "terms": {
                "terms.category.term_id": [
                  3497,
                  3490,
                  3498,
                  3499,
                  3481,
                  3502,
                  3508,
                  3487,
                  3501,
                  3495,
                  3504,
                  3485,
                  3500,
                  3494,
                  3496,
                  3491,
                  3479,
                  3484,
                  3509,
                  3492,
                  3483,
                  3503,
                  3493,
                  3486,
                  3482,
                  3480,
                  3489,
                  3488,
                  3505,
                  3507,
                  3506,
                  1
                ]
              }
            }
          }
        },
        {
          "terms": {
            "post_type.raw": [
              "article"
            ]
          }
        },
        {
          "terms": {
            "post_status": [
              "publish"
            ]
          }
        }
      ]
    }
  },
  "track_total_hits": true
}

Checklist:

  • I have read the CONTRIBUTING document.
  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added tests to cover my change.
  • All new and existing tests passed.

Applicable Issues

#2406

Changelog Entry

Adds category_in support for the post search.

@brandwaffle
Copy link
Contributor

@sathyapulse would you mind testing #2419 to see if this resolves your issue? Thanks in advance!

@sathyapulse
Copy link
Contributor Author

@sathyapulse would you mind testing #2419 to see if this resolves your issue? Thanks in advance!

@brandwaffle Apologies for the delay from my end. Thank you for working on that. I've validated this particular case, and it resolves the problem and works as expected with the fix.

@felipeelia
Copy link
Member

Closed by #2419

@felipeelia felipeelia closed this Mar 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

category__in search support for the post searches
3 participants