Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions x-pack/plugins/reporting/server/lib/esqueue/__tests__/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ describe('Worker class', function () {
});

describe('query body', function () {
const conditionPath = 'query.constant_score.filter.bool';
const conditionPath = 'query.bool.filter.bool';
const jobtype = 'test_jobtype';

beforeEach(() => {
Expand All @@ -377,7 +377,7 @@ describe('Worker class', function () {
it('should search by job type', function () {
const { body } = getSearchParams(jobtype);
const conditions = get(body, conditionPath);
expect(conditions.filter).to.eql({ term: { jobtype: jobtype } });
expect(conditions.must).to.eql({ term: { jobtype: jobtype } });
});

it('should search for pending or expired jobs', function () {
Expand All @@ -388,7 +388,7 @@ describe('Worker class', function () {
// this works because we are stopping the clock, so all times match
const nowTime = moment().toISOString();
const pending = { term: { status: 'pending' } };
const expired = { bool: { filter: [
const expired = { bool: { must: [
{ term: { status: 'processing' } },
{ range: { process_expiration: { lte: nowTime } } }
] } };
Expand All @@ -400,6 +400,12 @@ describe('Worker class', function () {
expect(expiredMatch).to.not.be(undefined);
});

it('specify that there should be at least one match', function () {
const { body } = getSearchParams(jobtype);
const conditions = get(body, conditionPath);
expect(conditions).to.have.property('minimum_should_match', 1);
});

it('should use default size', function () {
const { body } = getSearchParams(jobtype);
expect(body).to.have.property('size', defaults.size);
Expand Down
17 changes: 10 additions & 7 deletions x-pack/plugins/reporting/server/lib/esqueue/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -350,17 +350,20 @@ export class Worker extends events.EventEmitter {
excludes: [ 'output.content' ]
},
query: {
constant_score: {
bool: {
filter: {
bool: {
filter: { term: { jobtype: this.jobtype } },
minimum_should_match: 1,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We want a query that's equivalent of

jobtype == this.jobtype &&
  (status == "pending" ||
    (status == "processing" && process_expiration <= nowTime))

I'd recommend trying to do this without using constant_score. Can we try:

{
  query: {
    bool: {
      filter: {
        bool: {
          must: { term: { jobtype: this.jobtype } },
          should: [
            { term: { status: 'pending' } },
            {
              bool: {
                must: [
                  { term: { status: 'processing' } },
                  { range: { process_expiration: { lte: nowTime } } }
                ]
              }
            }
          ]
        }
      }
    }
  }
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dope! I still needed the minimum_should_match: 1 in order for this to work, just an FYI

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With further digging, I find that adding minimum_should_match: 1 was perhaps the only change needed.

must: { term: { jobtype: this.jobtype } },
should: [
{ term: { status: 'pending' } },
{ bool: {
filter: [
{ term: { status: 'processing' } },
{ range: { process_expiration: { lte: nowTime } } }
] }
{
bool: {
must: [
{ term: { status: 'processing' } },
{ range: { process_expiration: { lte: nowTime } } }
]
}
}
]
}
Expand Down