PHP Zend Lucene Search Query with multiple criteria fail - php

Zend Lucene Search Document
Lucene Document
pk:Keyword
category_id:Keyword
title:UnStored
description:UnStored
This is my string query "java lucene AND +category_id:7".
Result here:
Array
(
[0] => Array
(
[pk] => 209
[category_id] => 7
[id] => 0
[score] => 0.40750848701418
)
[1] => Array
(
[pk] => 225
[category_id] => 7
[id] => 3
[score] => 0.30750848701619
)
[2] => Array
(
[pk] => 211
[category_id] => 8 ====>>> WRONG!!!
[id] => 2
[score] => 0.37152213415004
)
)
Can you do a Query search on the category_id = 7 only??
Thanks in advance.

I had solved this problem by using Zend Query Parsing
$strQuery = Zend_Search_Lucene_Search_QueryParser::parse('java lucene');
$cateTerm = new Zend_Search_Lucene_Index_Term(7 , 'category_id');
$cateQuery = new Zend_Search_Lucene_Search_Query_Term($cateTerm);
$query = new Zend_Search_Lucene_Search_Query_Boolean();
$query->addSubquery($strQuery, true /* required */);
$query->addSubquery($cateQuery, true /* required */);
Results will be only in category_id = 7 :)

You can remove the AND +category_id:7 from you query, what you want is a filter since +category_id:7 is not needed as a ranked value.
I don't know how to implement it with Zend_Lucene but in solr I used to pass fq parameter, this may give you a hint :)
Filtering is a process that constrains the search space and allows only a subset of documents to be considered for search hits. You can use this feature to implement search-within-search results. Lucene comes with various built-in filters such as BooleanFilter, CachingWrapperFilter, ChainedFilter, DuplicateFilter, PrefixFilter, QueryWrapperFilter, RangeFilter, RemoteCachingWrapperFilter, SpanFilter, etc.(THE NATIVE JAVA VERSION) Filter can be passed to IndexSearcher's search method to filter documents that match the filter criteria.

Related

usort of session array not outputting as expected

I have a grid of img squares that can be dragged into any order using the sortable library. Each img is a visual representation of a result from a mySQL db query that selects any image that shares an 'imageparent' identifier. The order they're presented in the grid is taken from the 'imageorder' column in the database and starts at 0 and works in sequence up to the nth number of images returned.
The purpose of dragging the img grid is to be able to change the 'imageorder' index. On completion of the drag, the sortable library POSTS an 'imageorder' var by ajax to service.php and is received correctly. So rather than the original 0,1,2,3,4,5,6,7 order of the original, it sends a string like 2,1,0,3,4,5,7,6. Not too hard to grasp. After I switch the order the orderList var sent to service.php is always correct, but the array I end up sending to the db and setting as my session var becomes a little garbled in order after the second or third drag and I'm not quite sure why.
Code Examples and Comments
$_SESSION['selectedCsImages'] Array structure:
[0] => Array
(
[imagename] => "Title"
[imageorder] => 0
[imageid] => 43
)
[1] => Array
(
[imagename] => "Title"
[imageorder] => 1
[imageid] => 21
)
[2] => Array
(
[imagename] => "Title"
[imageorder] => 2
[imageid] => 3
)
etc...
Services.php extract:
if (session_status() == PHP_SESSION_NONE) {
session_start();
}
// Turn the orderList posted into an array
$removeChars = array('"','[',']');
$orderList = str_replace($removeChars, "", $_POST['order']); // POST received fine.
$listArray = explode(",",$orderList);
// Retrieve the session array
$sorting = $_SESSION['selectedCsImages'];
/* My logic is that I compare the $sorting array to $listArray and reorder $sorting by 'imageorder' to match $listarray */
usort($sorting, function($a, $b) use ($listArray) {
return array_search($a['imageorder'], $listArray) - array_search($b['imageorder'], $listArray);
});
/* I now have a $sorting array that (sometimes, hence the problem) matches the order that the images had just been dragged into by the user. Typically, as I mentioned above, it's correct after the first drag, but not always after the second or third where it creates a new order that I can't see a pattern or logic in. */
/* Had there not been errors with the usort function, I (would) have a $sorting array in the order I want but with imageorder values referring to pre-sorting. I iterate through the array and set each key to 0, 1, 2, etc. so that I have an array in the correct order and with each imageorder correctly stating its place.*/
$i = 0;
foreach ($sorting as $key => $value) {
$sorting[$key]['imageorder'] = $i;
$i++;
}
/* The information is attempted to be sent to the db and, on success I update the session var */
// Database code (runs succesfully and updates the db as per the image orders found in the $sorting array)
$_SESSION['selectedCsImages'] = $sorting;
Debugging:
From debugging, it appears that something happens with the usort function when I call this page from ajax for the second or third time. Everything after this follows through fine and processes the correct or incorrect order as per expectations. The orderList var posted by sortable is correct each time. I'd provide a sample of the $sorted var after usort each time but it's as simple to describe it as the above array example in an order I didn't specify after dragging and I can't see a pattern in the seemingly random order it outputs.
From researching, I had thought that it was an issue with session vars being retained until the page is refreshed but it appears that the ajax call to services.php should refresh the $_SESSION['selectedCsImages'] var. I had also read that, perhaps, I was unknowingly using referenced array values and - as I source from a session var to a new array and, ultimately, save back to this session var from this array - I may have created some messy referencing feedback. However, I tried using $sorted = (array)clone(object)$_SESSION['selectedCsImages']; before attempting usort and the results didn't change.
PHP error logs are showing nothing.
Updates:
Per the suggestion of #Ayaou, I've checked the output of $listArray and am getting some unexpected results. I'd wrongly assumed that as the posted $orderList was correct, that the exploded array would not be a culprit.
Here's the output of print_r($listArray) after completing the following order swaps of 16 img elements: 1st with 2nd, 2nd last with last,6th with 7th:
1st and 2nd:
(
[0] => 1
[1] => 0
[2] => 2
[3] => 3
[4] => 4
[5] => 5
[6] => 6
[7] => 7
[8] => 8
[9] => 9
[10] => 10
[11] => 11
[12] => 12
[13] => 13
[14] => 14
[15] => 15
)
last and 2nd last:
(
[0] => 1
[1] => 0
[2] => 2
[3] => 3
[4] => 4
[5] => 5
[6] => 6
[7] => 7
[8] => 8
[9] => 9
[10] => 10
[11] => 11
[12] => 12
[13] => 13
[14] => 15
[15] => 14
)
6th with 7th:
(
[0] => 1
[1] => 0
[2] => 2
[3] => 3
[4] => 4
[5] => 6
[6] => 5
[7] => 7
[8] => 8
[9] => 9
[10] => 10
[11] => 11
[12] => 12
[13] => 13
[14] => 15
[15] => 14
)
I was progressing with the idea that $listArray would show a sequential 0,1,2,3,etc. each time with only the two swapped items showing order changes. As it's not, I'll look back again at $orderList and check if my sortable library is updating the orders it's obtaining correctly from the updated session var. Older order swaps are being retained somewhere along the chain where they shouldn't.
The solution is on your sortable form (on the front end), so instead of sending the imageorder on your 'order' post data, send the imageid index.
Then change your sort callback like this
//Use imageid index instead of imageorder
usort($sorting, function($a, $b) use ($listArray) {
return array_search($a['imageid'], $listArray) - array_search($b['imageid'], $listArray);
});

php mysql check if vendor has 3 low consecutive ratings

on my ratings table for my software i have 4 fields.
id autoincrement
rvid vendor id
ratedate date of rating
rating the actual numeric rating
I have done alot with it over the last few months but this time im stumped and i cant get a clear picture in my head of the best way to do this. What i am trying to do is find out if the vendor has had 3 low 'consecutive' ratings. If their last three ratings have been < 3 then i want to flag them.
I have been playing with this for a few hours now so i thought i would ask (not for the answer) but for some path direction just to push me forward, im stuck in thought going in circles here.
I have tried GROUP BY and several ORDER BY but those attempts did not go well and so i am wondering if this is not a mysql answer but a php answer. In other words maybe i just need to take what i have so far and just move to the php side of things via usort and the like and do it that way.
Here is what i have so far i did select id as well at first thinking that was the best way to get the last consective but then i had a small breakthrough that if they have had 3 in a row the id does not matter, so i took it out of the query.
$sql = "SELECT `rvid`, `rating` FROM `vendor_ratings_archive` WHERE `rating` <= '3' ORDER BY `rvid` DESC";
which give me this
Array
(
[0] => Array
(
[rvid] => 7
[rating] => 2
)
[1] => Array
(
[rvid] => 5
[rating] => 1
)
[2] => Array
(
[rvid] => 5
[rating] => 0
)
[3] => Array
(
[rvid] => 5
[rating] => 3
)
)
this is just just samples i tossed in the fields, and there are only 4 rows here where as in live it will be tons of rows. But basically this tells me that these are the vendors that have low ratings in the table. And that is where i get stumpted. I can only do one sort in the query so that is why i am thinking that i need to take this and move to the php side to finish it off.
I think i need to sort the elements by rvid with php first i think, and then see if three elements in a row are the same vender (rvid).
Hope that makes sense. My brain hurts lol...
update - here is all of the table data using *
Array
(
[0] => Array
(
[id] => 7
[rvid] => 7
[ratedate] => 2016-05-01
[rating] => 2
)
[1] => Array
(
[id] => 8
[rvid] => 5
[ratedate] => 2016-05-01
[rating] => 1
)
[2] => Array
(
[id] => 6
[rvid] => 5
[ratedate] => 2016-05-01
[rating] => 0
)
[3] => Array
(
[id] => 5
[rvid] => 5
[ratedate] => 2016-05-01
[rating] => 3
)
)
Here's one way you can begin approaching this - completely in SQL:
Get the last rating for the vendor. ORDER BY date DESC, limit 1.
Get the second to last rating for the vendor. ORDER BY date DESC, limit 1, OFFSET 1.
Then write a query that does a LEFT join of the first two tables. You will have a dataset that has three columns:
vendor id
last rating
second to last rating
Then you can write an expression that says "if column1 is <3 and column2 < 3, then this new column is true"
You should be able to extend this to three columns relatively easily.
Here is what a came up with to solve this riddle. I think explaining it on here helped as well as Alex also helped as he keyed my brain on using the date. I first started looking at using if statment inside of the query and actually that got my brain out of the box and then it hit me what to do.
It is not perfect and certainly could use some trimming to reduce the code, but i understand it and it seems to work, so that is par for me on this course.
the query...
$sql = "SELECT `rvid`, `ratedate`,`rating` FROM `vendor_ratings_archive` WHERE `rating` <= '3' ORDER BY `ratedate`, `rvid` DESC";
which gives me this
Array
(
[0] => Array
(
[rvid] => 7
[ratedate] => 2016-05-01
[rating] => 2
)
[1] => Array
(
[rvid] => 5
[ratedate] => 2016-05-01
[rating] => 1
)
[2] => Array
(
[rvid] => 5
[ratedate] => 2016-05-01
[rating] => 0
)
[3] => Array
(
[rvid] => 5
[ratedate] => 2016-05-01
[rating] => 3
)
)
notice how vendor (rvid) 5 is grouped together which is an added plus.
next a simple foreach to load a new array
foreach($results as $yield)
{
$rvidarray[] = $yield['rvid'];
}//close foreach
which gives me this
Array
(
[0] => 7
[1] => 5
[2] => 5
[3] => 5
)
then we count the array values to group dups
$rvidcounter = array_count_values($rvidarray);
which results in this
Array(
[7] => 1
[5] => 3
)
so now vender 7 as 1 low score and vendor 5 has 3 low scores and since they were already sorted by date i know that its consecutive. Well it sounds good anyway lol ")
then we create our final array with another foreach
foreach($rvidcounter as $key => $value)
{
//anything 3 or over is your watchlist
if($value > 2)
{
$watchlist[] = $key; //rvid number stored
}
}//close foreach
which gives me this
Array
(
[0] => 5
)
this was all done in a service function. So the final deal is everyone in this array has over 3 consecutive low ratings and then i just use a returned array back in my normal php process file and grab the name of each vender by id and pass that to the html and print out the list.
done...
please feel free to improve on this if you like. I may or may not use it because the above code makes sense to me. Something more complicated may not make sense to me 6 mos from now lol But it would be interesting to see what someone comes up with to shorten the process a bit.
Thanks so much and Happy Coding !!!!!
Dave :)
You could do it in SQL like that:
SET #rvid = -1;
SELECT DISTINCT rvid FROM
(
SELECT
rvid,
#neg := rating<3, /* 0 or 1 for addition in next line */
#count := IF(#rvid <> rvid , #neg, #count+#neg) AS `count`, /* set or add */
#rvid := rvid /* remember last row */
FROM
testdb.venrate
ORDER BY
rvid, datetime desc
) subq
WHERE count>=3
;
You set a variable to a non existing id. In each chronologically sorted row you check if rating is too low, that results in 1 (too low) or 0 (ok). If rvid is not equal to the last rvid, it means a new vender section is beginning. On begin of section set the value 0 or 1, else add this value. Finally store the current row's rvid for comparison in next row process.
The code above is looking for 3 consecutive low ratings (low means a value less than 3) over all the time.
A small modification checks if all the latest 3 ratings has been equal to or less than 3:
SET #rvid = -1;
SELECT DISTINCT
rvid
FROM
(
SELECT
rvid,
#high_found := rating>3 OR (#rvid = rvid AND #high_found) unflag,
#count := IF(#rvid <> rvid , 1, #count+1) AS `count`,
#rvid := rvid /* remember last row */
FROM
testdb.venrate
ORDER BY
rvid, datetime desc
) subq
WHERE count=3 AND NOT unflag
;

Searching through multiple fields in SphinxSearch (PHP)

I have the following code:
$this->api = new App_Other_SphinxSearch();
$this->api->SetServer($host, $port);
$this->api->SetConnectTimeout(1);
$this->api->SetArrayResult(true);
$results = $this->api->Query("#(title,content) test", 'members');
echo "<pre>";print_r($results);die;
According to their documentation, a syntax like #(field_1,field_2) query should return docs which match the string query in either field_1 or field_2.
The PHP SDK returns something entirely different:
Array
(
[error] =>
[warning] =>
[status] => 0
[fields] => Array
(
[0] => title
[1] => content
)
[attrs] => Array
(
[created] => 2
[content] => 7
)
[total] => 0
[total_found] => 0
[time] => 0.000
[words] => Array
(
[title] => Array
(
[docs] => 10
[hits] => 34
)
[content] => Array
(
[docs] => 34
[hits] => 139
)
[test] => Array
(
[docs] => 26
[hits] => 34
)
)
)
There is no matches key in the array, however it got some hits. I don't really understand why this is happening, especially because if I try the same query from the command line, everything works correctly.
Any help?
Edit: Querying like this works though: #* test. Not really what I want though, cause that searches in all fields.
[total_found] => 0 says there where no matches.
The words array, just tells you now many documents and how many times that word appears in ANY (and all) fields. (WITHOUT regard to your specific query)
Sphinx caches those word stats, which help it make quick sanity checks on queries. When the query runs it can end up not matching any documents (because only then does it apply the field level filters), even though the individual words are found.
That explains your misinpretation of the results, but not why getting the results you get.
You are entering a Extended Mode query (as evidenced by the link to the sphinx documentation) , but the sphinx API defaults to ALL query mode. Notice also that title and content are in the words array, so are being taken as plain keywords not as syntax.
So you need to include a
$this->api->SetMatchMode(SPH_MATCH_EXTENDED);
btw, #* test, works because the #* are simply ignored in ALL query mode.

codeigniter join multiple rows where

I'm trying to code a search function for a site written on CodeIgniter but have trouble with the join statement in the model.
A controller passes an array of search criteria to the model function: get_sales($search_criteria);
function get_sales($search_criterie){
$this->db->join('sales_prices', 'sales_prices.sale_id = sales.sale_id', 'left');
$this->db->join('sales_sizes', 'sales_sizes.sale_id = sales.sale_id', 'left');
$query = $this->db->get_where('sales', $search_criterie);
$sale_data = $query->result_array();
}
I'm running into two problems:
1) I need a WHERE clause in the sales_sizes join. Right now it joins sales_sizes.sale_id ON sales.sale_id but I want to be able to filter by size via the $search_criteria array. How can I do this?
2) Every sales usually has multiple sizes and I want the output of the query formatted like:
Array
(
[sale_id] => 1
[sale_category] => shoes
[sale_price] => 29.99
[sale_sizes] => Array
(
[0] => 10
[1] => 10.5
[2] => 11
)
)
I've tried some foreach loops to format the output but can't get it to work. Hope somebody can help.
Update:
How do I process the following query result into a format like the one above?
Array
(
[0] => Array
(
[sale_id] => 1
[sale_category] => shoes
[sale_price] => 29.99
[sale_size] => 10
)
[1] => Array
(
[sale_id] => 1
[sale_category] => shoes
[sale_price] => 29.99
[sale_size] => 10.5
)
[2] => Array
(
[sale_id] => 1
[sale_category] => shoes
[sale_price] => 29.99
[sale_size] => 11
)
)
I am personally not a big fan of using the active record as given. I find that it often restricts the programmer from writing more complex queries. If you find that this is true as well you could rewrite your query as followed:
$this->db->query('SELECT * FROM `sales_prices`, `sales_sizes` WHERE `sales_prices`.`sale_id` = `sales`.`sale_id` AND `sales_sizes`.`sale_id` = `sales`.`sale_id` AND `sales` = ?', array($search_criterie));
$sale_data = $query->results();
This will generate the same result and will also parameterize your query. Note that the question mark corresponds in order to values in your array that will make up the second parameter of you $this->db->query() function.
Try this type of query
$this->db->select('places.*, category.*')
->from('places')
->join('category', 'places.category_id = category.category_id', 'left')
->join('places_reviews', 'places_reviews.place_id = places.id', 'left')
->where('places.category_id', $category_id)
->limit($limit, $offset)
->order_by($sort_by, $sort_order);
The below solution will work for you, it won't generate exact output, but very close to one.
$this->db->select('sales.*, GROUP_CONCAT(DISTINCT sales_prices.sale_id) as sales_price GROUP_CONCAT(DISTINCT sales_sizes.sale_id) as sales_size')
This will generate a comma separated value for you.

Retrieving and formatting all hasMany records in column of a MySQL result

I'm not sure if this is even possible straight from a MySQL query (without manipulating the data after), but I have been pondering on something...
Say we have one table, authors, and another books. Authors can have many books, and this is indicated by a author_id column in the books table.
Is there a way to perform a query that:
1) Retrieves a single author record, with a column name 'books' which holds an array of book records, belonging to the author.
2) The same result, but for all authors in the database.
Any input on whether this is possible or not, and any methods would be greatly appreciated!
Update
Here's an example of the desired output of the first query:
stdClass Object
(
[id] => 1
[name] => Test Author
[age] => 28
[books] => Array
(
[0] => stdClass Object
(
[title] => Book 1
[id] => 1
)
[1] => stdClass Object
(
[title] => Book 2
[id] => 2
)
)
)
1) Yes, GROUP_CONCAT() can do that (although I don't this would be the best way to d o it).
2) Yes, GROUP BY author

Categories