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

added 2 new facets plugins for processor and query_type. Together the… #235

Conversation

snpower
Copy link
Contributor

@snpower snpower commented Oct 3, 2022

…y provide a new pre_query processor on facets to AND facet groups together while still allowing OR within a facet group. This is based on initial code provided by Andy Broomfield

…y provide a new pre_query processor on facets to AND facet groups together while still allowing OR within a facet group. This is based on initial code provided by Andy Broomfield
@snpower snpower linked an issue Oct 3, 2022 that may be closed by this pull request
@finnlewis
Copy link
Member

Thanks @snpower ! @andybroomfield and @ekes said they'd look and @ekes mentioned we probably want some tests.

@andybroomfield
Copy link
Contributor

Looks good from testing so far, thanks @snpower.

Before, the Just right listing should not show.

Screenshot 2022-10-04 at 1 37 22 pm

With the And filter, it is now filtered when over 18s is checked.

Screenshot 2022-10-04 at 1 38 14 pm

But returns as expected when under 18s checked.

Screenshot 2022-10-04 at 1 38 31 pm

@markconroy
Copy link
Member

@finnlewis I nabbed Stella's time over the weekend when she was offically not working! Any chance someone else from the LGD team would be able to put the necessary tests for this together; I have a feeling that might be a quicker way for us to get this complete and merged?

@finnlewis
Copy link
Member

@andybroomfield not totally clear by your screen shots - is the first shot showing that it does not work or is it all good?

@finnlewis
Copy link
Member

@Adnan-cds would you like to take a look at this too? Hoping to get it merged soon.

@andybroomfield
Copy link
Contributor

@finnlewis that is the before screenshot.
However the localgov and filter has to be enabled first (second screenshot).

@ekes
Copy link
Member

ekes commented Oct 6, 2022

@andybroomfield 's looking at adding a Functional test. I've had a quick look at doing a Kernel one, but it's probably more 'prophesy' than is worth it. But while doing so I did come up with a couple of questions I'll pop in a review.

/**
* {@inheritdoc}
*/
public function preQuery(FacetInterface $facet) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry, is which part needed?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The content of the preQuery just seems to getActiveItems from the $facet and then set the same value back?
Or does this cause the facet to do something, looking it the class it doesn't seem to.

* {@inheritdoc}
*/
public function build() {
$query_operator = $this->facet->getQueryOperator();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like it's very similar to the Drupal\facets\Plugin\facets\query_type\SearchApiString without the missing facet error handling. Can it inherit that, rather than base, and just override the execute() that is different?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, it probably could alright

Copy link
Member

@ekes ekes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@andybroomfield Think we were agreed this should be enabled on the facet by default. So I've added it here.

@finnlewis
Copy link
Member

@andybroomfield has not had a chance to write tests for this yet, so we're not merging just yet, sorry!

This test creates 4 facets in two groups, and four directory entries in
a test directory channel.
It then tries to click on each facet, testing that the facets work
(in the default configuration) as OR facets within the group,
and AND facets accross the facet groups.
@finnlewis
Copy link
Member

I might be missing the point, but I don't think this is working quite as I would expect.

I have a few items.

  • English Government: (Country: England, Org type: Government association)
  • Scottish Government: (Country: Scotland, Org type: Government association)
  • Welsh Government: (Country: Wales, Org type: Government association)
  • Scottish Local Authority: (Country: Scotland, Org type: Local Authority)

image

When I check England, I see only the England result, which is expected, but I can no longer see the other facets to be able to select Scotland:

image

I think what I am trying to do is be able to select Enland and Scotland to be able see a list of items that are tagged Enland OR Scotland.

Then be able to reduce that set by selecting an Organisation Type.

Is there a way we can get the other facets with a group to be displayed?

@andybroomfield
Copy link
Contributor

@finnlewis
Copy link
Member

finnlewis commented Oct 17, 2022

@markconroy could you run this by your team and explore the UX that I describe above? Is this intended? Can we find a way to work around this specific case?

@3mkay ?

@andybroomfield
Copy link
Contributor

The quick and dirty method is just to re-run the query without facets, since that is what is used to generate them (means all applied facets are visible).

  /**
   * {@inheritdoc}
   */
  public function build() {
    $query_operator = $this->facet->getQueryOperator();
    $temp_query = $this->query->getOriginalQuery();
    // dpm(get_class_methods($temp_query));
    $temp_query->preExecute();
    // dpm(array_keys($temp_query->getOptions()));
    // dpm($this->query->getConditionGroup()->getConditions());
    $conditions = &$temp_query->getConditionGroup()->getConditions();
    // dpm($conditions);
    foreach ($conditions as $key => $condition) {
      if ($condition instanceof \Drupal\search_api\Query\ConditionGroupInterface) {
        $tags = $condition->getTags();
        foreach($tags as $tag) {
          if (strpos($tag, 'facet:localgov_directory_facets_filter.') === 0) {
            unset($conditions[$key]);
          }
        }
      }
    }
    $temp_query->execute();
    $avalible_facets = $temp_query->getResults()->getExtraData('search_api_facets');
    $results = $avalible_facets['localgov_directory_facets_filter'] ?? $this->results;

    if (!empty($results)) {
      $facet_results = [];
      foreach ($results as $result) {
        if ($result['count'] || $query_operator === 'or') {
          $result_filter = $result['filter'] ?? '';
          if ($result_filter[0] === '"') {
            $result_filter = substr($result_filter, 1);
          }
          if ($result_filter[strlen($result_filter) - 1] === '"') {
            $result_filter = substr($result_filter, 0, -1);
          }
          $count = $result['count'];
          $result = new Result($this->facet, $result_filter, $result_filter, $count);
          $facet_results[] = $result;
        }
      }
      $this->facet->setResults($facet_results);
    }

    return $this->facet;
  }

@markconroy
Copy link
Member

I had a chat with @3mkay about this, and it's starting to look like

  1. the facets are working the way they should, and
  2. what is being asked for is "filters" and not "facets"

@3mkay can you chip in if there's any further details needed?

@finnlewis
Copy link
Member

From further discussion, it sounds like the behaviour I outlined above is expected.

@ekes is happy for the motivation on this and we can either merge this in as is, or see if @andybroomfield's approach above will make it even better!

@markconroy confirmed that there is no a huge rush for this, they already have it working.

@andybroomfield
Copy link
Contributor

I've started a new branch with a refined version of this code.
feature/193-use-an-and-query-between-facet-groups-and-or-within-them-filter

@finnlewis
Copy link
Member

Discussing at Merge Monday.

@andybroomfield is keen to know how this is playing out at Essex. @3mkay do you know who to talk to to find out how this is being used?

Our motivation is to understand whether to go with how this pull request is working as is, or explore the branch that @andybroomfield created with the enhancements.

@andybroomfield
Copy link
Contributor

Just to add, I would be happy with this functionality to go out sooner and we can then work a fix on my other branch. My hunch is that real world use the issue above is not something that is relevant as much as most directory entries have good cross catogrisation accross the filters.

@andybroomfield
Copy link
Contributor

Here is an example of where it might be an issue, though it took a few ticks to get there.
https://send.essex.gov.uk/search-support-groups-and-activities?f%5B0%5D=localgov_directories_facets%3A3&f%5B1%5D=localgov_directories_facets%3A19&f%5B2%5D=localgov_directories_facets%3A20&f%5B3%5D=localgov_directories_facets%3A21&f%5B4%5D=localgov_directories_facets%3A27&f%5B5%5D=localgov_directories_facets%3A57

As a user, I would expect to now expand my options for areas covered, special needs and support type.

Screenshot 2022-11-15 at 2 33 00 pm

@finnlewis
Copy link
Member

We're keen to push forward with this and bring it in if it is an improvement.

@andybroomfield wants to have a look at it again.

In practise we think this is good to go ahead with.

@andybroomfield will test soon with some live BHCC directoires.

@finnlewis
Copy link
Member

Update from Merge Monday call, @andybroomfield is hoping to test with real live directories in the next week or two.

Otherwise we're all happy to merge this.

Watch this space!

@finnlewis
Copy link
Member

On @andybroomfield 's backlog for the forthcoming sprint, where they'll be looking at directoires.

@finnlewis
Copy link
Member

@andybroomfield and team in the process of testing this and working on some changes which will come back as a PR into this branch.

andybroomfield added a commit that referenced this pull request Mar 17, 2023
This is to address the issue in this comment
  #235 (comment)

Instead of just using the facets from the result set, re-run the query, each
time eliminating all the facets except each group, and filter out its own
so there is a list of each sibling facet that could be set with an OR that
also mathces an AND in the other group.
This then produces a filtered list of sibling facets that should be reachable
within the AND accross the groups and OR within them.
@finnlewis
Copy link
Member

@ekes is working on #269 with @andybroomfield

Once that is merged into this branch we should be a good to go! :)

@willguv
Copy link
Member

willguv commented Apr 17, 2023

@finnlewis @andybroomfield can I see this working somewhere? Many thanks!

@finnlewis
Copy link
Member

Interesting question @willguv, I wonder if we can spin up a Gitpod instance with this branch to demonstrate?

@finnlewis
Copy link
Member

@andybroomfield will get back to this at some point, but not top of the list. Mainly just needs to review comment from @ekes above.

@finnlewis
Copy link
Member

Just checking in on this at Merge Monday. @andybroomfield is hoping to be able to progress the other pull request #269 and then get back to this one.

From discussion today, we think we should re-target this to the 3.x branch to support Drupal 10 initially, then back-port to the 2.x / Drupal 9 branch if necessary.

@snpower @markconroy any issues with re-targeting this pull request to the 3.x branch by creating a new branch and new pull request?

@markconroy
Copy link
Member

Fine with me.

@andybroomfield
Copy link
Contributor

Would it b e ok if I rebased this to 3.x?

@markconroy
Copy link
Member

I don't see any issue with that.

@stephen-cox stephen-cox removed their request for review October 20, 2023 12:37
@andybroomfield andybroomfield marked this pull request as draft November 4, 2023 20:47
@andybroomfield
Copy link
Contributor

I've converted this back to draft as it needs to be rebased on 3.x.
Would there be any objection to merging in #2141 as there is progress being made on that now and we then only have one PR to review?

@finnlewis
Copy link
Member

Discussing this in Tech Drop-in, @NDITGC needs this!

@andybroomfield and @ekes in helpting to test this and #235 to bring into the fold.

Note: @NDITGC is still on Drupal 9, so it would be valuable to get this merged to the Drupal 9 branches and Drupal 10.

@finnlewis finnlewis marked this pull request as ready for review November 9, 2023 15:34
andybroomfield added a commit that referenced this pull request Nov 10, 2023
This is to address the issue in this comment
  #235 (comment)

Instead of just using the facets from the result set, re-run the query, each
time eliminating all the facets except each group, and filter out its own
so there is a list of each sibling facet that could be set with an OR that
also mathces an AND in the other group.
This then produces a filtered list of sibling facets that should be reachable
within the AND accross the groups and OR within them.
@finnlewis
Copy link
Member

@andybroomfield and @snpower is this one now redundant as we merged #325 ?

@finnlewis
Copy link
Member

Closing this - thanks for the patience!

@finnlewis finnlewis closed this Dec 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Development

Successfully merging this pull request may close these issues.

Use an 'and' query between facet groups and 'or' within them.
7 participants