amachado
amachado

Reputation: 1040

elasticsearch java search query

I'm doing a ecomerce website and for my initial page I will put the top 10 product that are finishing. So what I'm doing is a search query with match all, filtered by finish_dat > than not +1h and limiting the size to 10 (I don't know if this is the best way to do this...)

The thing here is I want to create a config with this query (for in future to change, lets say, to top20, or filter by now +1d), so I'm reading the query string to a xml file and then use esClient.prepareSearch("products").setTypes("product").setSearchType(SearchType.QUERY_AND_FETCH).setQuery(myQuery).execute().actionGet();

The problem is I'm trying to use the body query from the curl:

{  "sort" : [
        { "finish_date" : {"order" : "asc"}}     
    ],
     "query" : {"match_all" : {}},
      "filter": {
        "range": {
          "finish_date": {
            "from": "now+1h"
          }
        }
      }

And is giving an error

Root cause is: Failed to execute phase [query_fetch], all shards failed; shardFailures {[lWQlkEM_QQCJY0RzHBz_vw][auction][4]: RemoteTransportException[[MODAM][inet[/192.168.1.4:9301]][search/phase/query+fetch]]; nested: SearchParseException[[auction][4]: from[-1],size[-1]: Parse Failure [Failed to parse source [{"query_binary":"InNvcnQiIDogW3sgImZpbmlzaF9kYXRlIiA6IHsib3JkZXIiIDogImFzYyJ9fV0sInF1ZXJ5IiA6IHsibWF0Y2hfYWxsIiA6IHt9fSwiZmlsdGVyIjogeyJyYW5nZSI6IHsiZmluaXNoX2RhdGUiOiB7ImZyb20iOiAibm93KzFoIn19fSwgImZyb20iIDogMCwgInNpemUiIDogMTA="}]]]; nested: QueryParsingException[[auction] [_na] query malformed, must start with start_object]; }{[Ltw-Ez47SD6WkLgON6kvHQ][auction][3]: SearchParseException[[auction][3]: from[-1],size[-1]: Parse Failure [Failed to parse source [{"query_binary":"InNvcnQiIDogW3sgImZpbmlzaF9kYXRlIiA6IHsib3JkZXIiIDogImFzYyJ9fV0sInF1ZXJ5IiA6IHsibWF0Y2hfYWxsIiA6IHt9fSwiZmlsdGVyIjogeyJyYW5nZSI6IHsiZmluaXNoX2RhdGUiOiB7ImZyb20iOiAibm93KzFoIn19fSwgImZyb20iIDogMCwgInNpemUiIDogMTA="}]]]; nested: QueryParsingException[[auction] [_na] query malformed, must start with start_object]; }{[Ltw-Ez47SD6WkLgON6kvHQ][auction][1]: SearchParseException[[auction][1]: from[-1],size[-1]: Parse Failure [Failed to parse source [{"query_binary":"InNvcnQiIDogW3sgImZpbmlzaF9kYXRlIiA6IHsib3JkZXIiIDogImFzYyJ9fV0sInF1ZXJ5IiA6IHsibWF0Y2hfYWxsIiA6IHt9fSwiZmlsdGVyIjogeyJyYW5nZSI6IHsiZmluaXNoX2RhdGUiOiB7ImZyb20iOiAibm93KzFoIn19fSwgImZyb20iIDogMCwgInNpemUiIDogMTA="}]]]; nested: QueryParsingException[[auction] [_na] query malformed, must start with start_object]; }{[Ltw-Ez47SD6WkLgON6kvHQ][auction][2]: SearchParseException[[auction][2]: from[-1],size[-1]: Parse Failure [Failed to parse source [{"query_binary":"InNvcnQiIDogW3sgImZpbmlzaF9kYXRlIiA6IHsib3JkZXIiIDogImFzYyJ9fV0sInF1ZXJ5IiA6IHsibWF0Y2hfYWxsIiA6IHt9fSwiZmlsdGVyIjogeyJyYW5nZSI6IHsiZmluaXNoX2RhdGUiOiB7ImZyb20iOiAibm93KzFoIn19fSwgImZyb20iIDogMCwgInNpemUiIDogMTA="}]]]; nested: QueryParsingException[[auction] [_na] query malformed, must start with start_object]; }{[Ltw-Ez47SD6WkLgON6kvHQ][auction][0]: SearchParseException[[auction][0]: from[-1],size[-1]: Parse Failure [Failed to parse source [{"query_binary":"InNvcnQiIDogW3sgImZpbmlzaF9kYXRlIiA6IHsib3JkZXIiIDogImFzYyJ9fV0sInF1ZXJ5IiA6IHsibWF0Y2hfYWxsIiA6IHt9fSwiZmlsdGVyIjogeyJyYW5nZSI6IHsiZmluaXNoX2RhdGUiOiB7ImZyb20iOiAibm93KzFoIn19fSwgImZyb20iIDogMCwgInNpemUiIDogMTA="}]]]; nested: QueryParsingException[[auction] [_na] query malformed, must start with start_object]; } org.elasticsearch.action.search.SearchPhaseExecutionException: Failed to execute phase [query_fetch], all shards failed; shardFailures {[lWQlkEM_QQCJY0RzHBz_vw][auction][4]: RemoteTransportException[[MODAM][inet[/192.168.1.4:9301]][search/phase/query+fetch]]; nested: SearchParseException[[auction][4]: from[-1],size[-1]: Parse Failure [Failed to parse source [{"query_binary":"InNvcnQiIDogW3sgImZpbmlzaF9kYXRlIiA6IHsib3JkZXIiIDogImFzYyJ9fV0sInF1ZXJ5IiA6IHsibWF0Y2hfYWxsIiA6IHt9fSwiZmlsdGVyIjogeyJyYW5nZSI6IHsiZmluaXNoX2RhdGUiOiB7ImZyb20iOiAibm93KzFoIn19fSwgImZyb20iIDogMCwgInNpemUiIDogMTA="}]]]; nested: QueryParsingException[[auction] [_na] query malformed, must start with start_object]; }{[Ltw-Ez47SD6WkLgON6kvHQ][auction][3]: SearchParseException[[auction][3]: from[-1],size[-1]: Parse Failure [Failed to parse source [{"query_binary":"InNvcnQiIDogW3sgImZpbmlzaF9kYXRlIiA6IHsib3JkZXIiIDogImFzYyJ9fV0sInF1ZXJ5IiA6IHsibWF0Y2hfYWxsIiA6IHt9fSwiZmlsdGVyIjogeyJyYW5nZSI6IHsiZmluaXNoX2RhdGUiOiB7ImZyb20iOiAibm93KzFoIn19fSwgImZyb20iIDogMCwgInNpemUiIDogMTA="}]]]; nested: QueryParsingException[[auction] [_na] query malformed, must start with start_object]; }{[Ltw-Ez47SD6WkLgON6kvHQ][auction][1]: SearchParseException[[auction][1]: from[-1],size[-1]: Parse Failure [Failed to parse source [{"query_binary":"InNvcnQiIDogW3sgImZpbmlzaF9kYXRlIiA6IHsib3JkZXIiIDogImFzYyJ9fV0sInF1ZXJ5IiA6IHsibWF0Y2hfYWxsIiA6IHt9fSwiZmlsdGVyIjogeyJyYW5nZSI6IHsiZmluaXNoX2RhdGUiOiB7ImZyb20iOiAibm93KzFoIn19fSwgImZyb20iIDogMCwgInNpemUiIDogMTA="}]]]; nested: QueryParsingException[[auction] [_na] query malformed, must start with start_object]; }{[Ltw-Ez47SD6WkLgON6kvHQ][auction][2]: SearchParseException[[auction][2]: from[-1],size[-1]: Parse Failure [Failed to parse source [{"query_binary":"InNvcnQiIDogW3sgImZpbmlzaF9kYXRlIiA6IHsib3JkZXIiIDogImFzYyJ9fV0sInF1ZXJ5IiA6IHsibWF0Y2hfYWxsIiA6IHt9fSwiZmlsdGVyIjogeyJyYW5nZSI6IHsiZmluaXNoX2RhdGUiOiB7ImZyb20iOiAibm93KzFoIn19fSwgImZyb20iIDogMCwgInNpemUiIDogMTA="}]]]; nested: QueryParsingException[[auction] [_na] query malformed, must start with start_object]; }{[Ltw-Ez47SD6WkLgON6kvHQ][auction][0]: SearchParseException[[auction][0]: from[-1],size[-1]: Parse Failure [Failed to parse source [{"query_binary":"InNvcnQiIDogW3sgImZpbmlzaF9kYXRlIiA6IHsib3JkZXIiIDogImFzYyJ9fV0sInF1ZXJ5IiA6IHsibWF0Y2hfYWxsIiA6IHt9fSwiZmlsdGVyIjogeyJyYW5nZSI6IHsiZmluaXNoX2RhdGUiOiB7ImZyb20iOiAibm93KzFoIn19fSwgImZyb20iIDogMCwgInNpemUiIDogMTA="}]]]; nested: QueryParsingException[[auction] [_na] query malformed, must start with start_object]; } at org.elasticsearch.action.search.type.TransportSearchTypeAction$BaseAsyncAction.onFirstPhaseResult(TransportSearchTypeAction.java:272) at org.elasticsearch.action.search.type.TransportSearchTypeAction$BaseAsyncAction$3.onFailure(TransportSearchTypeAction.java:224) at org.elasticsearch.search.action.SearchServiceTransportAction.sendExecuteFetch(SearchServiceTransportAction.java:307) at org.elasticsearch.action.search.type.TransportSearchQueryAndFetchAction$AsyncAction.sendExecuteFirstPhase(TransportSearchQueryAndFetchAction.java:71) at org.elasticsearch.action.search.type.TransportSearchTypeAction$BaseAsyncAction.performFirstPhase(TransportSearchTypeAction.java:216) at org.elasticsearch.action.search.type.TransportSearchTypeAction$BaseAsyncAction$4.run(TransportSearchTypeAction.java:292) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) at java.lang.Thread.run(Thread.java:744) 

Someone knows how to do this? (an example or point to the right direction)??

Thanks in advance

Upvotes: 3

Views: 2030

Answers (1)

Garry Welding
Garry Welding

Reputation: 3609

You need to use a template query, this is the officially supported way of doing what you want, and it also supports storing the templates on the file system, but as mustache templates rather then XML.

You can read more about template queries at http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-template-query.html.

You wont actually need to install mustache into your project for this to work, but if it interests you you can read more about it at http://mustache.github.io/. They even have a Java version of it, https://github.com/spullara/mustache.java.

Upvotes: 4

Related Questions