Search a database and pull document as result

Hello-

Based on this article, https://support.wix.com/en/article/velo-tutorial-adding-collection-data-search-functionality, I created the following code for my site:

import wixData from “wix-data” ;

export function searchbutton_onClick(event)
{ // Runs a query on the “PMANewsletterArchive” collection
wixData.query( ‘PMANewsletterArchive’ )
.contains( ‘keyword’ ,$w( ‘#textBox1’ ).value)
.or(wixData.query( ‘PMANewsletterArchive’ ).contains( ‘keyword1’ ,$w( ‘#textBox1’ ).value))
.or(wixData.query( ‘PMANewsletterArchive’ ).contains( ‘keyword2’ ,$w( ‘#textBox1’ ).value))
.or(wixData.query( ‘PMANewsletterArchive’ ).contains( ‘keyword3’ ,$w( ‘#textBox1’ ).value))
.or(wixData.query( ‘PMANewsletterArchive’ ).contains( ‘keyword4’ ,$w( ‘#textBox1’ ).value))
.or(wixData.query( ‘PMANewsletterArchive’ ).contains( ‘keyword5’ ,$w( ‘#textBox1’ ).value))
.or(wixData.query( ‘PMANewsletterArchive’ ).contains( ‘keyword6’ ,$w( ‘#textBox1’ ).value))
.or(wixData.query( ‘PMANewsletterArchive’ ).contains( ‘keyword7’ ,$w( ‘#textBox1’ ).value))
.or(wixData.query( ‘PMANewsletterArchive’ ).contains( ‘keyword8’ ,$w( ‘#textBox1’ ).value))
.or(wixData.query( ‘PMANewsletterArchive’ ).contains( ‘keyword9’ ,$w( ‘#textBox1’ ).value))
.or(wixData.query( ‘PMANewsletterArchive’ ).contains( ‘keyword10’ ,$w( ‘#textBox1’ ).value))
.or(wixData.query( ‘PMANewsletterArchive’ ).contains( ‘keyword11’ ,$w( ‘#textBox1’ ).value))
.find( ‘pdf’ ) // Run the query
.then(res => {
// Set the table data to be the results of the query
$w( ‘#table1’ ).rows = res.items;

});

$w.onReady( function () {
$w( “#table1” ).columns = [
{
“id” : “col1” ,
“dataPath” : “pdf” ,
“label” : “pdf” ,
“type” : “document” ,
}
];
});
}

The idea is to search, and if that term is in the database in one of the keyword columns (keyword1, keyword2, keyword3, etc) the result would be the corresponding pdf in that row. Can anyone help me with this? It’s not returning anything.

Thanks!
Lisa

Hello Lisa.

The idea is good but the implementation is bad.
Instead of generating a lot of columns in your DATABASE, you should better work with —> “TAG-FIELD”…(here an example of TAG-FIELDS)…


You can put all your search-words into a single TAG-Field.

You will find an example for this 2x TAG-FIELDS and how it’s working here…
https://www.media-junkie.com/pflegeservice

Load the “Main-DB-Preset” first.

Thanks, but I was trying to follow the code in the article. Is it not possible to retrieve a document from your data collection when searching keywords in other fields?

Show me an example of your DB and i’ll show you the result.
I do not know how is structured your databse.

This is how I built the data collection. If someone searches a term in the search box that is in any of the keyword columns, it would return the pdf in that row.

Ok, there are 2-ways of how you could solve it…

  1. Way-1 is already READY…take a look here…


In this filtering version, you would use the —> “OR”-Filter…

Try it out! I already setted up an example-DB for you on SLOT-1/PRESET-1.


  1. Way-2 would be exactly the way you showed in your DB-example.
    Therefore you would use single Drop-Downs to make “AND-Filtering-combinations” to get the right PDF.

Both ways are possible with this Filter-Engine.

To be continued…:wink:

Second option…


The way you wanted it to have…

…using DropDowns and —> “AND-FILTERING”…

Ohhh sorry! I mean it is also a —> " OR-FILTER ". But as i recognized, my filtering-tool has a bug, which has to be fixed (OR-FILTERING just works for CheckBox-Group and not for DropDowns) DAMN (did not see this). :roll_eyes:

I will have to improve my Filtering-Engine :grin:.

By the way, i have expanded the Database (“PDF-Data”) [“keyword1”-“keyword4”]

To be continued…

Ok, i think now should everything working like expected. I think i could fix the bug in my filter-tool…(try it out) :wink: are these the functions you need?


Now it even would be able to combine and do a mix-filtering between both types of filtering :grin::grin::sunglasses:

Thanks for your help! How do I view the code for these ideas? I actually don’t want to use drop downs, just a search box utilizing all the keywords that return the associated pdfs.

You will find code-parts of this “filter-tool” by reading all the related posts which are given here…
https://www.media-junkie.com/databases

and some code-parts you also can get on the main-page of the “filter-tool”…
https://www.media-junkie.com/pflegeservice

Click onto ----> CODE and some starting code-parts will be shown in a boxed window.


Yes not all of code-parts are shown, because this would not be for free.

This is just an example, which shall show you what is possible to be made by Velo-Code.

If you need more informations on this project/topic, you can contact me.
You will find by using my PROFILE-PAGE here at velo-forum.

By the way:
A SEARCH-FUNCTION is also integrated in this “filter-tool”.

Thanks, but I feel like this is simpler than you present. I was hoping someone could give me guidance on my code with reinventing the wheel.

Thanks, but I feel like this is simpler than you present. I was hoping someone could give me guidance on my code with reinventing the wheel.

Back to your own code…

import wixData from "wix-data";

$w.onReady(()=>{
    createTable()
    $w('#searchbutton').onClick(()=>{
        searchFilter()
    })
})

function createTable() {
    $w("#table1").columns = [
        {
         "id": "col1",
         "dataPath": "pdf",
         "label": "pdf",
         "type": "document",
        }
   ];
}

function searchFilter(){
   let myQuery = wixData.query('PMANewsletterArchive')
   myQuery.contains('keyword',$w('#textBox1').value)
    .or(wixData.query('PMANewsletterArchive').contains('keyword1',$w('#textBox1').value))   .or(wixData.query('PMANewsletterArchive').contains('keyword2',$w('#textBox1').value))    .or(wixData.query('PMANewsletterArchive').contains('keyword3',$w('#textBox1').value))    .or(wixData.query('PMANewsletterArchive').contains('keyword4',$w('#textBox1').value))    .or(wixData.query('PMANewsletterArchive').contains('keyword5',$w('#textBox1').value))    .or(wixData.query('PMANewsletterArchive').contains('keyword6',$w('#textBox1').value))    .or(wixData.query('PMANewsletterArchive').contains('keyword7',$w('#textBox1').value))    .or(wixData.query('PMANewsletterArchive').contains('keyword8',$w('#textBox1').value))    .or(wixData.query('PMANewsletterArchive').contains('keyword9',$w('#textBox1').value))    .or(wixData.query('PMANewsletterArchive').contains('keyword10',$w('#textBox1').value))    .or(wixData.query('PMANewsletterArchive').contains('keyword11',$w('#textBox1').value))
    .find() 
    .then((res)=> {   
        console.log(res)
        let items = res.items
        console.log(items)
        $w('#table1').rows = ritems
    })
}

Take a look into CONSOLE for RESULTS.

Continue to code your search-filter.

Good luck and happy coding.