acsh
acsh

Reputation: 103

Return list of strings which contain the keyword entered by the user in elastic search

I am looking for a type of search in elastic search which returns all the strings which contain a keyword. Following query only matches if the whole text is 'google' (case insensitive, so it also matches Google or GooGlE etc). How do I match for the 'google' inside of another string or even if I write 'goog'?

query: {
bool : {
    must: {
        match: { text: 'google'} 
    }
}

}

Upvotes: 0

Views: 311

Answers (2)

Polynomial Proton
Polynomial Proton

Reputation: 5135

How do I match for the 'google' inside of another string or even if I write 'goog'?

Wildcard query can help you find the sub-text you are looking for within a field.

In your case, it'll look like below. (It will return any documents where the text goog exists no matter what the pre and post text is for example: this is google OR he is googling the answer)

GET /_search
{
    "query": {
        "wildcard": {
            "field_name": {
                "value": "*goog*",              
            }
        }
    }
}

Upvotes: 1

Spider
Spider

Reputation: 455

Baeldung has a tutorial on this https://www.baeldung.com/spring-data-elasticsearch-tutorial, but below is how you retrieve data:

String articleTitle = "Spring Data Elasticsearch";
SearchQuery searchQuery = new NativeSearchQueryBuilder()
 .withQuery(matchQuery("title", articleTitle).minimumShouldMatch("75%"))
 .build();

List<Article> articles = elasticsearchTemplate.queryForList(searchQuery, Article.class);

Upvotes: 1

Related Questions