Troublewriting query in ES using JAVA API

I have tried a lot and searched stackoverflow about building queries in JAVA for ES, I think I have basic understanding but still not able to write queries properly. Can someone please explain in detail with several examples, it would be really really awesome guys.
And if you haven't check Falcon Heavy launch video, I think you should do that first :stuck_out_tongue:

My Query string ->

GET instagram_2018/_search
{
  "_source": [
    "message",
    "created_time"
  ],
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "object_type": "comment"
          }
        },
        {
          "wildcard": {
            "profile_name": "Some name"
          }
        },
        {
          "wildcard": {
            "post_id": "*_123456789"
          }
        }
      ]
    }
  },
  "sort": [
    {
      "created_time": {
        "order": "desc"
      }
    }
  ]
} 

Thanks in advance!

Something like:

client.search(
	new SearchRequest("instagram_2018").source(
			new SearchSourceBuilder()
			.query(QueryBuilders.boolQuery()
				.must(QueryBuilders.matchQuery("object_type", "comment"))
				.must(QueryBuilders.wildcardQuery("profile_name", "Some name"))
				.must(QueryBuilders.wildcardQuery("post_id", "*_123456789"))
			)
			.sort("created_time", SortOrder.DESC)
			.fetchSource(new String[]{"message", "created_time"}, null)
	)
);

BTW this is a VERY bad idea to do this: QueryBuilders.wildcardQuery("post_id", "*_123456789")

2 Likes

thanks! btw may i know why it's bad idea to use wildcardQuery ?

Wilcard queries can be very slow, as they need to iterate over many terms. In order to prevent that, a wildcard term should not start with one of the wildcards * or ?

Additional useful info can be found in the official guide: https://www.elastic.co/guide/en/elasticsearch/guide/current/_wildcard_and_regexp_queries.html

1 Like

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.