How to make queryStringQuery?

Can anybody please help me to convert the below query to a queryStringQuery which I can execute in java by QueryStringQueryBuilder .

The Query:-

{
  "bool" : {
    "must" : [
      {
        "bool" : {
          "must" : [
            {
              "term" : {
                "languageId" : {
                  "value" : "2",
                  "boost" : 1.0
                }
              }
            },
            {
              "term" : {
                "storeId" : {
                  "value" : "2",
                  "boost" : 1.0
                }
              }
            }
          ],
          "adjust_pure_negative" : true,
          "boost" : 1.0
        }
      }
    ],
    "filter" : [
      {
        "bool" : {
          "adjust_pure_negative" : true,
          "boost" : 1.0
        }
      },
      {
        "bool" : {
          "should" : [
            {
              "range" : {
                "publish_date" : {
                  "from" : "2019-01-26T05:48:27Z",
                  "to" : null,
                  "include_lower" : true,
                  "include_upper" : true,
                  "boost" : 1.0
                }
              }
            },
            {
              "bool" : {
                "must_not" : [
                  {
                    "exists" : {
                      "field" : "publish_date",
                      "boost" : 1.0
                    }
                  }
                ],
                "adjust_pure_negative" : true,
                "boost" : 1.0
              }
            }
          ],
          "adjust_pure_negative" : true,
          "boost" : 1.0
        }
      },
      {
        "bool" : {
          "should" : [
            {
              "range" : {
                "publish_end_date" : {
                  "from" : null,
                  "to" : "2019-07-26T05:48:27Z",
                  "include_lower" : true,
                  "include_upper" : true,
                  "boost" : 1.0
                }
              }
            },
            {
              "bool" : {
                "must_not" : [
                  {
                    "exists" : {
                      "field" : "publish_end_date",
                      "boost" : 1.0
                    }
                  }
                ],
                "adjust_pure_negative" : true,
                "boost" : 1.0
              }
            }
          ],
          "adjust_pure_negative" : true,
          "boost" : 1.0
        }
      }
    ],
    "should" : [
      {
        "bool" : {
          "should" : [
            {
              "match_phrase_prefix" : {
                "name" : {
                  "query" : "spider",
                  "slop" : 0,
                  "max_expansions" : 50,
                  "boost" : 1.0
                }
              }
            },
            {
              "match_phrase_prefix" : {
                "category" : {
                  "query" : "spider",
                  "slop" : 0,
                  "max_expansions" : 50,
                  "boost" : 1.0
                }
              }
            }
          ],
          "adjust_pure_negative" : true,
          "boost" : 1.0
        }
      },
      {
        "bool" : {
          "adjust_pure_negative" : true,
          "boost" : 1.0
        }
      }
    ],
    "adjust_pure_negative" : true,
    "boost" : 1.0
  }
}

I want to execute this like below-

QueryStringQueryBuilder queryBuilder = QueryBuilders.queryStringQuery("The QUERY STRING");
		SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
		searchSourceBuilder.query(queryBuilder);
    	
		SearchRequest searchRequest = new SearchRequest();
		
		/*****set the indices you want to search in*****/
		searchRequest.indices("content","a");
		searchRequest.source(searchSourceBuilder);
		
		try {
			SearchResponse response = client.search(searchRequest);
			System.out.println(response);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

Why do you want to convert a complex query like this to a queryString query?
I don't think it's doable. Or at least hardly doable.

Ok forget about that complected query I just want to implement this simple query but not getting any result:

QueryStringQueryBuilder queryBuilder = QueryBuilders.queryStringQuery(" (name:spider OR category:spider) AND storeId:2 AND languageId:2 ");
		SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
		searchSourceBuilder.query(queryBuilder);
    	
		SearchRequest searchRequest = new SearchRequest();
		
		/*****set the indices you want to search in*****/
		searchRequest.indices("content","a");
		searchRequest.source(searchSourceBuilder);
		
		try {
			SearchResponse response = client.search(searchRequest);
			System.out.println(response);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

May be I am doing something wrong.

Probably. Does it work in Kibana dev console?

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