# Attachment(PDF/DOC) Indexing and Searching on ElasticSearch in PHP

**URL:** <https://discuss.elastic.co/t/attachment-pdf-doc-indexing-and-searching-on-elasticsearch-in-php/72370>\
**Category:** Elasticsearch\
**Created:** [January 21, 2017, 3:34pm UTC](https://discuss.elastic.co/t/attachment-pdf-doc-indexing-and-searching-on-elasticsearch-in-php/72370 "2017-01-21T15:34:22Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![selimppc](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/selimppc/32/14789_2.png) [@selimppc](https://discuss.elastic.co/u/selimppc)\
**Post date:** [January 21, 2017, 3:34pm UTC](https://discuss.elastic.co/t/attachment-pdf-doc-indexing-and-searching-on-elasticsearch-in-php/72370/1 "2017-01-21T15:34:22Z")

</div>

I have installed elasticsearch in my local pc. Also installed mapper-attachments and kibana.

I can index string data into a index and search. But when I trying to search text from PDF or docx in a folder :: Its not working. Its returning null or blank array().

I searched on google and read the elastic documentation but Still I have nothing to resolve it. Would you guide me ? Here is my code ::

```
$client = \Elasticsearch\ClientBuilder::create()->build();

$doc_src = public_path()."/uploads/files/bower.pdf";
$binary = fread(fopen($doc_src, "r"), filesize($doc_src));
$doc_str = base64_encode($binary);

$data_string = 'Welcome to Dhaka! Anwar is a gentleman!';

//index data and assign by index "my_index"
$params = [
    'index' => 'my_index',
    'type' => 'attachment',
    'id' => 'my_id',
    'body' => [
        'testField' => $data_string,
        'fileName' => $doc_src,
        'file' => $doc_str
    ]

];

$response = $client->index($params);
#print_r($response);

//search data from the index "my_index"
$params = [
    'index' => 'my_index',
    'type' => 'attachment',
    'body' => [
        'query' => [
            'match' => [
                #'testField' => 'dhaka',
                'file' => 'dhaka'
            ]
        ],
    ]
];

$response = $client->search($params);
print_r($response['hits']['hits']);

```

Bottom points :

```
1. when I tried to teach using `testField` from the `data_string` then its working nicely. 
2. When I tried to search from pdf file `doc_str` `=>` Its not working 

```

I think I missed something big. Any helps is highly appreciated!

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [January 21, 2017, 7:20pm UTC](https://discuss.elastic.co/t/attachment-pdf-doc-indexing-and-searching-on-elasticsearch-in-php/72370/2 "2017-01-21T19:20:39Z")

</div>

What is your mapping?

BTW prefer using ingest-attachment instead.

---

<div class="post-metadata">

**Author:** ![selimppc](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/selimppc/32/14789_2.png) [@selimppc](https://discuss.elastic.co/u/selimppc)\
**Post date:** [January 22, 2017, 6:42am UTC](https://discuss.elastic.co/t/attachment-pdf-doc-indexing-and-searching-on-elasticsearch-in-php/72370/3 "2017-01-22T06:42:48Z")

</div>

I did not understand the mapping policy from documentation. Would you please send an example for PHP ?

And Would you please send an example for ingest-attachment with mapping and searching ?

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [January 22, 2017, 7:54am UTC](https://discuss.elastic.co/t/attachment-pdf-doc-indexing-and-searching-on-elasticsearch-in-php/72370/4 "2017-01-22T07:54:33Z")

</div>

If you did not define a mapping then it explains why it does not work.

Read:

[https://www.elastic.co/guide/en/elasticsearch/plugins/current/mapper-attachments-helloworld.html](https://www.elastic.co/guide/en/elasticsearch/plugins/current/mapper-attachments-helloworld.html)

For ingest:

[https://www.elastic.co/guide/en/elasticsearch/plugins/current/using-ingest-attachment.html](https://www.elastic.co/guide/en/elasticsearch/plugins/current/using-ingest-attachment.html)

---

<div class="post-metadata">

**Author:** ![selimppc](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/selimppc/32/14789_2.png) [@selimppc](https://discuss.elastic.co/u/selimppc)\
**Post date:** [January 22, 2017, 8:02am UTC](https://discuss.elastic.co/t/attachment-pdf-doc-indexing-and-searching-on-elasticsearch-in-php/72370/5 "2017-01-22T08:02:43Z")

</div>

Thanks again. Now I tried to use ingest\_processor in PHP

Here is the mapping code

```
public function ingest_processor_mapping()
{
    $client = \Elasticsearch\ClientBuilder::create()->build();
    $params = [
        'id' => 'attachment',
        'body' => [
            'description' => 'Extract attachment information',
            'processors' => [
                [
                    'attachment' => [
                        'field' => 'content',
                        'indexed_chars' => -1
                    ]
                ]
            ]
        ],
        
    ];
    return $client->ingest()->putPipeline($params);
}  

```

**Result** ::

```
{
 "acknowledged": true
}

```

Here is my Indexing Code in PHP

```
public function ingest_processor_indexing()
{
    $client = \Elasticsearch\ClientBuilder::create()->build();
    $fullfile = public_path().'/uploads/files/bower.pdf';
    $params = [
        'index' => 'ingest_index',
        'type' => 'attachment',
        'id' => 'document_id',
        'pipeline' => 'attachment', // <----- here
        'body' => [
            'content' => base64_encode(file_get_contents($fullfile)),
            'file_path' =>$fullfile,
        ]
    ];
    return $client->index($params);
}

```

**Result ::**

```
 {
  "_index": "ingest_index",
  "_type": "attachment",
  "_id": "document_id",
  "_version": 2,
  "result": "updated",
  "_shards": {
"total": 2,
"successful": 1,
"failed": 0
  },
  "created": false
}

```

Here is my searching CODE in PHP ::

```
public function ingest_processor_searching()
{
    $client = \Elasticsearch\ClientBuilder::create()->build();
    $params = [
        'index' => 'ingest_index',
        'type' => 'attachment',
        'body' => [
            'query' => [
                'match' => [
                    'content' => 'dhaka'
                ]
            ],
        ]
    ];

    $response = $client->search($params);
    print_r($response['hits']['hits']);
}

```

**Result ::**

```
Array( )

```

Apologies, I messed up something. Would you please help me so that every one can get the best answer ?

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [January 22, 2017, 10:18am UTC](https://discuss.elastic.co/t/attachment-pdf-doc-indexing-and-searching-on-elasticsearch-in-php/72370/6 "2017-01-22T10:18:05Z")

</div>

Can you do:

```auto
GET ingest_index/attachment/document_id

```

---

<div class="post-metadata">

**Author:** ![selimppc](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/selimppc/32/14789_2.png) [@selimppc](https://discuss.elastic.co/u/selimppc)\
**Post date:** [January 22, 2017, 1:29pm UTC](https://discuss.elastic.co/t/attachment-pdf-doc-indexing-and-searching-on-elasticsearch-in-php/72370/7 "2017-01-22T13:29:19Z")

</div>

Yeah Sure ..

I hit the URL ( GET )

```
http://localhost:9200/ingest_index/attachment/document_id

```

Result ::

```
    {
  "_index": "ingest_index",
  "_type": "attachment",
  "_id": "document_id",
  "_version": 14,
  "found": true,
  "_source": {
    "file_path": "/Users/selimreza/Sites/haber_dev/public/uploads/files/bower.txt",
    "attachment": {
      "content_type": "text/plain; charset=ISO-8859-1",
      "language": "en",
      "content": "Welcome to Dhaka",
      "content_length": 18
    },
    "content": "V2VsY29tZSB0byBEaGFrYQo="
  }
}

```

Now I need to search. How will I search using the `URL (ingest_index/attachment/document_id)` and `query` ?

---

<div class="post-metadata">

**Author:** ![selimppc](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/selimppc/32/14789_2.png) [@selimppc](https://discuss.elastic.co/u/selimppc)\
**Post date:** [January 22, 2017, 4:23pm UTC](https://discuss.elastic.co/t/attachment-pdf-doc-indexing-and-searching-on-elasticsearch-in-php/72370/8 "2017-01-22T16:23:32Z")

</div>

my search method is not working ::

Here is my code in PHP

```
public function ingest_processor_searching($query)
{
    $client = $this->client;
    $params = [
        'index' => 'ingest_index',
        'type' => 'attachment',
        'body' => [
            'query' => [
                'match' => [
                    'textField' => $query,
                ]
            ],
        ],
    ];

    $response = $client->search($params);
    return $response;
}

```

**Result ::**

```
{
"took": 7,
"timed_out": false,
"_shards": {
   "total": 5,
   "successful": 5,
   "failed": 0
 },
  "hits": {
  "total": 0,
  "max_score": null,
  "hits": []
 } 
}

```

But I have Data for the GET `http://localhost:9200/ingest_index/attachment/2`

```
{
  "_index": "ingest_index",
  "_type": "attachment",
  "_id": "2",
  "_version": 1,
  "found": true,
  "_source": {
    "file_path": "/Users/selimreza/Sites/haber_dev/public/uploads/files/bower.txt",
    "attachment": {
      "content_type": "text/plain; charset=ISO-8859-1",
      "language": "en",
      "content": "Welcome to Dhaka",
      "content_length": 18
    },
    "textField": "V2VsY29tZSB0byBEaGFrYQo="
  }
}

```

What is the mistake I did ? Would you please help me out ?

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [January 22, 2017, 4:43pm UTC](https://discuss.elastic.co/t/attachment-pdf-doc-indexing-and-searching-on-elasticsearch-in-php/72370/9 "2017-01-22T16:43:51Z")

</div>

Search in attachment.content field

---

<div class="post-metadata">

**Author:** ![selimppc](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/selimppc/32/14789_2.png) [@selimppc](https://discuss.elastic.co/u/selimppc)\
**Post date:** [January 22, 2017, 4:48pm UTC](https://discuss.elastic.co/t/attachment-pdf-doc-indexing-and-searching-on-elasticsearch-in-php/72370/10 "2017-01-22T16:48:40Z")

</div>

Thank you so much .. It works...

I am going to upload my complete code in php. SO that Any one can test ES within a minute

---

<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:** [February 19, 2017, 4:48pm UTC](https://discuss.elastic.co/t/attachment-pdf-doc-indexing-and-searching-on-elasticsearch-in-php/72370/11 "2017-02-19T16:48:46Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
