Merge pull request #303 from Friss/update-quick-start
Update quick start guide's complex query.
This commit is contained in:
@ -113,49 +113,50 @@ NOTE: In this example, `request` and `response` are http://expressjs.com/api.htm
|
|||||||
|
|
||||||
[source,js]
|
[source,js]
|
||||||
-----------------
|
-----------------
|
||||||
var pageNum = request.param('page', 1);
|
var pageNum = request.params.page;
|
||||||
var perPage = request.param('per_page', 15);
|
var perPage = request.params.per_page;
|
||||||
var userQuery = request.param('search_query');
|
var userQuery = request.params.search_query;
|
||||||
var userId = request.session.userId;
|
var userId = request.session.userId;
|
||||||
|
|
||||||
client.search({
|
client.search({
|
||||||
index: 'posts',
|
index: 'posts',
|
||||||
from: (pageNum - 1) * perPage,
|
from: (pageNum - 1) * perPage,
|
||||||
size: perPage,
|
size: perPage,
|
||||||
body: {
|
body: {
|
||||||
query: {
|
query: {
|
||||||
filtered: {
|
filtered: {
|
||||||
query: {
|
query: {
|
||||||
match: {
|
match: {
|
||||||
// match the query agains all of
|
// match the query agains all of
|
||||||
// the fields in the posts index
|
// the fields in the posts index
|
||||||
_all: userQuery
|
_all: userQuery
|
||||||
}
|
|
||||||
},
|
|
||||||
filter: {
|
|
||||||
// only return documents that are
|
|
||||||
// public or owned by the current user
|
|
||||||
or: [
|
|
||||||
{
|
|
||||||
term: { privacy: "public" }
|
|
||||||
},
|
|
||||||
{
|
|
||||||
term: { owner: userId }
|
|
||||||
}
|
}
|
||||||
]
|
},
|
||||||
|
filter: {
|
||||||
|
// only return documents that are
|
||||||
|
// public or owned by the current user
|
||||||
|
or: [
|
||||||
|
{
|
||||||
|
term: { privacy: "public" }
|
||||||
|
},
|
||||||
|
{
|
||||||
|
term: { owner: userId }
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}, function (error, response) {
|
}, function (err, res) {
|
||||||
if (err) {
|
if (err) {
|
||||||
// handle error
|
// handle error
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
response.render('search_results', {
|
response.render('search_results', {
|
||||||
results: response.hits.hits,
|
results: res.hits.hits,
|
||||||
page: pageNum,
|
page: pageNum,
|
||||||
pages: Math.ceil(response.hits.total / perPage)
|
pages: Math.ceil(res.hits.total / perPage)
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
-----------------
|
-----------------
|
||||||
|
|||||||
Reference in New Issue
Block a user