Call search_analyzer on text using elasticsearch java api

486 views Asked by At

I have the same question as this post but with one difference. I need to analyze text using the elasticsearch api, but calling the search_analyzer for the field.

(previous answer) how to analyze text in elasticsearch using java api?

The field that I care about has both an analyzer and a custom analyzer. Like so:

       "body": {
          "type": "string",
          "fields": {"exact":{"type":"string"}},
         "analyzer" : "customfullTextUAX",
         "search_analyzer" : "customfullText"
       }

...

The code i have been calling to analyze it uses the analyzer, and i cannot figure out any setting which would call the search_analyzer.

Code I used:

AnalyzeRequest ac = new AnalyzeRequest(index).field(field).text(text).explain(true);
DetailAnalyzeResponse dar = iac.analyze(ac).actionGet().detail();
1

There are 1 answers

5
Val On

You need to explicitly instruct the AnalyzeRequest to use the search analyzer instead:

AnalyzeRequest ac = new AnalyzeRequest(index)
    .field(field)
    .analyzer("customfullText")          <--- add this
    .text(text)
    .explain(true);
DetailAnalyzeResponse dar = iac.analyze(ac).actionGet().detail();