I am using the following code:
include('sphinxapi.php');
$search = "John"
$s = new SphinxClient;
$s->SetServer("localhost", 9312);
$s->SetMatchMode(SPH_MATCH_EXTENDED2);
$s->SetSortMode(SPH_SORT_EXTENDED, 'name ASC');
$nameindex = $s->Query("$search");
echo $nameindex['total_found'];
This returns a blank page however without the SetSortMode it works fine and returns the number of results. No matter what I set the SetSortMode to it does not work. Any ideas as to why this would be?
I am indexing one column called name
You can't sort by (normal) fields in Sphinx, only attributes, or fields marked with the sql_field_string setting (which creates an attribute of the same name). So you'll need to either add an attribute with the same column, or use sql_field_string - they're equivalent.
Also: I've removed the thinking-sphinx tag - you're not using Ruby, and thus not the Thinking Sphinx library.
Related
Good time of day!
There is such config Sphinx
source txtcontent : ru_config
{
sql_query = SELECT `id` as `txt_id`, 1 as index_id, `type_id`,`content_type_id`, `title`, `annonce`, `content` FROM `TxtContent` WHERE `status` = 1 AND `content_type_id` != 14
sql_attr_uint = index_id
sql_attr_uint = type_id
}
The entire table is indexed, and is stored in one large search index.
When it comes to find what is in it then all works OK
But today the task was to search for categories
The categories described in the field and have a type_id of type int
How in php using SphinxAPI to perform such a search?
Standard search looks like this.
$sphinxClient = new SphinxClient();
$sphinxClient->SetServer("127.0.0.1", 3312 );
$sphinxClient->SetLimits( 0, 700,700 );
$sphinxClient->SetSortMode(SPH_SORT_RELEVANCE);
$sphinxClient->SetArrayResult( true );
$result = $sphinxClient->Query( $this->query, 'txtcontent provider item');
I tried to add
$sphinxClient->SetFilter('type_id','1');
To search only where type_id = 1 but it didn't help.
Actually how can I search for a specific category? option to find everything in php to let go of the result excess is not considered (otherwise, the search will then be saturada existing limit) how to do it "properly" via the API without placing each topic in a separate search index?
setFilter takes an Array of values. And they need to be numeric (type_id is a numeric attribute)
$sphinxClient->SetFilter('type_id',array(1));
The sphinxapi class actully uses assertions to detect invalid data like this, which I guess you have disabled (otherwise would of seen them!).
I have JSON string stored in my database column. I have to update that value in JSON string.
Here Is my table.
I want to update the state value inside it.
Example:
Name1 has State value KA so I want to update it to GJ.
What I have Tried So far?
UPDATE Customer
SET Detail = JSON_MODIFY(Detail , '$.Address.State', 'KA')
WHERE Name = 'name1';
Also Tried JSON_REPLACE is also not working.
But it shows the error:
FUNCTION Customer.JSON_MODIFY does not exist
Note: I know one workaround to do this but I didn't Want to fetch that string and update it completely. I want to update the particular detail in string.
I have also created the SQL Fiddle.
I am doing this on localhost. Below are the localhost detail.
Database server
Server: localhost (localhost via TCP/IP)
Software: MySQL
MySQL Version :5.5.24
phpMyAdmin
Version information: 3.5.1, latest stable version: 4.7.3
12.16 JSON Functions
...
Unless otherwise indicated, the JSON functions were added in MySQL
5.7.8.
...
Try:
UPDATE `Customer`
SET `Detail` = JSON_REPLACE(`Detail`, '$.Address.State', 'GJ')
WHERE `Name` = 'name1';
See db-fiddle.
As #wchiquito already pointed out, the JSON function were added in MySQL 5.7.8.
If you are not able to upgrade your MySQL installation you will have to take the workaround you mentioned. Using regular expression to replace your value is also not going to work without being able to define custom mysql functions. (Also there is a lot that can go wrong if you do operations like this with regex...)
So the only options I see you have, are:
upgrade your installation (award to #wchiquito).
Fetch the column, parse it and update it. Just as you mentioned yourself as a workaround.
That could look something like this:
// fetch the details
$sth = $pdo->prepare('select `Detail` from `Customer` where `Name` = ?');
$sth->execute(['name1']);
$detail = json_decode($sth->fetchColumn(), true);
// modify the state
$detail['Address']['State'] = 'KA';
// update the details
$sth = $pdo->prepare('update `Customer` set `Detail` = ? where `Name` = ?');
$sth->execute([json_encode($detail), 'name1']);
But I recommend just upgrading your MySQL installation if possible.
As you have an old MySQL, fetch the JSON string, decode it to an array, edit the array, re-encode it, and update your row:
// Fetch row
$json = $row['columnName'];
$array = json_decode($json, true); //true creates array and not stdClass
$array['valueToChange'] = 'some new value';
$json = json_encode($array);
// Perform update query
You can:
Use JSON_INSERT function to add the property to the object if it is not exist
Use JSON_REPLACE function substitutes the property only if it is exist
Use JSON_SET function to add the property if it is not found then replace it.
Hope this helps!
I have the following PHP script and am using the sphinx search API. I want to search for a custom keyword but only in the title column of the MySQL database.
$s = new SphinxClient;
$s->setServer("localhost", 9312);
$s->setMatchMode(SPH_MATCH_EXTENDED);
$s->SetLimits(0, 10000);
$result = $s->Query("#(title) apple");
Unfortunately this returns nothing but when i use the following script:
$s = new SphinxClient;
$s->setServer("localhost", 9312);
$s->setMatchMode(SPH_MATCH_EXTENDED);
$s->SetLimits(0, 10000);
$result = $s->Query("apple");
I obtain the results, the problem is that the script searches in all columns.
What am I doing wrong?
I should also mention that on localhost (using XAMPP) it is working fine like in the first example.
One thing I do notice, you dont explicitly note which index going to search - so the Query() searches ALL indexes.
Persumably then on one server you have an index that doesnt contain #title.
... for maximum compatiblity (so it doesnt matter waht other indexes add to the server, should probably search a specific index...
$s->Query("#(title) apple",'my_index');
I'm currently indexing a database with lucene. I was thinking of storing the table id in the index, but I can't find a way to retrieve the documents by that field. I guess some pseudo-code will further clarify the question:
document.add("_id", 7, Field.Index.UN_TOKENIZED, Field.Store.YES);
// How can I query the document with _id=7
// without getting the document with _id=17 or _id=71?
EDIT for Zend Lucene:
You will need a Keyword type field in order for it to be searched.
For indexing, use something like:
$doc->addField(Zend_Search_Lucene_Field::Keyword('_id', '7'));
For search, use:
$idTerm = new Zend_Search_Lucene_Index_Term('_id', '7');
$idQuery = new Zend_Search_Lucene_Search_Query_Term($idTerm);
Just to say I've just implemented this successfully on my Zend Lucene search engine. However, after some time troubleshooting I discovered that the field name and field value are the opposite way around to the way shown. To correct the example:
// Fine - no change here
$doc->addField(Zend_Search_Lucene_Field::Keyword('_id', '7'));
// Reversed order of parameters
$idTerm = new Zend_Search_Lucene_Index_Term('7', '_id',);
$idQuery = new Zend_Search_Lucene_Search_Query_Term($idTerm);
I hope that helps someone!
I am trying to create an "advanced search", where I can let the user search only specific fields of my index. For that, I'm using a boolean query:
$sq1 = Zend_Search_Lucene_Search_QueryParser::parse($field1); // <- provided by user
$sq2 = Zend_Search_Lucene_Search_QueryParser::parse($field2); // <- provided by user
$query = new Zend_Search_Lucene_Search_Query_Boolean();
$query->addSubquery($sq1, true);
$query->addSubquery($sq2, true);
$index->find($query);
How can I specify specify that sq1 will search field 'foo', and sq2 will search field 'bar'?
I feel like I should be parsing the queries differently for the effect (because the user might type in a field name), but the docs only mention the QueryParser for joining user-input queries with API queries.
It seems the simplest way to do this is just to fudge the user input:
$sq1 = Zend_Search_Lucene_Search_QueryParser::parse("foo:($field1)");
$sq2 = Zend_Search_Lucene_Search_QueryParser::parse("bar:($field2)");
$field1 and $field2 should be stripped of parenthesis and colons beforehand to avoid "search injection".
What you want is the query construction API: http://www.zendframework.com/manual/en/zend.search.lucene.query-api.html#zend.search.lucene.queries.multiterm-query
However, I'd recommend that you drop Zend_Search_Lucene altogether. The Java implementation is wonderful, but the PHP implementation is very bad. Regarding what you are trying to do it behaves very buggy, see question 1508748. It's also very, very slow.