Skip to content

Conversation

@cewald
Copy link
Contributor

@cewald cewald commented Dec 28, 2019

Related issues

I needed a possibility to simply add an or/should filter to the quickSearchByQuery method.
This wasn't possible yet, so I extended the prepareElasticsearchQueryBody method of the ElasticSearch search-adapter to accept an or operator in the value property.
I also added an ES exists filter if the value is null if you just wan't the field to be null.

Short description and why it's useful

I had to make a product query where I wanted a collection of products with a specific value or an empty one. This wasn't possible yet because all filters are chained as 'AND' filters.

Now you can make query like this (or in the last two lines) to show all products with custom_attribute is 2 or 3 or custom_attribute is just empty (null):

let query = new SearchQuery()
query
  .applyFilter({ key: 'visibility', value: { in: [2, 3, 4] } })
  .applyFilter({ key: 'status', value: { in: [0, 1] } })
  .applyFilter({ key: 'category_ids', value: { in: [categoryId] } })
  .applyFilter({ key: 'custom_attribute', value: { or: null } })
  .applyFilter({ key: 'custom_attribute', value: { or: [2, 3] } })

In our use-case the products have clusters, so I wan't to show products to the customer which have his current cluster OR if there are not enough of them just regular products without a cluster.

With this changes I can make queries like this to the ES while I still can use all the advantages of quickSearchByQuery:

{
  "filter": {
    "bool": {
      "should": [
        {
          "terms": {
            "custom_attribute": [2, 3]
          }
        },
        {
          "bool": {
            "must_not": [
              {
                "exists": {
                  "field": "custom_attribute"
                }
              }
            ]
          }
        }
      ]
    }
  }
}

If you think it isn't an essential functionallity, just ignore and close this PR.
But I thought this might come in handy.

Which environment this relates to

Check your case. In case of any doubts please read about Release Cycle

  • Test version (https://test.storefrontcloud.io) - this is a new feature or improvement for Vue Storefront. I've created branch from develop branch and want to merge it back to develop
  • RC version (https://next.storefrontcloud.io) - this is a stabilisation fix for Release Candidate of Vue Storefront. I've created branch from release branch and want to merge it back to release
  • Stable version (https://demo.storefrontcloud.io) - this is an important fix for current stable version. I've created branch from hotfix or master branch and want to merge it back to hotfix

Upgrade Notes and Changelog

  • No upgrade steps required (100% backward compatibility and no breaking changes)
  • I've updated the Upgrade notes and Changelog on how to port existing VS sites with this new feature

IMPORTANT NOTICE - Remember to update CHANGELOG.md with description of your change

Contribution and currently important rules acceptance

@cewald
Copy link
Contributor Author

cewald commented Dec 28, 2019

@andrzejewsky This is the updated version of this PR #3834

@pkarw pkarw merged commit 4a0226c into vuestorefront:develop Dec 28, 2019
pkarw added a commit that referenced this pull request Dec 28, 2019
…-null-rebase

Fix typo for #3960 causing use of `filter` instead of `notFilter`
@cewald
Copy link
Contributor Author

cewald commented Dec 28, 2019

@pkarw @andrzejewsky I'm so sorry but I found a typo I added when I had to recreate the PR – but now you already merged it (thanks anyway 🙂).

I opened an new PR for that: #3961

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.

2 participants