Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use ignore_unavailable parameter in ElasticSearch Java client

I'm using ElasticSearch java client as a Maven dependency:

    <dependency>
        <groupId>org.elasticsearch</groupId>
        <artifactId>elasticsearch</artifactId>
        <version>1.7.1</version>
    </dependency>

I am doing search with multi-index, and I'm getting IndexMissingException and I want to ignore it using the parameter: 'ignore_unavailable' but I don't know how to use it, and don't found documentation about this.

https://www.elastic.co/guide/en/elasticsearch/reference/current/multi-index.html (link about ignore_unavailable)

I'm creating the search like this:

    SearchRequestBuilder srb = ess.getClient()
        .prepareSearch(generateIndex(query)) //generateIndex returns a array
        .setTypes("mytype")
        .setSearchType(SearchType.DFS_QUERY_AND_FETCH)
        .setPostFilter(FilterBuilders.rangeFilter("Time")
                .from(time1.toInstant(ZoneOffset.UTC))
                .to(time2.toInstant(ZoneOffset.UTC))
        )   
        .setFrom(0).setSize(RESULT_MAX).setExplain(true)
        //putHeader don't work...
       .putHeader("ignore_unavailable", true);   

How do I use ignore_unavalilable here?

like image 860
RuiOliveiras Avatar asked Dec 25 '22 15:12

RuiOliveiras


1 Answers

You need to use setIndicesOptions(IndicesOptions) - eg.

.prepareSearch(generateIndex(query))
.setIndicesOptions(IndicesOptions.fromOptions(true, false, false, false))

You could also use IndicesOptions.lenientExpandOpen() instead of calling fromOptions().

like image 197
Matt Pearce Avatar answered Feb 23 '23 12:02

Matt Pearce