<commit_before>[[java-search]]
== Search API

The search API allows one to execute a search query and get back search hits
that match the query. It can be executed across one or more indices and
across one or more types. The query can provided using the <<java-query-dsl,query Java API>>.
The body of the search request is built using the `SearchSourceBuilder`. Here is an example:

[source,java]
--------------------------------------------------
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.index.query.QueryBuilders.*;
--------------------------------------------------

[source,java]
--------------------------------------------------
SearchResponse response = client.prepareSearch("index1", "index2")
        .setTypes("type1", "type2")
        .setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
        .setQuery(QueryBuilders.termQuery("multi", "test"))                 // Query
        .setPostFilter(QueryBuilders.rangeQuery("age").from(12).to(18))     // Filter
        .setFrom(0).setSize(60).setExplain(true)
        .execute()
        .actionGet();
--------------------------------------------------

Note that all parameters are optional. Here is the smallest search call
you can write:

[source,java]
--------------------------------------------------
// MatchAll on the whole cluster with all default options
SearchResponse response = client.prepareSearch().execute().actionGet();
--------------------------------------------------

NOTE:   Although the Java API defines the additional search types QUERY_AND_FETCH and
        DFS_QUERY_AND_FETCH, these modes are internal optimizations and should not
        be specified explicitly by users of the API.

For more information on the search operation, check out the REST
{ref}/search.html[search] docs.


[[java-search-scrolling]]
=== Using scrolls in Java

Read the {ref}/search-request-scroll.html[scroll documentation]
first!

[source,java]
--------------------------------------------------
import static org.elasticsearch.index.query.QueryBuilders.*;

QueryBuilder qb = termQuery("multi", "test");

SearchResponse scrollResp = client.prepareSearch(test)
        .setSearchType(SearchType.SCAN)
        .setScroll(new TimeValue(60000))
        .setQuery(qb)<commit_msg>Replaced SCAN search type with _doc sort in scroll query<commit_after>[[java-search]]
== Search API

The search API allows one to execute a search query and get back search hits
that match the query. It can be executed across one or more indices and
across one or more types. The query can provided using the <<java-query-dsl,query Java API>>.
The body of the search request is built using the `SearchSourceBuilder`. Here is an example:

[source,java]
--------------------------------------------------
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.index.query.QueryBuilders.*;
--------------------------------------------------

[source,java]
--------------------------------------------------
SearchResponse response = client.prepareSearch("index1", "index2")
        .setTypes("type1", "type2")
        .setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
        .setQuery(QueryBuilders.termQuery("multi", "test"))                 // Query
        .setPostFilter(QueryBuilders.rangeQuery("age").from(12).to(18))     // Filter
        .setFrom(0).setSize(60).setExplain(true)
        .execute()
        .actionGet();
--------------------------------------------------

Note that all parameters are optional. Here is the smallest search call
you can write:

[source,java]
--------------------------------------------------
// MatchAll on the whole cluster with all default options
SearchResponse response = client.prepareSearch().execute().actionGet();
--------------------------------------------------

NOTE:   Although the Java API defines the additional search types QUERY_AND_FETCH and
        DFS_QUERY_AND_FETCH, these modes are internal optimizations and should not
        be specified explicitly by users of the API.

For more information on the search operation, check out the REST
{ref}/search.html[search] docs.


[[java-search-scrolling]]
=== Using scrolls in Java

Read the {ref}/search-request-scroll.html[scroll documentation]
first!

[source,java]
--------------------------------------------------
import static org.elasticsearch.index.query.QueryBuilders.*;

QueryBuilder qb = termQuery("multi", "test");

SearchResponse scrollResp = client.prepareSearch(test)
        .addSort(FieldSortBuilder.DOC_FIELD_NAME, SortOrder.ASC)
        .setScroll(new TimeValue(60000))
        .setQuery(qb)
{'fraction_non_alphanumeric': 0.23174872665534804, 'fraction_numerical': 0.006791171477079796, 'mean_word_length': 2.158847184986595, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5423114', 'n_tokens_mistral': 1162, 'n_tokens_neox': 1137, 'n_words': 367}