Hello Gustav Sl,
Welcome to the Microsoft Q&A and thank you for posting your questions here.
I understand that your Regex Queries works in Azure Portal but not with AzSearch.Automagic and
pagination, facets, filters, and built-in query handling are lost when bypassing Automagic.
To achieve regex support while preserving Automagic's features, you can extend the Automagic library to handle regex queries. These are a few steps you will need to do it:
- You need to modify the Automagic library to include regex query support. This involves extending the existing query handling mechanism and, clone the AzSearch.Automagic library from its repository to your local development environment.
- Locate the part of the library where queries are constructed and modify it to include regex support. You can add a condition to check for a regex query type and handle it accordingly.
// Example modification in the query construction function buildQuery(queryType, searchText) { let query = {}; if (queryType === 'regex') { query = { queryType: 'full', search: searchText, searchMode: 'all', queryType: 'regex' }; } else { query = { queryType: 'semantic', search: searchText, searchMode: 'all' }; } return query; }
- Locate the part of the library where queries are constructed and modify it to include regex support. You can add a condition to check for a regex query type and handle it accordingly.
- Implement a toggle button in your web application to switch between
queryType: full
(with regex) andqueryType: semantic
.- HTML for Toggle Button: <button id="toggleQueryType">Toggle Query Type</button>\
- JavaScript for Toggle Functionality:
let currentQueryType = 'semantic'; document.getElementById('toggleQueryType').addEventListener('click', function() { currentQueryType = currentQueryType === 'semantic' ? 'regex' : 'semantic'; console.log('Query type switched to:', currentQueryType); }); function executeSearch(searchText) { const query = buildQuery(currentQueryType, searchText); automagic.search(query); }
- Make sure that the modified Automagic library is used in your web application and that the search execution function utilizes the extended query handling.
- To initialize Automagic:
var automagic = new AzSearch.Automagic({ index: 'your-index-name', queryKey: 'your-query-key', service: 'your-service-name' }); automagic.addSearchBox('searchBox', { highlightPreTag: '<b>', highlightPostTag: '</b>', suggesterName: 'sg', select: 'number,street,city,region,countryCode' }); automagic.addResults('results'); automagic.addPager('pager'); automagic.addRangeFacet('sqftFacet', 'sqft', 'number', 0, 17000); automagic.addCheckboxFacet('bedsFacet', 'beds', 'number'); automagic.addCheckboxFacet('bathsFacet', 'baths', 'number'); automagic.addCheckboxFacet('typeFacet', 'type', 'string'); automagic.addCheckboxFacet('tagsFacet', 'tags', 'collection');
- To execute Search with Extended Query Handling:
document.getElementById('searchButton').addEventListener('click', function() { const searchText = automagic.searchBox.getValue(); executeSearch(searchText); });
- To initialize Automagic:
I hope this is helpful! Do not hesitate to let me know if you have any other questions.
Please don't forget to close up the thread here by upvoting and accept it as an answer if it is helpful.