"Fossies" - the Fresh Open Source Software Archive

Member "elasticsearch-6.8.23/docs/groovy-api/search.asciidoc" (29 Dec 2021, 2741 Bytes) of package /linux/www/elasticsearch-6.8.23-src.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format (assuming AsciiDoc format). Alternatively you can here view or download the uninterpreted source code file. A member file download can also be achieved by clicking within a package contents listing on the according byte size field.

The search API is very similar to the Java search API. The Groovy extension allows to provide the search source to execute as a Closure including the query itself (similar to GORM criteria builder):

def search = node.client.search {
    indices "test"
    types "_doc"
    source {
        query {
            term(test: "value")
        }
    }
}

search.response.hits.each {SearchHit hit ->
    println "Got hit $hit.id from $hit.index/$hit.type"
}

It can also be executed using the "Java API" while still using a closure for the query:

def search = node.client.prepareSearch("test").setQuery({
        term(test: "value")
}).gexecute();

search.response.hits.each {SearchHit hit ->
    println "Got hit $hit.id from $hit.index/$hit.type"
}

The format of the search Closure follows the same JSON syntax as the {ref}/search-search.html[Search API] request.

More examples

Term query where multiple values are provided (see {ref}/query-dsl-terms-query.html[terms]):

def search = node.client.search {
    indices "test"
    types "_doc"
    source {
        query {
            terms(test: ["value1", "value2"])
        }
    }
}

Query string (see {ref}/query-dsl-query-string-query.html[query string]):

def search = node.client.search {
    indices "test"
    types "_doc"
    source {
        query {
            query_string(
                fields: ["test"],
                query: "value1 value2")
        }
    }
}

Pagination (see {ref}/search-request-from-size.html[from/size]):

def search = node.client.search {
    indices "test"
    types "_doc"
    source {
        from = 0
        size = 10
        query {
            term(test: "value")
        }
    }
}

Sorting (see {ref}/search-request-sort.html[sort]):

def search = node.client.search {
    indices "test"
    types "_doc"
    source {
        query {
            term(test: "value")
        }
        sort = [
            date : [ order: "desc"]
        ]
    }
}