6
I am making a mini search system for urls that are stored in a.txt file, one per line, my problem is the performance issue because I have 5000 urls stored (only) and it is already slow in searching, on the server side I have:
if(isset($_GET['search'])) {
$urls = array();
if(trim($_GET['search']) != '') {
$urls = search_in_urls($_GET['search']);
}
echo json_encode($urls);
}
function search_in_urls($search) {
$file = fopen("urls.txt", "r");
$urls = array();
while(!feof($file)){
$url = fgets($file);
if (strpos($url, $search) !== false) {
$urls[] = $url;
}
}
return $urls;
}
Vi this reply accepted in the Soen and would like to implement it, but how to return all lines where a match with my $search
?
The search solution I saw that I would like (unless there are better options) to implement was:
if(exec('grep '.escapeshellarg($search).' ./urls.txt')) {
// do stuff
}
That is, there is some way not to go all the lines looking for a match (set of chars equal to the one I wrote) and return that corresponding line in full? Or that at least for this search to be much faster?
You will have to use some indexing mechanism. Is this only for study purposes or is it for a real system? Because if it’s for a real system I suggest using ready-made tools like Elasticsearch.
– Vinicius Zaramella
It’s just for fun/curiosity, I also know that the performance would be better if I had the
urls
stored in a database, that’s what you’d do in a real project. Obgado @Viniciuszaramella– Miguel
Good luck: http://homepages.dcc.ufmg.br/~Nivio/cursos/ri16/transp/Indexing.pdf
– taiar
There is no shell command that can be used? that returns the line(s) all(s) where there is match? @taiar. That way you could inject the command into the
exec(...
– Miguel
The grep you posted does just that. I can’t figure out the result you’ll get in terms of performance. According to the documentation http://php.net/manual/en/function.exec.php. you must pass a second "output" variable in which the lines will be inserted and it was able to find a match.
– Vinicius Zaramella
@Viniciuszaramella We were with the same doubt, I think it only lacked him to pass a same output. See my answer.
– bfavaretto