Reputation: 2790
I'm trying to do a query on elasticsearch but I'm suprise by the fact that the response is very slow.
request = {
"query": {
"bool": {
"must": [{"match": {"user.id": self.id}}]
}
},
"sort": [
{"created_at": {"order": "asc"}}
]
}
The query take three seconds and a half (3.5s) to have a response. I'm working in local so the problem can't come from a network issue.
How can I improve the speed of my query knowing that the query hit an index containing 40 millions documents?
Upvotes: 2
Views: 114
Reputation: 16355
Assuming, you are using the default settings, you are using the Standard Analyzer
The match query before actually querying, will apply the same standard analyzer to the search term. It will also calculate the score of the matching documents which will also have some impact on the query performance.
The term query does not apply any analyzers to the search term, so will only look for that exact term in the inverted index.
Now, whether the scoring is computed or not, depends on the query-filter-context
Query context
A query clause used in query context answers the question “How well does this document match this query clause?” Besides deciding whether or not the document matches, the query clause also calculates a _score representing how well the document matches, relative to other documents.
Filter context
In filter context, a query clause answers the question “Does this document match this query clause?” The answer is a simple Yes or No — no scores are calculated.
Frequently used filters will be cached automatically by Elasticsearch, to speed up performance.
You may create your query as described below :
GET _search
{
"query": {
"bool": {
"must": [
{ "match_all": {}}
],
"filter": [
{ "term": { "user.id": <YOUR_ID_PARAM> }},
]
},
"sort" :{.....}
}
}
Upvotes: 1