How can I match "laptop" and "laptops" when search for "laptop" is performed using MySQL's
Date : March 29 2020, 07:55 AM
I wish did fix the issue. If your requirements are more general, requiring full stemming support e.g. matching "testing" or "tests" against test in the full text index for example then you can use an alternative full text index plugin. A google search pulls up a number of possibilities although a quick glance suggests that most are commercial rather than open source.
|
How to create a "Next Page" link to the "non-displayed" results of mySQL search?
Tag : php , By : user179863
Date : March 29 2020, 07:55 AM
To fix this issue I have a search engine on my webpage (PHP). So far, it would show all results on the same result page. I want to limit the number of results to 20 per page, and create a "Next Page" link... <?php<br>
// database connection info<br>
$conn = mysql_connect('localhost','dbusername','dbpass') or trigger_error("SQL", E_USER_ERROR);<br>
$db = mysql_select_db('dbname',$conn) or trigger_error("SQL", E_USER_ERROR);<br>
// find out how many rows are in the table <br>
$sql = "SELECT COUNT(*) FROM numbers";<br>
$result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);<br>
$r = mysql_fetch_row($result);<br>
$numrows = $r[0];<br>
// number of rows to show per page<br>
$rowsperpage = 10;<br>
// find out total pages
$totalpages = ceil($numrows / $rowsperpage);<br>
// get the current page or set a default<br>
if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) {<br>
// cast var as int<br>
$currentpage = (int) $_GET['currentpage'];<br>
} else {<br>
// default page num<br>
$currentpage = 1;<br>
} // end if<br>
// if current page is greater than total pages...<br>
if ($currentpage > $totalpages) {<br>
// set current page to last page<br>
$currentpage = $totalpages;<br>
} // end if<br>
// if current page is less than first page...<br>
if ($currentpage < 1) {<br>
// set current page to first page<br>
$currentpage = 1;<br>
} // end if<br>
// the offset of the list, based on current page <br>
$offset = ($currentpage - 1) * $rowsperpage;<br>
// get the info from the db <br>
$sql = "SELECT id, number FROM numbers LIMIT $offset, $rowsperpage";<br>
$result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);<br>
// while there are rows to be fetched...<br>
while ($list = mysql_fetch_assoc($result)) {<br>
// echo data<br>
echo $list['id'] . " : " . $list['number'] . "<br />";<br>
} // end while
/****** build the pagination links ******/<br>
// range of num links to show<br>
$range = 3;<br>
// if not on page 1, don't show back links<br>
if ($currentpage > 1) {<br>
// show << link to go back to page 1<br>
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=1'><<</a> ";<br>
// get previous page num<br>
$prevpage = $currentpage - 1;<br>
// show < link to go back to 1 page<br>
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage'><</a> ";<br>
} // end if <br>
// loop to show links to range of pages around current page<br>
for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) {<br>
// if it's a valid page number...<br>
if (($x > 0) && ($x <= $totalpages)) {<br>
// if we're on current page...<br>
if ($x == $currentpage) {<br>
// 'highlight' it but don't make a link<br>
echo " [<b>$x</b>] ";<br>
// if not current page...<br>
} else {<br>
// make it a link<br>
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$x'>$x</a> ";<br>
} // end else<br>
} // end if <br>
} // end for<br>
// if not on last page, show forward and last page links <br>
if ($currentpage != $totalpages) {<br>
// get next page<br>
$nextpage = $currentpage + 1;<br>
// echo forward link for next page <br>
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$nextpage'>></a> ";<br>
// echo forward link for lastpage
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$totalpages'>>></a> ";<br>
} // end if<br>
/****** end build pagination links ******/<br>
?>
|
How can I make Django search in multiple fields using QuerySets and MySql "Full Text Search"?
Tag : mysql , By : drbillll
Date : March 29 2020, 07:55 AM
This might help you I'm a Django novice, trying to create a "Search" form for my project using MySql with MyISAM engine. So far, I manage to get the form to work, but Django doesn't seem to search all fields the same way. Results are random. Exemple: search in region returns no result or worst search in description works fine while howtogetin doesn't seem to apply.. , I'd recommend you to implement this: #views.py
def normalize_query(query_string,
findterms=re.compile(r'"([^"]+)"|(\S+)').findall,
normspace=re.compile(r'\s{2,}').sub):
'''
Splits the query string in invidual keywords, getting rid of unecessary spaces and grouping quoted words together.
Example:
>>> normalize_query(' some random words "with quotes " and spaces')
['some', 'random', 'words', 'with quotes', 'and', 'spaces']
'''
return [normspace(' ',(t[0] or t[1]).strip()) for t in findterms(query_string)]
def get_query(query_string, search_fields):
'''
Returns a query, that is a combination of Q objects.
That combination aims to search keywords within a model by testing the given search fields.
'''
query = None # Query to search for every search term
terms = normalize_query(query_string)
for term in terms:
or_query = None # Query to search for a given term in each field
for field_name in search_fields:
q = Q(**{"%s__icontains" % field_name: term})
if or_query is None:
or_query = q
else:
or_query = or_query | q
if query is None:
query = or_query
else:
query = query & or_query
return query
#views.py
def search_for_something(request):
query_string = ''
found_entries = None
if ('q' in request.GET) and request.GET['q'].strip():
query_string = request.GET['q']
entry_query = get_query(query_string, ['field1', 'field2', 'field3'])
found_entries = Model.objects.filter(entry_query).order_by('-something')
return render_to_response('app/template-result.html',
{ 'query_string': query_string, 'found_entries': found_entries },
context_instance=RequestContext(request)
)
#template.html
<form class="" method="get" action="{% url 'search_for_something' model.pk %}">
<input name="q" id="id_q" type="text" class="form-control" placeholder="Search" />
<button type="submit">Search</button>
</form>
#template-result.html
{% if found_entries %}
{% for field in found_entries %}
{{ model.field }}
{% endfor %}
{% endif %}
#urls.py
url(r'^results/$', 'app.views.search_for_something', name='search_for_something'),
|
Search text "\**" throws error "Failed to parse query string." but "\*", "\*~" a
Tag : azure , By : dormsbee
Date : March 29 2020, 07:55 AM
I hope this helps . The wildcard operator '*' you are appending to query terms enables prefix search query. The prefix search query '\**' is returning 400 because Azure Search disallows '*' to be used as the first character in a prefix search query regardless whether it is escaped or not. The character '*' is the only character that a prefix search query cannot begin with. Thank you very much for reporting this. The error message isn't clear in this case and we will improve it. By the way, was it your intention to find documents that contain terms that start with '*' in issuing the search query "\**"?
|
MySQL: How to search for spelling variants? ("murrays", "murray's" etc)
Tag : php , By : Nick Pegg
Date : March 29 2020, 07:55 AM
|