Zend_Search_Lucene - weird behaviour for Wildcard search and "numeric" string - string

Weird issue I get every time I search "11.11" or "22.22" etc... No problem if I search for "aa.aa" but when I put only integers into my string, I get the following exception:
Wildcard search is supported only for non-multiple word terms
My implementation of Zend search is as below (ZF 1.11):
Zend_Search_Lucene_Search_Query_Wildcard::setMinPrefixLength(0);
Zend_Search_Lucene_Search_QueryParser::setDefaultEncoding('utf-8');
Zend_Search_Lucene_Analysis_Analyzer::setDefault(
new Zend_Search_Lucene_Analysis_Analyzer_Common_Utf8Num_CaseInsensitive()
);
$index = Zend_Search_Lucene::open(APPLICATION_PATH.'/../var/search');
if(str_word_count($searchQuery) > 1){
$searchQuery = Zend_Search_Lucene_Search_QueryParser::escape($searchQuery);
$searchQueryArray = explode(' ', $searchQuery);
$query = new Zend_Search_Lucene_Search_Query_Phrase($searchQueryArray);
}else{
$searchQuery = Zend_Search_Lucene_Search_QueryParser::escape($searchQuery);
$query = Zend_Search_Lucene_Search_QueryParser::parse(
'title:*'.$searchQuery.'* OR
description:*'.$searchQuery.'* OR
content:*'.$searchQuery.'*'
);
}
$result = $index->find($query);
I can't really find any related issue on internet so please, let me know if you've ever been in front of the similar issue. Thank you.

Related

Different approach for pagination - LIMIT when using SQLSRV

I'm trying to make an instant pagination using SQLSRV and PHP, I have successfully did this using MySQL but unable to do so when using SQL Server as it does not support LIMIT.
I have the following codes working in MySQL and I wanted to apply the same thing in sqlsrv but since this is not possible, I'm looking forward in creating a different approach(code) to achieve this, can someone give me an idea or a walkthrough to make this happen please, thanks in advanced.
if(isset($_POST['page'])):
$paged=$_POST['page'];
$sql="SELECT * FROM `member` ORDER BY `member`.`member_id` ASC";
if($paged>0){
$page_limit=$resultsPerPage*($paged-1);
$pagination_sql=" LIMIT $page_limit, $resultsPerPage";
}
else{
$pagination_sql=" FETCH 0 , $resultsPerPage";
}
$result=sqlsrv_query($sql.$pagination_sql);
Try the following code, I hope you find it helpful
$paged = filter_input(INPUT_POST, 'page', FILTER_SANITIZE_NUMBER_INT);
//Initialize these values
$Table = 'your_tbl_name'; //Table name
$IndexColumn = 'pk_col_name'; //Primary key column
$resultsPerPage = '10'; //Page size
$Where = ''; //Optional WHERE clause, may leave empty
$Order = ''; //Optional ORDER clause, may leave empty
$top = ($paged>0) ? $resultsPerPage * ($paged-1) : 0 ;
$limit = 'TOP ' . $resultsPerPage ;
$pagination_sql = "SELECT $limit *
FROM $Table
$Where ".(($Where=="")?" WHERE ":" AND ")." $IndexColumn NOT IN
(
SELECT $IndexColumn FROM
(
SELECT TOP $top *
FROM $Table
$Where
$Order
)
as [virtTable]
)
$Order";
$result=sqlsrv_query($conn, $pagination_sql);

How to search using Prestashop web service

I'm trying to use Prestashop's web service to build an app for my store. For searching products, it's known that we can use
http://store_url/api/search?query=keywords&language=1
But if I search for anything, it won't give me more than 10 results. I've tried using &limit and &display. But nothing works.
If there's an alternative, please let me know.
I think you can try this way to search products:
http://store_url/api/products/?display=full&limit=2&filter[name]=[print]%
You can change display and limit.
It's from Prestashop web services doc.
Hope this helps anyone.
After much wall smashing, I've found a work around. So, the problem is in this file located at /classes/webservice,
WebserviceSpecificManagementSearch.php
Around line 87, you'll find
$results = Search::find($this->wsObject->urlFragments['language'], $this->wsObject->urlFragments['query'], 1,1, 'position', 'desc', true, false);
This the last but second argument tells the Search function that it's an ajax search. MAKING IT FALSE WILL NOT WORK.
Instead, you need to go to /classes/Search.php and find the function "find". Starting at around line 278, you'll find this:
if ($ajax)
{
$sql = 'SELECT DISTINCT p.id_product, pl.name pname, cl.name cname,
cl.link_rewrite crewrite, pl.link_rewrite prewrite '.$score.'
FROM '._DB_PREFIX_.'product p
INNER JOIN `'._DB_PREFIX_.'product_lang` pl ON (
p.`id_product` = pl.`id_product`
AND pl.`id_lang` = '.(int)$id_lang.Shop::addSqlRestrictionOnLang('pl').'
)
'.Shop::addSqlAssociation('product', 'p').'
INNER JOIN `'._DB_PREFIX_.'category_lang` cl ON (
product_shop.`id_category_default` = cl.`id_category`
AND cl.`id_lang` = '.(int)$id_lang.Shop::addSqlRestrictionOnLang('cl').'
)
WHERE p.`id_product` '.$product_pool.'
ORDER BY position DESC LIMIT 10');
return $db->executeS($sql);
}
As you can see, the results are limited by 10. So you need a way to tell the function that it's being called by the WebService. So what I did was this. It's pretty simple. Instead of
ORDER BY position DESC LIMIT 10'
use:
ORDER BY position DESC LIMIT '.(($isWS)? '10000': '10')
The variable $isWS is type Boolean. So you'll have to change the find function's declaration to this:
public static function find($id_lang, $expr, $page_number = 1, $page_size = 1, $order_by = 'position',
$order_way = 'desc', $ajax = false, $use_cookie = true, Context $context = null, $isWS = false)
Once you've done this, you can now pass in the value for $isWS from WebserviceSpecificManagementSearch. So change it to this at line 87:
$results = Search::find($this->wsObject->urlFragments['language'], $this->wsObject->urlFragments['query'], 1,1, 'position', 'asc', true, false, null, true);
Hope this helps anyone with the same problem.

Search with Wildcard PHP

I have a search form to search for usernames. And I have maked SQL Wildcard LIKE but it only print one username?? I have also tried to while it, but it doesn't?
$besked = " ";
if(isset($_POST['submit'])){
if(!empty($_POST['username'])){
$username = $_POST['username'];
$getHeads = mysqli_query($mysqli, "SELECT * FROM user WHERE username LIKE '%".$username."%'") or die(mysql_error());
while($headInfo = mysqli_fetch_array($getHeads))
$besked = "<a href='member.php?id=".$headInfo['id']."'><font color='blue'><h3>".$headInfo['username']."</h3></font></a>";
} else
$besked = "No user found!";
}
Without more context, it's hard to find what's causing the problem. However, there are a lot of errors in the code you provided, so try fixing those first.
Problems
Why are you wrapping everything in two if statements? Just use && to check both variables in the same if.
You're mixing mysqli_* and mysql_* functions. Don't do that, mysqli and mysql are two different database abstraction layers, and should not be used concurrently in your application.
It's possible that I'm misinterpreting your script so I will leave this part untouched in the edited code below, but your if statement is checking that both variables are set, and if they are not, you're telling the user "No user found!". However, what this should probably say is, "Please enter a search term," because you're not actually searching for any users.
Try to write more organized code. You're missing some curly braces, and it's probably causing the script to brake.
Not a backend problem, but I just feel the need to remind you not to use the <font> tag. Use CSS instead.
$besked = " ";
if(isset($_POST['submit']) && !empty($_POST['username'])){
$username = $_POST['username'];
$getHeads = mysqli_query($mysqli, "SELECT * FROM user WHERE username LIKE '%".$username."%'") or die(mysqli_error());
while($headInfo = mysqli_fetch_array($getHeads)) {
$besked = "<a href='member.php?id=".$headInfo['id']."'><font color='blue'><h3>".$headInfo['username']."</h3></font></a>";
}
} else {
$besked = "No user found!";
}
// Echo out the result
echo $besked;

how to: findByProperty on two fields in typo3 v4.5.30?

I want to search for records which match on two fields with two supplied objects,
for example
$cnt = $this->usedCouponRepository->findByUser($validvip)->toArray() ;
$cnt2 = $this->usedCouponRepository->findByCoupon($validcoupon)->toArray() ;
get interesection, (find how many times validvip pairs up with validcoupon in the usedcoupon table)
a call like this would be it -
$cnt3 = $this->usedCouponRepository->findByUserAndCoupon($validcoupon,$validuser);
is there a magic function to do this or some other efficient way? I'd hate to just loop over the first looking for a match in code.
Thanks
Nope, there is no "extbase magic" doing this job for you. You have to implement it yourself. But lucky you, its not that hard. Just add a method to your repository like this one:
public function findByUserAndCoupon($validcoupon, $validuser) {
$query = $this->createQuery();
$query->matching(
$query->logicalAnd(
$query->equals('user', $validuser),
$query->equals('coupon', validcoupon)
)
);
$result = $query->execute();
return $result;
}
Now you can call this like you tried before. The strings 'user' and 'coupon' in the logicalAnd Statement have to be the fieldnames of the database.

Searching phpbb's 'topic_title' via MYSQL php, but exact match doesn't work

$sql = sprintf( "SELECT topic_title
FROM `phpbb_topics`
WHERE `topic_title` LIKE '%%%s%%' LIMIT 20"
, mysql_real_escape_string('match this title')
);
Which I run this query in phpMyAdmin the results are: (correct)
match this title
match this title 002
But when I run that same MYSQL query in PHP I get: (incorrect)
match this title 002
I have also tried MATCH AGAINST with the same result with both php and phpMyAdmin:
$sql = "SELECT topic_title
FROM phpbb_topics
WHERE MATCH (topic_title)
AGAINST('match this title' IN BOOLEAN MODE)";
The whole block of code im using to search with:
mysql_connect("localhost", "user", "pass") or die(mysql_error());
mysql_select_db("phpbb") or die(mysql_error());
$query = "match this title";
$query = "SELECT topic_title
FROM phpbb_topics
WHERE MATCH (topic_title)
AGAINST('$query' IN BOOLEAN MODE)";
// Doesn't work (these 2 both give the same result "match this title 002" and no the "match this title")
// $query = "SELECT * FROM `phpbb_topics`
// WHERE `topic_title`
// LIKE '%$query%'
// LIMIT 0, 30 "; // Doesn't work
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_array($result) or die(mysql_error());
while($row = mysql_fetch_array($result)){
$topic_title = $row['topic_title'];
echo "$topic_title";
}
Any idea as to what i'm doing wrong?
I'v been searching all over the place and have found next to no help :(
The problem is that after you execute your query you fetch the first row, do nothing with it, enter the loop by fetching the second row and start printing results..
If you remove the first $row = mysql_fetch_array($result), (directly after $result = mysql_query($query) or die(mysql_error());) you should be fine.
Another comment; If you echo a variable you don't have to put any qoutes around it. And in the way you're doing it now, you won't get a newline between the results so you might want to change that line to echo $topic_title . "<br>";

Resources