<p>I have now found a workaround using the information @brad-bell provided, that is working, but not exactly what I wanted:</p>

<pre><code>{# Normal search #}
{% set resultsA = craft.entries(search_criteria).ids() %}

{# Assets search #}
{% set assets = craft.assets.customFieldName('*'~query~'*') %}
{% set resultsB = craft.entries.relatedTo({
        targetElement: assets,
        field: 'customFieldName'
    }).ids() | without(resultsA) %}

{# Merge results, paginate #}
{% set results = resultsA | merge(resultsB) %}
{% paginate results.limit(10) as pageInfo, pageEntries %}
</code></pre>

<p>How I did it:</p>

<ol>
<li>Perform a <strong>normal</strong> search for entries containing the <em>query</em> in one of their fields (specified in search_criteria, not included in the code above) and <strong>get the IDs</strong> of the found entries.</li>
<li>Search for assets that contain the <em>query</em> in their <em>customField</em>. Since I'm only searching one field, I do not use the search-function, which would be an option when searching multiple fields.</li>
<li>Now I'm getting the entries that are related to the assets. As in step 1, I attain the IDs, ignoring IDs already present in the first set of results, to avoid duplicates <em>(which I found out late are not a problem, since <code>craft.entries</code> - in point 5 - only returns each entry once)</em></li>
<li>I merge both arrays.</li>
<li>Get an <em>ElementCriteriaObject</em> containing all entries with the IDs collected before.</li>
<li>I paginate them as usual.</li>
</ol>

<p>If you do not need to paginate, step 5-6 are might be unnecessary. You could simple use <code>find()</code> instead of <code>ids()</code> to get arrays which you then could merge. This however will cause duplicates!</p>

<h2>Although this code works, it has a few disadvantages:</h2>

<ul>
<li>The results are <strong>ordered by their position in both arrays</strong>. <em>resultsA</em> are listed first, followed by <em>resultsB</em>. In my application I can live with that, since queries fetching many results in assets will usually fetch almost no results in entries and vice versa. I tried to <strong>sort</strong> and <strong>order</strong> them by the number of occurrences, however, this did not work using only twig functions. With the help of a simple plugin as host for some php array sorting, ordering of the results could be improved.</li>
<li><strong>Performance</strong> is not very good, since many queries are necessary. </li>
</ul>

<h2>Idea</h2>

<p>One idea in my mind was to build a custom MySQL-query, which JOINS the assets fields (combined to a string if there are multiple ones) to the entry's row. However, I was not able to accomplish that. If anyone knows how to do so: Let me know!</p>

<p>Thanks for your help!</p>

CC BY-SA 3.0
2016-08-24T22:04:28.697Z
2016-08-24T22:04:28.697Z
Luke
<p>I have a large list of entries. Each entry has a a bunch of fields and a file-field with one or two files. Those files have related field (Author, Date, ..).</p>

<p>I have now implemented a search. All things are working so far. However, the search does not include the asset's fields. Only the asset's title is recognized. </p>

<p>Can I somehow search in asset fields? I tried something like</p>

<pre><code>field1: *query* AND files.author: *query*
</code></pre>

<p>but this does not work :(</p>

<p>I have read that I can search all assets that are related to that section, but I would like to include this query in one query than in two queries..</p>

CC BY-SA 3.0
2016-07-23T02:54:51.927Z
Just to be clear, your entry has an Assets field on it and in the Assets field layout, there are other fields (author and date)? Is author a Users field and Date a date/time field?
Brad Bell
CC BY-SA 3.0
2016-07-23T12:10:31.257Z
Yes. My entry has an assets field. The assets are restricted to a source that has some custom fields in its field layout. So each asset/file has a few basic plain text fields that I want to include in my search, nothing fancy, it's simply plain text.
Luke
CC BY-SA 3.0
2016-07-25T21:52:02.227Z
I don't think there's any way to do this in one query. You could get the get the actual `author` field and do another search against that (and merge the results) or go the "relatedTo" route. See here for differences: https://craftcms.stackexchange.com/questions/1387/using-search-vs-relatedto-in-a-template
Brad Bell
CC BY-SA 3.0
2016-08-20T18:44:53.093Z
I have one more idea.. Can I do something like a "JOIN" with mysql? Since **all** items contain at least one file, a collection could be _build_ with the file.author field included as a field on the same level as the entries field. Then this collection should be searchable. Is it possible to create such a collection with the content of the assets field as an entries field?
Luke
<assets><field><search><entries-field>
Search entries AND within associated assets fields
# Search entries AND within associated assets fields

(Asked by: username_0 on 2016-07-22)

I have a large list of entries. Each entry has a a bunch of fields and a file-field with one or two files. Those files have related field (Author, Date, ..).
I have now implemented a search. All things are working so far. However, the search does not include the asset's fields. Only the asset's title is recognized. 
Can I somehow search in asset fields? I tried something like
field1: *query* AND files.author: *query*

but this does not work :(
I have read that I can search all assets that are related to that section, but I would like to include this query in one query than in two queries..

**Tags:** assets, field, search, entries-field

> **Comments:**
> **username_1**: Just to be clear, your entry has an Assets field on it and in the Assets field layout, there are other fields (author and date)? Is author a Users field and Date a date/time field?  
> **username_0**: Yes. My entry has an assets field. The assets are restricted to a source that has some custom fields in its field layout. So each asset/file has a few basic plain text fields that I want to include in my search, nothing fancy, it's simply plain text.  
> **username_1**: I don't think there's any way to do this in one query. You could get the get the actual `author` field and do another search against that (and merge the results) or go the "relatedTo" route. See here for differences: https://craftcms.stackexchange.com/questions/1387/using-search-vs-relatedto-in-a-template  
> **username_0**: I have one more idea.. Can I do something like a "JOIN" with mysql? Since **all** items contain at least one file, a collection could be _build_ with the file.author field included as a field on the same level as the entries field. Then this collection should be searchable. Is it possible to create such a collection with the content of the assets field as an entries field?  

---

## 1 answers

### Answer by username_0 on 2016-08-24. Score: 1

I have now found a workaround using the information @brad-bell provided, that is working, but not exactly what I wanted:
{# Normal search #}
{% set resultsA = craft.entries(search_criteria).ids() %}

{# Assets search #}
{% set assets = craft.assets.customFieldName('*'~query~'*') %}
{% set resultsB = craft.entries.relatedTo({
        targetElement: assets,
        field: 'customFieldName'
    }).ids() | without(resultsA) %}

{# Merge results, paginate #}
{% set results = resultsA | merge(resultsB) %}
{% paginate results.limit(10) as pageInfo, pageEntries %}

How I did it:

Perform a normal search for entries containing the query in one of their fields (specified in search_criteria, not included in the code above) and get the IDs of the found entries.
Search for assets that contain the query in their customField. Since I'm only searching one field, I do not use the search-function, which would be an option when searching multiple fields.
Now I'm getting the entries that are related to the assets. As in step 1, I attain the IDs, ignoring IDs already present in the first set of results, to avoid duplicates (which I found out late are not a problem, since craft.entries - in point 5 - only returns each entry once)
I merge both arrays.
Get an ElementCriteriaObject containing all entries with the IDs collected before.
I paginate them as usual.

If you do not need to paginate, step 5-6 are might be unnecessary. You could simple use find() instead of ids() to get arrays which you then could merge. This however will cause duplicates!
Although this code works, it has a few disadvantages:

The results are ordered by their position in both arrays. resultsA are listed first, followed by resultsB. In my application I can live with that, since queries fetching many results in assets will usually fetch almost no results in entries and vice versa. I tried to sort and order them by the number of occurrences, however, this did not work using only twig functions. With the help of a simple plugin as host for some php array sorting, ordering of the results could be improved.
Performance is not very good, since many queries are necessary. 

Idea
One idea in my mind was to build a custom MySQL-query, which JOINS the assets fields (combined to a string if there are multiple ones) to the entry's row. However, I was not able to accomplish that. If anyone knows how to do so: Let me know!
Thanks for your help!


---

