# Problem in fetching text from an attachment

**URL:** <https://discuss.elastic.co/t/problem-in-fetching-text-from-an-attachment/32910>\
**Category:** Elasticsearch\
**Created:** [October 24, 2015, 1:11pm UTC](https://discuss.elastic.co/t/problem-in-fetching-text-from-an-attachment/32910 "2015-10-24T13:11:30Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tarneet\_Singh](https://avatars.discourse-cdn.com/v4/letter/t/f0a364/32.png) [@Tarneet\_Singh](https://discuss.elastic.co/u/Tarneet_Singh)\
**Post date:** [October 24, 2015, 1:11pm UTC](https://discuss.elastic.co/t/problem-in-fetching-text-from-an-attachment/32910/1 "2015-10-24T13:11:30Z")

</div>

Hi,

I am using elasticsearch-1.7.3 and mapper attachment plugin-2.7.1  
I have successfully add an attachment in elasticsearch with the help of php code, but while searching a text from attachment, i did not get the desired results. i get a complete attachment.  
php code for adding attachment is:  
$binary = fread(fopen($target\_file,"r"), filesize($target\_file));  
$base = base64\_encode($binary);

$article = array();  
$article['index'] = 'test';  
$article['type'] = 'person';  
$article['body'] = array('my\_attachment' =\> $base,'location2' =\> $location,'skills2' =\> $skills);  
$result = $es-\>index($article);  
where target\_file is the location of file stored on my server folders, locations and skills are other fields in elasticsearch

when i search for a word in this file, i use the following php code:

```
 $params =array();

```

$params['index'] = 'test';  
$params['type'] = 'person';  
$params['body']['query']['match']['my\_attachment'] = $q;

$params['body']['highlight']['fields']['my\_attachment'] = array("term\_vector" =\> "with\_positions\_offsets","store" =\> true);

$query = $es-\>search($params);  
where $q is just a text(which is there in file)

my file is a simple ms word file.  
everything worked but when i echo the result it gives me complete document printed, instead of my searched text.  
please if anybody can solve it or may give me a link where i can find help regarding the php code of mapper attachment(search text in a file), it would be great please reply asap

---

<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:** [October 24, 2015, 2:15pm UTC](https://discuss.elastic.co/t/problem-in-fetching-text-from-an-attachment/32910/2 "2015-10-24T14:15:53Z")

</div>

I don't exactly understand the php code here.  
Did you define a mapping?

What does it look like?

BTW, read the doc here: [https://github.com/elastic/elasticsearch-mapper-attachments/blob/master/README.md#highlighting-attachments](https://github.com/elastic/elasticsearch-mapper-attachments/blob/master/README.md#highlighting-attachments)

---

<div class="post-metadata">

**Author:** ![Tarneet\_Singh](https://avatars.discourse-cdn.com/v4/letter/t/f0a364/32.png) [@Tarneet\_Singh](https://discuss.elastic.co/u/Tarneet_Singh)\
**Post date:** [October 24, 2015, 2:21pm UTC](https://discuss.elastic.co/t/problem-in-fetching-text-from-an-attachment/32910/3 "2015-10-24T14:21:04Z")

</div>

yes mapping is defined  
could you please tell me how to print our searched texted i think search is going well but i don't know how to print my result on screen  
here is the mapping  
{  
"test": {  
"mappings": {  
"person": {  
"properties": {  
"location2": {  
"type": "string"  
},  
"my\_attachment": {  
"type": "attachment",  
"path": "full",  
"fields": {  
"my\_attachment": {  
"type": "string",  
"store": true,  
"term\_vector": "with\_positions\_offsets"  
},  
"author": {  
"type": "string"  
},  
"title": {  
"type": "string"  
},  
"name": {  
"type": "string"  
},  
"date": {  
"type": "date",  
"format": "dateOptionalTime"  
},  
"keywords": {  
"type": "string"  
},  
"content\_type": {  
"type": "string"  
},  
"content\_length": {  
"type": "integer"  
},  
"language": {  
"type": "string"  
}  
}  
},  
"skills2": {  
"type": "string"  
}  
}

---

<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:** [October 24, 2015, 2:45pm UTC](https://discuss.elastic.co/t/problem-in-fetching-text-from-an-attachment/32910/4 "2015-10-24T14:45:41Z")

</div>

Again: What does it look like?

---

<div class="post-metadata">

**Author:** ![Tarneet\_Singh](https://avatars.discourse-cdn.com/v4/letter/t/f0a364/32.png) [@Tarneet\_Singh](https://discuss.elastic.co/u/Tarneet_Singh)\
**Post date:** [October 24, 2015, 2:47pm UTC](https://discuss.elastic.co/t/problem-in-fetching-text-from-an-attachment/32910/5 "2015-10-24T14:47:00Z")

</div>

this is the mapping  
{  
"test": {  
"mappings": {  
"person": {  
"properties": {  
"location2": {  
"type": "string"  
},  
"my\_attachment": {  
"type": "attachment",  
"path": "full",  
"fields": {  
"my\_attachment": {  
"type": "string",  
"store": true,  
"term\_vector": "with\_positions\_offsets"  
},  
"author": {  
"type": "string"  
},  
"title": {  
"type": "string"  
},  
"name": {  
"type": "string"  
},  
"date": {  
"type": "date",  
"format": "dateOptionalTime"  
},  
"keywords": {  
"type": "string"  
},  
"content\_type": {  
"type": "string"  
},  
"content\_length": {  
"type": "integer"  
},  
"language": {  
"type": "string"  
}  
}  
},  
"skills2": {  
"type": "string"  
}  
}

---

<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:** [October 24, 2015, 3:10pm UTC](https://discuss.elastic.co/t/problem-in-fetching-text-from-an-attachment/32910/6 "2015-10-24T15:10:47Z")

</div>

What does this produce in JSON?

$params['body']['highlight']['fields']['my\_attachment'] = array("term\_vector" =\> "with\_positions\_offsets","store" =\> true);

Can you print the full JSON query so it will be easier to understand?  
Did you try to run an highlighting query from curl or SENSE ?

---

<div class="post-metadata">

**Author:** ![Tarneet\_Singh](https://avatars.discourse-cdn.com/v4/letter/t/f0a364/32.png) [@Tarneet\_Singh](https://discuss.elastic.co/u/Tarneet_Singh)\
**Post date:** [October 25, 2015, 5:14am UTC](https://discuss.elastic.co/t/problem-in-fetching-text-from-an-attachment/32910/7 "2015-10-25T05:14:08Z")

</div>

$params['body']['highlight']['fields']['my\_attachment'] =  
array("term\_vector" =\> "with\_positions\_offsets","store" =\> true);  
this is basically used for searching purpose when i delete it and try to search again it still gives me same result.  
when i add file(attachment), i got a huge string of chracters like this  
"source" : "my\_attachment":"fdvmevkjvmvvmvvvf................................................."

i did not try to run highlighting query. i am using postman  
it does not matter whether i use $params['body']['highlight']['fields']['my\_attachment'] =  
array("term\_vector" =\> "with\_positions\_offsets","store" =\> true); or not  
when i do this print\_r($query);

i got this

Array  
(  
[took] =\> 56  
[timed\_out] =\>  
[\_shards] =\> Array  
(  
[total] =\> 5  
[successful] =\> 5  
[failed] =\> 0  
)

```
[hits] => Array
    (
        [total] => 2
        [max_score] => 0.027122213
        [hits] => Array
            (
                [0] => Array
                    (
                        [_index] => test
                        [_type] => person
                        [_id] => AVCdYEvW0tzmItCH2MC9
                        [_score] => 0.027122213
                        [_source] => Array
                            (
                                [my_attachment] => dsfgbdjherijfgvejkvefjbedvbejve vjin......................................

```

[location2] =\> sdfdsgdfgdfg  
[skills2] =\> dfdsgdsgvdsgv  
)

```
                    )

            )

    )

```

)

can you tell me how to use highlight query in php code because i think search does work but i do not know how to print it on screen  
should i do echo $some\_variable['\_source']['my\_attachment']; or what??

---

<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:** [October 25, 2015, 6:58am UTC](https://discuss.elastic.co/t/problem-in-fetching-text-from-an-attachment/32910/8 "2015-10-25T06:58:24Z")

</div>

> [@Tarneet\_Singh](#):
>
> $params['body']['highlight']['fields']['my\_attachment'] = array("term\_vector" =\> "with\_positions\_offsets","store" =\> true);

IMO this is incorrect. Look at the doc. When we query we don't add term\_vector...

---

<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:** [October 25, 2015, 6:59am UTC](https://discuss.elastic.co/t/problem-in-fetching-text-from-an-attachment/32910/9 "2015-10-25T06:59:21Z")

</div>

Also with POSTMAN, use POST instead of GET.

---

<div class="post-metadata">

**Author:** ![Tarneet\_Singh](https://avatars.discourse-cdn.com/v4/letter/t/f0a364/32.png) [@Tarneet\_Singh](https://discuss.elastic.co/u/Tarneet_Singh)\
**Post date:** [October 25, 2015, 7:06am UTC](https://discuss.elastic.co/t/problem-in-fetching-text-from-an-attachment/32910/10 "2015-10-25T07:06:30Z")

</div>

ok so what query should i use in place of that??  
what i am saying is there is a word in my file "address" now when i search something else no result is shown(print\_r($query) is empty)  
which is excellent because that word is not there but when i search for address, it is searching from file that is correct but i do not know how to get only the desired word from file.please help me with that.

---

<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:** [October 25, 2015, 7:46am UTC](https://discuss.elastic.co/t/problem-in-fetching-text-from-an-attachment/32910/11 "2015-10-25T07:46:59Z")

</div>

Anything unclear in doc?

GET /test/person/\_search  
{  
"fields": [],  
"query": {  
"match": {  
"file": "king queen"  
}  
},  
"highlight": {  
"fields": {  
"file": {  
}  
}  
}  
}

---

<div class="post-metadata">

**Author:** ![Tarneet\_Singh](https://avatars.discourse-cdn.com/v4/letter/t/f0a364/32.png) [@Tarneet\_Singh](https://discuss.elastic.co/u/Tarneet_Singh)\
**Post date:** [October 25, 2015, 7:54am UTC](https://discuss.elastic.co/t/problem-in-fetching-text-from-an-attachment/32910/12 "2015-10-25T07:54:09Z")

</div>

ok in this king queen is searched and we get this as result  
"highlight": {  
"file": [  
""God Save the _Queen_" (alternatively "God Save the _King_"\n"  
]  
}  
what i am getting here is my full document instead of only desired word.  
$q="address";  
$params['body']['query']['match']['my\_attachment'] = $q;  
when i print\_r($params); this  
i get Array  
(  
[index] =\> test  
[type] =\> person  
[body] =\> Array  
(  
[query] =\> Array  
(  
[match] =\> Array  
(  
[my\_attachment] =\> address  
)

```
            )

    )

```

)  
which is great so the next step is search i.e.  
$query = $es-\>search($params);  
so it should give me the desired word from that file but i got full document  
"highlight": {  
"my\_attachment": [  
"...... my full document ......."  
]  
}

---

<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:** [October 25, 2015, 8:35am UTC](https://discuss.elastic.co/t/problem-in-fetching-text-from-an-attachment/32910/13 "2015-10-25T08:35:22Z")

</div>

Why do you want to get back a word instead of the document itself?

---

<div class="post-metadata">

**Author:** ![Tarneet\_Singh](https://avatars.discourse-cdn.com/v4/letter/t/f0a364/32.png) [@Tarneet\_Singh](https://discuss.elastic.co/u/Tarneet_Singh)\
**Post date:** [October 25, 2015, 8:41am UTC](https://discuss.elastic.co/t/problem-in-fetching-text-from-an-attachment/32910/14 "2015-10-25T08:41:32Z")

</div>

basically i am working on a recruitment framework and users upload their resumes.  
so when i search skills(php,java) i want to search from those uploaded files and from that i get number of users and their skills and their addresses etc.

---

<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:** [October 25, 2015, 11:34am UTC](https://discuss.elastic.co/t/problem-in-fetching-text-from-an-attachment/32910/15 "2015-10-25T11:34:00Z")

</div>

So the result you want to get is a resume, not really a single "word", right?

Then, you don't really need highlight here.

You have two options IMO:

- get the result back from elasticsearch (use a simple search) and extract from the \_source field the BASE64 content, decode it on the client and open the file in your browser.
- ask for field `file` in the above example:

```auto
PUT /test/person/_mapping
{
  "person": {
    "properties": {
      "file": {
        "type": "attachment",
        "fields": {
          "file": {
            "type": "string",
            "term_vector":"with_positions_offsets",
            "store": true
          }
        }
      }
    }
  }
}

```

Something like:

```auto
GET /test/person/_search
{
  "fields": ["file"],
  "query": {
    "match": {
      "file": "king queen"
    }
  }
}

```

If it does not work, try with field `file.file`.

---

<div class="post-metadata">

**Author:** ![Tarneet\_Singh](https://avatars.discourse-cdn.com/v4/letter/t/f0a364/32.png) [@Tarneet\_Singh](https://discuss.elastic.co/u/Tarneet_Singh)\
**Post date:** [October 25, 2015, 2:42pm UTC](https://discuss.elastic.co/t/problem-in-fetching-text-from-an-attachment/32910/16 "2015-10-25T14:42:32Z")

</div>

basically what i want is for example there are 1000 resumes in elasticsearch and i want candidates with experience in java and i have many fields like i have skills field,experience fileld etc.so i will put java in the skills field search and say i put experience =1year in experience field search and based on that i should get all information about candidates experience and all information about skills.

---

<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:** [October 25, 2015, 3:31pm UTC](https://discuss.elastic.co/t/problem-in-fetching-text-from-an-attachment/32910/17 "2015-10-25T15:31:09Z")

</div>

When you say "fields" you mean fields inside JSON document (structured data) or inside the PDF (unstructured)?

Try to add explain: true in the query. That could help may be...

---

<div class="post-metadata">

**Author:** ![Tarneet\_Singh](https://avatars.discourse-cdn.com/v4/letter/t/f0a364/32.png) [@Tarneet\_Singh](https://discuss.elastic.co/u/Tarneet_Singh)\
**Post date:** [October 26, 2015, 1:28pm UTC](https://discuss.elastic.co/t/problem-in-fetching-text-from-an-attachment/32910/18 "2015-10-26T13:28:51Z")

</div>

fields means fields in form

---

<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:** [October 26, 2015, 1:41pm UTC](https://discuss.elastic.co/t/problem-in-fetching-text-from-an-attachment/32910/19 "2015-10-26T13:41:21Z")

</div>

So I don't understand. If you have structured data and unstructured data, what is the problem then?

May be it will be easier to understand if you send a full example of your JSON document?

---

<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 5, 2017, 11:42pm UTC](https://discuss.elastic.co/t/problem-in-fetching-text-from-an-attachment/32910/20 "2017-07-05T23:42:40Z")

</div>


