0

Currently able to GET and POST to my collection but need the ability for more complicated queries, I am using bodybuilder to structure the request and axios as my client.

However using POST doesn't return my specified results instead just the first 10 items in my index and using GET I'm unable to send a body for these complicated requests leaving me with POST.

I've switched setting my data from data:data to body:data with the same result.

Currently this is my POST which again returns data but NOT my filtered data just the first 10 items of my collection.

Any insight would be appreciated!

export function searchQuery(search: string) {
  var body = bodybuilder().query("query_string", "query", search).build();

  const data = JSON.stringify(body);
    
  axios({
    url: `${SEARCH_URL}`,
    method: "POST",
    headers: {
      "Content-Type": "application/json",
    },
    body: data,
  }).then((res) => {
    console.log(res);
  });
}

This is my current log of data:

{"query":{"query_string":{"query":"Green"}}}
  • 1
    Please log the `data` to make sure you're sending a correct request to ES. – ilvar Dec 12 '21 at 15:53
  • 2
    Also makes sense to run that logged request against ES manually to ensure it filters the data – ilvar Dec 12 '21 at 15:57
  • This is my current log of data: `{"query":{"query_string":{"query":"Green"}}}` – Adrianwritenow Dec 13 '21 at 14:01
  • 1
    Does it return correct data if you run this query against Elasticsearch directly? – ilvar Dec 13 '21 at 18:13
  • No, it just returns the first 10 items in my collection – Adrianwritenow Dec 13 '21 at 18:30
  • You can use [Explain API](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-explain.html) to find out why some document is matching your query. Probably you have "Green" somewhere in your data? – ilvar Dec 14 '21 at 13:42

1 Answers1

1

Based on the comments in the question above, you are getting only the first 10 items in your collection, when you run the query against elasticsearch directly.

This is because elasticsearch by default returns only 10 documents in the search result if no size param is included in the search query.

If you want to get more than 10 results, you need to modify your search query as

{
  "size": 20,             // change this according to your requirement
  "query": {
    "query_string": {
      "query": "Green"
    }
  }
}
ESCoder
  • 15,431
  • 2
  • 19
  • 42
  • @Adrianwritenow its been a long time ! Can you please accept and upvote the answer, if it helped you resolve your issue. TIA ;-) – ESCoder Feb 14 '22 at 04:30