I am helping another developer with a ReactJS search application. No matter what I've tried, the search always returns exactly ten results. We need to return all the results of the search.
I have added from:0 and size:1000 to the query, two different ways. I'll include the code below. How can I return all the results, not just the default 10?
This is part of a file called search.js:
const { client, index } = require('./connection')
async function queryTerm (term, datasets){ //this is called from index.js
const body = {
"size":1000,
"from":0,
"_source": {
"includes":[ "field1", "field2", "field3"]
},
"query":{
"query_string":{
"default_operator":"AND",
"query": term
}
}
}
return await client.search({
index: datasets,
body: body
})
}
The connection.js file looks like this:
const { Client } = require('@elastic/elasticsearch')
const es_host = process.env.ES_HOST
const index=
const client = new Client({ node: es_host })
async function testConnection () {
try {
const response = await client.ping({requstTimeout:1000})
} catch (e) {
console.trace('elasticsearch cluster is down');
}
}
module.exports = {
client,index,testConnection
}
The index.js component that uses the search component above, looks like this (this is the beginning of this component, the relevant part):
const express = require('express')
const app = express()
const { serverStatusHtml } = require('./constants')
const cors = require('cors')
const search = require('./search')
app.use(cors())
app.use(express.json())
const port = process.env.PORT || 3001
app.get('/status',(req,res) = > res.send('${serverStatusHtml}'))
app.post('/search', async (req, res) => {
const (query, datasets } = req.body;
let {term} = req.body;
if(query) term = query;
if(term.trim()=="*") res.status(400).send('Invalid search...');
try {
const result = await search.queryTerm(term,datasets) //this is where search.js is called
if(result && result.meta) delete result.meta;
if(result && result.body && result.body._shards) delete result.body._shards;
res.send(result);
} catch (e) {
res.status(400).send('Search failed...')
}
res.end('OK')
})
...
Then there is another component called demo.js that displays the results in a table. It's too long to type here. But I will say that we are using getInitialProps, which always seems to return 10 results only. Do you see anything above that would prevent the search from returning all the results???
Thanks!