Sphinx extended match mode - php

I'm using a sphinx bundle (timewasted SphinxSearchBundle) in a symfony 2 app (https://github.com/timewasted/Search-SphinxsearchBundle) (based on the PHP API)
it works great as long as I don't try to use the SPH_MATCH_EXTENDED.
Here's a code sample
$sphinxSearch = $this->get('search.sphinxsearch.search');
$sphinxSearch->setMatchMode(SPH_MATCH_EXTENDED);
$searchResults = $sphinxSearch->search("#typemesure_id 2", $index);
But the result is always empty, and it seems that my field (#typemesure_id) is considered as a word :
'words' =>
array
'typemesure_id' =>
array
'docs' => string '0' (length=1)
'hits' => string '0' (length=1)
2 =>
array
'docs' => string '4169' (length=4)
'hits' => string '5714' (length=4)
Does anyone konws whats wrong in my request ?
Can anyone post an exemple of working extended query working with this bundle?
My goal is to get a condition like "#(user1,user2,user3) 15"
Thanks for your help
Notes:
the SPH_MATCH_ALL & SPH_MATCH_ANY are working fine.
The setFilter() method is also working

Looking at the function defition...
public function search($query, array $indexes, array $options = array(), $escapeQuery = true)
It will automaticlly escape the query, so the # will be getting escaped. Need to pass false as fourth param

Related

how to access fields in mongo document

I have a php codeigniter web app that has a mongo db backend.
i'm stuck for now using the mongoclient library for php.
I often have to run commands like this:
$result = $collection->find(
array("didnum" => $didnum)
);
$result = iterator_to_array($result);
Assuming that $result looks like this:
array (size=1)
'5824b9376b6347a422aae017' =>
array (size=10)
'_id' =>
object(MongoId)[22]
public '$id' => string '5824b9376b6347a422aae017' (length=24)
'users' =>
array (size=1)
0 =>
array (size=2)
...
'rules' =>
array (size=1)
0 =>
array (size=5)
...
'id' => string '5824b9376b6347a422aae017' (length=24)
'last_assigned' => string 'missing' (length=7)
'widgetnum' => string '+18455100023' (length=12)
'location' => string 'missing' (length=7)
What is the easiest way to access the location field?
In other words, in cases where I know there will only be one result, I'm still finding that i have to loop through $result because the array is an associative one, and i won't know what the ID is.
Just wondering if there's an easier way to do this?
Thanks.
I'm not very much into PHP, but findOne method seems to exist for PHP binding as well as other languages' bindings
You can use $collection->findOne instead of $collection->find

FindBy('myvar' => $array) not working with mongoDbOdm in Symfony3?

I have filled a mongodb-collection with the following values (identifer = myvar):
array (size=10)
0 => string 'B00LHILHS8' (length=10)
1 => string 'B00WSCCMV8' (length=10)
2 => string 'B00MA15OK8' (length=10)
However - findBy(['myvar' => 'B00LHILHS8']) returns the document as wanted. But findBy(['myvar' => ['B00LHILHS8', 'B00WSCCMV8']]) returns an empty result.
I am not sure, if this is right. As the Doctrine Manual - Working with objects - By Simple Conditions mentions, that it should be possible to pass an array for the findBy() method.
Should i use the query-builder instead?
use doctrine-mongodb-odm with Conditional Operators [link]
try:
$qb = $dm->createQueryBuilder('Entity')
->field('myvar')->in(array('B00LHILHS8', 'B00WSCCMV8'));

Get a field value from a taxonomy_term the "right way"

I am trying to get the value of various fields from taxonomy terms (and I'd like to do it the right way, i.e. not $term->field_foo['und'][0]['value'])
I'm able to do this reliably for nodes and adapted my method for taxonomy terms, but it doesn't seem to be working. Here is my code:
$field = field_get_items('taxonomy_term', $term, 'field_foo');
$value = field_view_value('taxonomy_term', $term, 'field_foo', $field[0]);
$rendered = render($value);
In troubleshooting this, I can see that field_get_items correctly returns an array... if I insert a var_dump (of $field) after the first line there, i get this:
array (size=1)
0 =>
array (size=1)
'value' => string '1' (length=1)
Yet field_view_value returns an empty string... again a var_dump (of $value) after the second line results in this:
array (size=2)
'#markup' => string '' (length=0)
'#access' => boolean true
Can anyone see where I'm going wrong?
You can use the Entity Metadata Wrapper:
$term = entity_metadata_wrapper('taxonomy_term', TERM_ID);
Simpler and reliable.

how to parse the json array elements using for loop to get the values one by one

i have a json array wherer i would like to pasre the json till the last element by using for loop for that i would like to get the number of array elements in the json array ,i have more than 3 objects in anarray ,so i am confused how to parse the json till the last element,
i can say you the idea
Count($json);
echo count;
for(i=0;i<l=count($json);i++)
{
then print the value of each key
}
i am stuck ,because there is no fixed lenght for the json i am getting as it is a server response it may return one object one may be twice or thrice or many ,so i thought it would be better to do with for loop ,as a json contain more than one json with 3 keys ,such as country can have more than one state,and one state can have more than one district ,plaese help me,i am stuck with question for last 2 days
thank you
An idea :
function printJson($json) {
foreach($json as $index=>$value) {
if(is_array($value)) {
printJson($value);
} else {
echo 'Value :'.$value.'<br />';
}
}
}
$stringJson = "{'location':[{...}]}"; //for example
printJson(json_decode($stringJson));
You can alternatively decode the json tring using json_decode() which will give u a php variable which u can then easily iterate over using php.
eg.
$string = '{"image":"fox.png","puzzlepieces":{"f":{"puzzlepiece":"one","position":"top:121px;left:389px;"},"x":{"puzzlepiece":"three","position":"top:164px;left:455px;"},"o":{"puzzlepiece":"two","position":"top:52px;left:435px;"}}}';
var_dump(json_decode($string));
will output as
object(stdClass)[1]
public 'image' => string 'fox.png' (length=7)
public 'puzzlepieces' =>
object(stdClass)[2]
public 'f' =>
object(stdClass)[3]
public 'puzzlepiece' => string 'one' (length=3)
public 'position' => string 'top:121px;left:389px;' (length=21)
public 'x' =>
object(stdClass)[4]
public 'puzzlepiece' => string 'three' (length=5)
public 'position' => string 'top:164px;left:455px;' (length=21)
public 'o' =>
object(stdClass)[5]
public 'puzzlepiece' => string 'two' (length=3)
public 'position' => string 'top:52px;left:435px;' (length=20)
My xdebug extension is on in WAMP so your var_dump might be a little differently formatted but overall you'd get a php variable from the array which u can iterate using foreach or other loops.
Read more on json_decode here

access associative array

I am using Code Igniter and I get following data structure after executing a query at DB
array
'application' =>
array
0 =>
object(stdClass)[19]
public 'app_id' => string '16' (length=2)
public 'app_name' => string 'dddddddd' (length=8)
public 'app_title' => string 'sdfsdf' (length=6)
public 'app_comments' => string 'sdfsdf' (length=6)
public 'active_flg' => string 'N' (length=1)
I know one way to access the values is
foreach($application as $key => $value)
$value->app_id
But I know that I will get only one record each time so I want to access the elements without using foreach.
I have tried to $application->app_id and $application['app_id'] but I keep getting error.
Can anybody please help me to understand how to access the data directly??
You are using multidimensional mixed type of array, with numeric indexing on the second level. SO, while accessing the values, you have to use them too. Like
echo $array['application'][0]->app_id;
A simple example to show you the structure of your array and how you might access it...
$objArray = array('app_id' => 7, 'app_name' => 'apps demo', 'app_title' => 'apps demo title');
$applicationArray = array('application' => array((object)$objArray));
// access the array
print $applicationArray['application'][0]->app_id;
Are you getting that result by doing the following?
$res = $this->db->query('select * from application limit 1')->result();
If so, you can put that result into an object by doing:
$app = $this->db->query('select * from application limit 1')->row();
This way you can access the properties as follows:
echo $app->app_id;
You should check out codeigniters manual on getting results.

Categories