Create search function in velo with mutliple contains

Question:
I’ve created a search function in velo, so that websites-guests can seach by topic. I tried to embedd the possibility to search for more than one field, but this doesn’t work. The goal is, that in one search field, the user can search for whatever he wants. as example:

if the collection is an adress-list, I want to have the possibility, that the user can put either the name, the city, the street or the country. not depending on which of them he is putting in, the searchbar should find the appropriate entries. how to do this?

This is the current code:

import wixData from "wix-data";

$w('#suche').onClick((event) => {
  wixData
        .query("Verbandsmitglieder")
          .contains("title", $w("#suchfeld").value)
        .find()
        .then((results) => {
         if (results.totalCount > 0) {
         $w("#suchresultate").data = results.items;
         }
        })
        .catch((error) => {
         console.error(error);
        });  
  
  $w("#suchresultate").onItemReady(($item, itemData, index) => {
        $item("#title").text = itemData.title;
        $item("#strasse").text = itemData.strasse;
        $item("#ort").text = itemData.ort;
        $item("#webseite").link = itemData.webseite;
  });
});

I tried with multiple contains:

import wixData from "wix-data";

$w('#suche').onClick((event) => {
  wixData
        .query("Verbandsmitglieder")
          .contains("title", $w("#suchfeld").value)
          .contains("ort", $w("#suchfeld").value)
          .contains("kanton", $w("#suchfeld").value)
        .find()
        .then((results) => {
         if (results.totalCount > 0) {
         $w("#suchresultate").data = results.items;
         }
        })
        .catch((error) => {
         console.error(error);
        });  
  
  $w("#suchresultate").onItemReady(($item, itemData, index) => {
        $item("#title").text = itemData.title;
        $item("#strasse").text = itemData.strasse;
        $item("#ort").text = itemData.ort;
        $item("#webseite").link = itemData.webseite;
  });
});

But it doens’t work. the searchbar works only for the last contains entry.

Product:
Wix Editor

What are you trying to achieve:
See question

Hi, Roberto_Bortoli !!

If you want to achieve what you’re aiming for, you’ll likely need to connect the queries using .or(). I’ll include a reference link below, so please check it out for more details. :wink:

https://dev.wix.com/docs/velo/api-reference/wix-data/wix-data-query/or

I think something like this might be necessary…


let query1 = wixData.query("Verbandsmitglieder").contains("title", $w("#suchfeld").value);
let query2 =wixData.query("Verbandsmitglieder").contains("ort", $w("#suchfeld").value); 
let newQuery = query1.or(query2);

newQuery.find()
       .then((results) => {
           if (results.totalCount > 0) {
               $w("#suchresultate").data = results.items;
           }
        })
        .catch((error) => {
         console.error(error);
        });  

That’s great onemoretime. Thanks a lot for your support - it works. :slight_smile:

1 Like

Enjoy Wix together !! :grin:

P.S.

I forgot to mention earlier, but if possible, I think the onItemReady callback should be set up before the query. Therefore, it would be better to place it outside the onClick event. :thinking:

yes, you’re right. Will change that.

I’m struggling with the property types. What is the property for the field type “Number”. Or what ist the property for the field type “Tags”?