# Index and search pdf file with elasticsearch php client

**URL:** <https://discuss.elastic.co/t/index-and-search-pdf-file-with-elasticsearch-php-client/16867>\
**Category:** Elasticsearch\
**Created:** [April 8, 2014, 8:22am UTC](https://discuss.elastic.co/t/index-and-search-pdf-file-with-elasticsearch-php-client/16867 "2014-04-08T08:22:21Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tanguy\_Bernard](https://avatars.discourse-cdn.com/v4/letter/t/839c29/32.png) [@Tanguy\_Bernard](https://discuss.elastic.co/u/Tanguy_Bernard)\
**Post date:** [April 8, 2014, 8:22am UTC](https://discuss.elastic.co/t/index-and-search-pdf-file-with-elasticsearch-php-client/16867/1 "2014-04-08T08:22:21Z")

</div>

Hello,  
Recently, I find a very helpfull information here :

> <https://gist.github.com/lukas-vlcek/1075067>

I would like to reproduce the same indexing and searching with php  
ElasticSearch client.  
My indexing seems to work!

\<?php require\_once 'vendor/autoload.php'; $client = new Elasticsearch\Client(); $doc\_src = "fn6742.pdf"; $binary = fread(fopen($doc\_src, "r"), filesize($doc\_src)); $doc\_str = base64\_encode($binary); $article = array(); $article['index'] = 'index2'; $article['type'] = 'attachment'; $article['body'] = array('file' =\> $doc\_str); $result = $client-\>index($article); ?\>

But my "search" does not work. I would like to find the sentence where my  
world is.  
I tried this :

$params2['body']['query']['match']['file'] = 'my word';  
$results = $client-\>search($params2);  
print\_r($results);

And I would like something like this : "file" : [ " It's_my word_  
/You can't use _my word_Â /Â becauseÂ " ]

I hope you can help me?

Thanks in advance

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/9eb30ab1-119d-4cb6-a502-5402986f5cfa%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/9eb30ab1-119d-4cb6-a502-5402986f5cfa%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Tanguy\_Bernard](https://avatars.discourse-cdn.com/v4/letter/t/839c29/32.png) [@Tanguy\_Bernard](https://discuss.elastic.co/u/Tanguy_Bernard)\
**Post date:** [April 8, 2014, 10:20am UTC](https://discuss.elastic.co/t/index-and-search-pdf-file-with-elasticsearch-php-client/16867/2 "2014-04-08T10:20:45Z")

</div>

I find the answer :

$params2 =array();

$params2['body']['query']['text']['file'] = 'my words';  
$params2['body']['highlight']['fields']['file'] = array("term\_vector" =\>  
"with\_positions\_offsets");  
$results = $client-\>search($params2);  
print\_r($results);

Le mardi 8 avril 2014 10:22:21 UTC+2, Tanguy Bernard a écrit :

> Hello,  
> Recently, I find a very helpfull information here :  
> [Test of attachments plugin · GitHub](https://gist.github.com/lukas-vlcek/1075067)
> 
> I would like to reproduce the same indexing and searching with php  
> Elasticsearch client.  
> My indexing seems to work!
> 
> \<?php require\_once 'vendor/autoload.php'; $client = new Elasticsearch\Client(); $doc\_src = "fn6742.pdf"; $binary = fread(fopen($doc\_src, "r"), filesize($doc\_src)); $doc\_str = base64\_encode($binary); $article = array(); $article['index'] = 'index2'; $article['type'] = 'attachment'; $article['body'] = array('file' =\> $doc\_str); $result = $client-\>index($article); ?\>
> 
> But my "search" does not work. I would like to find the sentence where my  
> world is.  
> I tried this :
> 
> $params2['body']['query']['match']['file'] = 'my word';  
> $results = $client-\>search($params2);  
> print\_r($results);
> 
> And I would like something like this : "file" : [ " It's_my word_  
> /You can't use _my word_Â /Â becauseÂ " ]
> 
> I hope you can help me?
> 
> Thanks in advance

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/3969e1e5-6a19-461d-87d3-3f5c8fa021fc%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/3969e1e5-6a19-461d-87d3-3f5c8fa021fc%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Rajesh\_Jai](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rajesh_jai/32/14160_2.png) [@Rajesh\_Jai](https://discuss.elastic.co/u/Rajesh_Jai)\
**Post date:** [June 23, 2014, 2:08pm UTC](https://discuss.elastic.co/t/index-and-search-pdf-file-with-elasticsearch-php-client/16867/3 "2014-06-23T14:08:17Z")

</div>

\<?php require\_once 'vendor/autoload.php'; $client = new Elasticsearch\Client(); $doc\_src = "path/file.pdf"; $binary = fread(fopen($doc\_src, "r"), filesize($doc\_src)); $doc\_str = base64\_encode($binary); $article = array(); $article['index'] = 'index2'; $article['type'] = 'attachment'; $article['body'] = array('file' =\> $doc\_str); $result = $client-\>index($article); ?\>

I used this code to index attached pdf files, indexing without error. But  
in indexed file, the file field is empty. Please see this image

[https://lh3.googleusercontent.com/-MDX53ZTDKMs/U6g0TZxx17I/AAAAAAAAANI/-XRdJeQO9rU/s1600/Screenshot%2Bfrom%2B2014-06-23%2B19%253A33%253A34.png](https://lh3.googleusercontent.com/-MDX53ZTDKMs/U6g0TZxx17I/AAAAAAAAANI/-XRdJeQO9rU/s1600/Screenshot%2Bfrom%2B2014-06-23%2B19%253A33%253A34.png)

\*will searching using this code r\*\*aising error : \*  
$params2 =array();

$params2['body']['query']['text']['file'] = 'my words';  
$params2['body']['highlight']['fields']['file'] = array("term\_vector" =\>  
"with\_positions\_offsets");  
$results = $client-\>search($params2);  
print\_r($results);

Raising error like :

PHP Fatal error: Uncaught exception  
'Guzzle\Http\Exception\ClientErrorResponseException' with message 'Client  
error response  
[status code] 400  
[reason phrase] Bad Request  
[url] [http://localhost:9200/\_search](http://localhost:9200/_search)' in  
/var/www/html/magento/vendor/guzzle/guzzle/src/Guzzle/Http/Exception/BadResponseException.php:43  
Stack trace:  
#0  
/var/www/html/magento/vendor/guzzle/guzzle/src/Guzzle/Http/Message/Request.php(145):  
Guzzle\Http\Exception\BadResponseException::factory(Object(Guzzle\Http\Message\EntityEnclosingRequest),  
Object(Guzzle\Http\Message\Response))  
#1 [internal function]:  
Guzzle\Http\Message\Request::onRequestError(Object(Guzzle\Common\Event),  
'request.error', Object(Symfony\Component\EventDispatcher\EventDispatcher))  
#2  
/var/www/html/magento/vendor/symfony/event-dispatcher/Symfony/Component/EventDispatcher/EventDispatcher.php(164):  
call\_user\_func(Array, Object(Guzzle\Common\Event), 'request.error',  
Object(Symfony\Component\EventDispatcher\EventDispatcher))  
#3 /var/www/html/magento/vendor/symfony/event-dispatcher/Symfony/Componen  
in  
/var/www/html/magento/vendor/elasticsearch/elasticsearch/src/Elasticsearch/Connections/GuzzleConnection.php  
on line 266

I hope u guys will help to me to fix this. Thanks in advance.

On Tuesday, 8 April 2014 13:52:21 UTC+5:30, Tanguy Bernard wrote:

> Hello,  
> Recently, I find a very helpfull information here :  
> [Test of attachments plugin · GitHub](https://gist.github.com/lukas-vlcek/1075067)
> 
> I would like to reproduce the same indexing and searching with php  
> Elasticsearch client.  
> My indexing seems to work!
> 
> \<?php require\_once 'vendor/autoload.php'; $client = new Elasticsearch\Client(); $doc\_src = "fn6742.pdf"; $binary = fread(fopen($doc\_src, "r"), filesize($doc\_src)); $doc\_str = base64\_encode($binary); $article = array(); $article['index'] = 'index2'; $article['type'] = 'attachment'; $article['body'] = array('file' =\> $doc\_str); $result = $client-\>index($article); ?\>
> 
> But my "search" does not work. I would like to find the sentence where my  
> world is.  
> I tried this :
> 
> $params2['body']['query']['match']['file'] = 'my word';  
> $results = $client-\>search($params2);  
> print\_r($results);
> 
> And I would like something like this : "file" : [ " It's_my word_  
> /You can't use _my word_Â /Â becauseÂ " ]
> 
> I hope you can help me?
> 
> Thanks in advance

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/561b7818-5deb-4f94-96d2-aa43389d8ffc%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/561b7818-5deb-4f94-96d2-aa43389d8ffc%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Rajesh\_Jai](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rajesh_jai/32/14160_2.png) [@Rajesh\_Jai](https://discuss.elastic.co/u/Rajesh_Jai)\
**Post date:** [June 23, 2014, 3:09pm UTC](https://discuss.elastic.co/t/index-and-search-pdf-file-with-elasticsearch-php-client/16867/4 "2014-06-23T15:09:23Z")

</div>

$params2 =array();

$params2['body']['query']['text']['file'] = 'my words';  
$params2['body']['highlight']['fields']['file'] = array("term\_vector" =\>  
"with\_positions\_offsets");  
$results = $client-\>search($params2);  
print\_r($results);

"search" does not work...  
_Result raising error like below._ please help. Give correct solution.  
Thanks in advance.  
PHP Fatal error: Uncaught exception  
'Guzzle\Http\Exception\ClientErrorResponseException' with message 'Client  
error response  
[status code] 400  
[reason phrase] Bad Request  
[url] [http://localhost:9200/\_search](http://localhost:9200/_search)' in  
/var/www/html/magento/vendor/guzzle/guzzle/src/Guzzle/Http/Exception/BadResponseException.php:43  
Stack trace:  
#0  
/var/www/html/magento/vendor/guzzle/guzzle/src/Guzzle/Http/Message/Request.php(145):  
Guzzle\Http\Exception\BadResponseException::factory(Object(Guzzle\Http\Message\EntityEnclosingRequest),  
Object(Guzzle\Http\Message\Response))  
#1 [internal function]:  
Guzzle\Http\Message\Request::onRequestError(Object(Guzzle\Common\Event),  
'request.error', Object(Symfony\Component\EventDispatcher\EventDispatcher))  
#2  
/var/www/html/magento/vendor/symfony/event-dispatcher/Symfony/Component/EventDispatcher/EventDispatcher.php(164):  
call\_user\_func(Array, Object(Guzzle\Common\Event), 'request.error',  
Object(Symfony\Component\EventDispatcher\EventDispatcher))  
#3 /var/www/html/magento/vendor/symfony/event-dispatcher/Symfony/Componen  
in  
/var/www/html/magento/vendor/elasticsearch/elasticsearch/src/Elasticsearch/Connections/GuzzleConnection.php  
on line 266

On Tuesday, 8 April 2014 15:50:45 UTC+5:30, Tanguy Bernard wrote:

> I find the answer :
> 
> $params2 =array();
> 
> $params2['body']['query']['text']['file'] = 'my words';  
> $params2['body']['highlight']['fields']['file'] = array("term\_vector" =\>  
> "with\_positions\_offsets");  
> $results = $client-\>search($params2);  
> print\_r($results);
> 
> Le mardi 8 avril 2014 10:22:21 UTC+2, Tanguy Bernard a écrit :
> 
> > Hello,  
> > Recently, I find a very helpfull information here :  
> > [Test of attachments plugin · GitHub](https://gist.github.com/lukas-vlcek/1075067)
> > 
> > I would like to reproduce the same indexing and searching with php  
> > Elasticsearch client.  
> > My indexing seems to work!
> > 
> > \<?php require\_once 'vendor/autoload.php'; $client = new Elasticsearch\Client(); $doc\_src = "fn6742.pdf"; $binary = fread(fopen($doc\_src, "r"), filesize($doc\_src)); $doc\_str = base64\_encode($binary); $article = array(); $article['index'] = 'index2'; $article['type'] = 'attachment'; $article['body'] = array('file' =\> $doc\_str); $result = $client-\>index($article); ?\>
> > 
> > But my "search" does not work. I would like to find the sentence where my  
> > world is.  
> > I tried this :
> > 
> > $params2['body']['query']['match']['file'] = 'my word';  
> > $results = $client-\>search($params2);  
> > print\_r($results);
> > 
> > And I would like something like this : "file" : [ " It's_my word_  
> > /You can't use _my word_Â /Â becauseÂ " ]
> > 
> > I hope you can help me?
> > 
> > Thanks in advance

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/78b4cad8-6b23-4dea-a1cc-15c8ca7cdaa0%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/78b4cad8-6b23-4dea-a1cc-15c8ca7cdaa0%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Rajesh\_Jai](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rajesh_jai/32/14160_2.png) [@Rajesh\_Jai](https://discuss.elastic.co/u/Rajesh_Jai)\
**Post date:** [June 24, 2014, 7:45am UTC](https://discuss.elastic.co/t/index-and-search-pdf-file-with-elasticsearch-php-client/16867/5 "2014-06-24T07:45:08Z")

</div>

If search is not working. Change this line  
_$params2['body']['query']['text']['file'_] _= 'my words';_ as \*$params2['body']['query']['match']['file']  
= 'my words'; \*

$params2 =array();

$params2['body']['query']['match']['file'] = 'my words';  
$params2['body']['highlight']['fields']['file'] = array("term\_vector" =\>  
"with\_positions\_offsets");  
$results = $client-\>search($params2);  
print\_r($results);

On Tuesday, 8 April 2014 15:50:45 UTC+5:30, Tanguy Bernard wrote:

> I find the answer :
> 
> $params2 =array();
> 
> $params2['body']['query']['text']['file'] = 'my words';  
> $params2['body']['highlight']['fields']['file'] = array("term\_vector" =\>  
> "with\_positions\_offsets");  
> $results = $client-\>search($params2);  
> print\_r($results);
> 
> Le mardi 8 avril 2014 10:22:21 UTC+2, Tanguy Bernard a écrit :
> 
> > Hello,  
> > Recently, I find a very helpfull information here :  
> > [Test of attachments plugin · GitHub](https://gist.github.com/lukas-vlcek/1075067)
> > 
> > I would like to reproduce the same indexing and searching with php  
> > Elasticsearch client.  
> > My indexing seems to work!
> > 
> > \<?php require\_once 'vendor/autoload.php'; $client = new Elasticsearch\Client(); $doc\_src = "fn6742.pdf"; $binary = fread(fopen($doc\_src, "r"), filesize($doc\_src)); $doc\_str = base64\_encode($binary); $article = array(); $article['index'] = 'index2'; $article['type'] = 'attachment'; $article['body'] = array('file' =\> $doc\_str); $result = $client-\>index($article); ?\>
> > 
> > But my "search" does not work. I would like to find the sentence where my  
> > world is.  
> > I tried this :
> > 
> > $params2['body']['query']['match']['file'] = 'my word';  
> > $results = $client-\>search($params2);  
> > print\_r($results);
> > 
> > And I would like something like this : "file" : [ " It's_my word_  
> > /You can't use _my word_Â /Â becauseÂ " ]
> > 
> > I hope you can help me?
> > 
> > Thanks in advance

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/c8f2a7cf-8474-430b-a72f-701916909af2%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/c8f2a7cf-8474-430b-a72f-701916909af2%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 6, 2017, 1:20am UTC](https://discuss.elastic.co/t/index-and-search-pdf-file-with-elasticsearch-php-client/16867/6 "2017-07-06T01:20:17Z")

</div>


