Search tags database

Hi, i’m trying have a search bar that allows multiple search in the database. Searching multiple category tags at the same time. We currently have a search bar that lets us search multiple tags but the problem is when searching, the tags should be in a pattern similar to how it is written on the database.

Example:
In the database, tags are written like “car, jeep, nature, youth, dark”
When we search " car, jeep, youth" - the video shows up because pattern is similar to what’s in the database
But if searched this way, “youth, car, jeep” - the video doesn’t show.

let lastSearchText
function filter(text) {
 if (lastSearchText !== text) {
 const filter = wixData.filter().contains('tags', text)
    $videosDataset.setFilter(filter)
    lastSearchText = text
  }
}

Hi,

See this thread

Hi Ido,

Thanks for that.

But it’s a little different from our problem… Our search bar works fine when you try to search a single tag but then when we try to search mutiple tags at the same time separated by a comma, it doesn’t work anymore… We would want to make it work that way as well…

This is the page - https://www.mooderi.com/video-library, maybe you could help us out.

Thanks!

Hey,

Actually, the thread referenced by Ido has the answer for that as well, right after the simpler case.
Basically, you should try something like this:

let searchWords = text.split(','); 
let filter = wixData.filter().contains('tags', searchWords[0]); 
for (let i=1; i < searchWords.length; i++) {
     filter = filter.contains('tags', searchWords[i]));
}
$videosDataset.setFilter(filter)

Good Luck,
Itai

Hi Itai,

Thanks for that… But i’m not sure what should i do or edit because the identifier “filter” has already been declared.

Here’s the code that we have so far for that page:


import wixData from 'wix-data'
const DATA_COLLECTION = 'Videos6'
const DROPDOWN_FIELD = 'author'

let $videosDataset
let $authorDropdown

$w.onReady(function() {
  $videosDataset = $w('#videosDataset')
  $authorDropdown = $w('#authorDropdown')
  setOptions($authorDropdown)
})

let debounceTimer
export function tagSearchInput_keyPress(ev, $w) {
 if (debounceTimer) {
    clearTimeout(debounceTimer)
    debounceTimer = null
  }
  debounceTimer = setTimeout(() => {
 const text = ev.target.value
    filter(text)
  }, 200)
}

export function authorDropdown_change(event, $w) {
 const value = event.target.value
 let filter
 if (value !== '0') {
    filter = wixData.filter().eq('author', value)
  } else {
    filter = wixData.filter().contains('author', '')
  }
  $videosDataset.setFilter(filter)
}

let lastSearchText
function filter(text) {
 if (lastSearchText !== text) {
 const filter = wixData.filter().contains('tags', text)
    $videosDataset.setFilter(filter)
    lastSearchText = text
  }
}

let isOptionsSet = false
function setOptions(dropdown) {
 if (isOptionsSet) {
 return
  }
 let options = dropdown.options
console.log('setting options', options.length)
 if (options.length === 1) {
 return setTimeout(() => setOptions(dropdown), 300)
  }
 const newOptions = []
  options = options.map(option => option.value).sort()
 for (let i = 0; i < options.length; i++) {
 if (options[i + 1] !== options[i]) {
      newOptions.push(options[i])
    }
  }
  dropdown.options = [{ label: 'all authors', value: '0' }].concat(
    newOptions.map(option => ({ label: option.toString(), value: option.toString() }))
  )
  isOptionsSet = true
}


Hope you could help us out. Thank you!

You can’t have more than 1 variable with the same name, i see you have at least 3 filters:
let filter
function filter(text) {
const filter = wixData.filter().contains(‘tags’, text)
Just change 2 of them to different names :slight_smile: