# Search within a specific type from Mustache

**URL:** <https://discuss.elastic.co/t/search-within-a-specific-type-from-mustache/127307>\
**Category:** Elasticsearch\
**Created:** [April 9, 2018, 10:50am UTC](https://discuss.elastic.co/t/search-within-a-specific-type-from-mustache/127307 "2018-04-09T10:50:37Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![najib](https://avatars.discourse-cdn.com/v4/letter/n/9d8465/32.png) [@najib](https://discuss.elastic.co/u/najib)\
**Post date:** [April 9, 2018, 10:50am UTC](https://discuss.elastic.co/t/search-within-a-specific-type-from-mustache/127307/1 "2018-04-09T10:50:37Z")

</div>

Hi there,  
Is it possible to make a Search query using a Mustache script but restrict the Search to give only results within a specific Type?  
My Java client code would send the type name to the Mustache script as a parameter for example.  
Thanks a million!

---

<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:** [April 9, 2018, 11:45am UTC](https://discuss.elastic.co/t/search-within-a-specific-type-from-mustache/127307/2 "2018-04-09T11:45:40Z")

</div>

Which Java client?

BTW in 6.x you can not create multiple types per index so whatever the answer is it will become useless with 6.x.

Type is going to be removed later on.

---

<div class="post-metadata">

**Author:** ![najib](https://avatars.discourse-cdn.com/v4/letter/n/9d8465/32.png) [@najib](https://discuss.elastic.co/u/najib)\
**Post date:** [April 9, 2018, 12:02pm UTC](https://discuss.elastic.co/t/search-within-a-specific-type-from-mustache/127307/3 "2018-04-09T12:02:31Z")

</div>

I see, thanks for that!

I am currently using 5.1.1  
Planning to upgrade to the latest version soon.

I am using this package in Java: org.elasticsearch.client

I have multiple Types within one Index and I want the search results to return only results with a specific Type. Using Mustache called from the above Java client.

A Mustache script with something like this where I can pass the Type through {{Param\_Type}} but it doesn't seem to work. Thanks a lot!

{

```
"_source": ["_id", "title", "text"],

```

"query": {

```
"type" : {
        "value" : "{{param_type}}"
    },

"multi_match": {
  "type": "most_fields", 
  "query": "{{param_text}}",
  "fields": ["title^10", "text", "text.stemmed^10"]
}

```

}  
}

---

<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:** [April 9, 2018, 12:33pm UTC](https://discuss.elastic.co/t/search-within-a-specific-type-from-mustache/127307/4 "2018-04-09T12:33:28Z")

</div>

Instead of calling

```
GET index/_search

```

Call

```
GET index/type/_search

```

Not sure what is the relationship with mustache though.

If you need more information share your current java code.

---

<div class="post-metadata">

**Author:** ![najib](https://avatars.discourse-cdn.com/v4/letter/n/9d8465/32.png) [@najib](https://discuss.elastic.co/u/najib)\
**Post date:** [April 9, 2018, 12:51pm UTC](https://discuss.elastic.co/t/search-within-a-specific-type-from-mustache/127307/5 "2018-04-09T12:51:01Z")

</div>

Indeed, that is the call I want to make. I want to call: index/type/\_search

But I want to do it by using a Java client and a Mustsache script.

This is the Java code:

```
Map<String, Object> params = new HashMap<>();
params.put("param_text", query);
params.put("param_type", "article");

SearchResponse response = new SearchTemplateRequestBuilder(client).setScript("template_file").setScriptType(ScriptType.FILE).setScriptParams(params).setRequest(new SearchRequest()).get().getResponse();

```

And this is the (erroneous) content of the Mustache script referenced in the Java code above:

```
{

	"_source": ["_id", "title", "text"],

  "query": {

    "type" : {
            "value" : "{{param_type}}"
        },
  
    "multi_match": {
      "type": "most_fields", 
      "query": "{{param_text}}",
      "fields": ["title^10", "text", "text.stemmed^10"]
    }
  }
}

```

I could do well with specifying the Type in the Java code itself, instead of passing it as a parameter to the Mustache script. Both solutions would work for me but I can't work out any of the two so far.

Thanks a lot again and apologies for any lack or clarity.

---

<div class="post-metadata">

**Author:** ![najib](https://avatars.discourse-cdn.com/v4/letter/n/9d8465/32.png) [@najib](https://discuss.elastic.co/u/najib)\
**Post date:** [April 9, 2018, 1:00pm UTC](https://discuss.elastic.co/t/search-within-a-specific-type-from-mustache/127307/6 "2018-04-09T13:00:37Z")

</div>

This said, as you have mentionned that from 6.x onwards, it is not possible to create multiple types per index, I think I will split my current index into different indices (each having a unique type) which would solve the issue.

Thanks again a lot!

EDIT:  
I figured out how to do it, I am putting it for the record. It is done in the SearchRequest parameter.

`SearchResponse response = new SearchTemplateRequestBuilder(client).setScript("template_file").setScriptType(ScriptType.FILE).setScriptParams(params).setRequest(new SearchRequest().types("article")).get().getResponse();`

---

<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:** [May 7, 2018, 1:01pm UTC](https://discuss.elastic.co/t/search-within-a-specific-type-from-mustache/127307/7 "2018-05-07T13:01:02Z")

</div>

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