Search within a specific type from Mustache

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!

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.

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" ]
}

}
}

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.

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.

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();

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