Module: Elasticsearch::Git::Repository::ClassMethods

Defined in:
lib/elasticsearch/git/repository.rb

Instance Method Summary collapse

Instance Method Details

#repositories_countObject

For Overwrite



378
379
380
# File 'lib/elasticsearch/git/repository.rb', line 378

def repositories_count
  10
end

#search(query, type: :all, page: 1, per: 20, options: {}) ⇒ Object



382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
# File 'lib/elasticsearch/git/repository.rb', line 382

def search(query, type: :all, page: 1, per: 20, options: {})
  results = { blobs: [], commits: []}

  case type.to_sym
  when :all
    results[:blobs] = search_blob(query, page: page, per: per, options: options)
    results[:commits] = search_commit(query, page: page, per: per, options: options)
  when :blob
    results[:blobs] = search_blob(query, page: page, per: per, options: options)
  when :commit
    results[:commits] = search_commit(query, page: page, per: per, options: options)
  end

  results
end

#search_blob(query, type: :all, page: 1, per: 20, options: {}) ⇒ Object



475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
# File 'lib/elasticsearch/git/repository.rb', line 475

def search_blob(query, type: :all, page: 1, per: 20, options: {})
  page ||= 1

  query_hash = {
    query: {
      bool: {
        must: {
          multi_match: {
            query: query,
            operator: :and,
            fields: [ 'blob.content', 'blob.file_name' ]
          }
        }
      }
    },
    aggs: {
      languageFacet: {
        terms: {
          field: :language,
          all_terms: true,
          size: 20
        }
      },
      blobRepositoryFaset: {
        terms: {
          field: :rid,
          all_terms: true,
          size: repositories_count
        }
      }
    },
    size: per,
    from: per * (page - 1)
  }

  query_hash[:query][:bool][:filter] = []

  if options[:repository_id]
    query_hash[:query][:bool][:filter] << {
      terms: {
        'blob.rid' => [options[:repository_id]].flatten
      }
    }
  end

  if options[:language]
    query_hash[:query][:bool][:filter] << {
      terms: {
        'blob.language' => [options[:language]].flatten
      }
    }
  end

  options[:order] = :default if options[:order].blank?

  order = case options[:order].to_sym
          when :recently_indexed
            { _timestamp: { order: :desc, mode: :min } }
          when :last_indexed
            { _timestamp: { order: :asc, mode: :min } }
          else
            {}
          end

  query_hash[:sort] = order.blank? ? [:_score] : [order, :_score]

  if options[:highlight]
    query_hash[:highlight] = {
      pre_tags: ["gitlabelasticsearch→"],
      post_tags: ["←gitlabelasticsearch"],
      fields: {
        "blob.content" => {},
        "blob.file_name" => {},
        "type" => "fvh",
        "boundary_chars" => "\n"
      }
    }
  end

  res = self.__elasticsearch__.search(query_hash)

  {
    results: res.results,
    total_count: res.size,
    languages: res.response["aggregations"]["languageFacet"]["buckets"],
    repositories: res.response["aggregations"]["blobRepositoryFaset"]["buckets"]
  }
end

#search_commit(query, page: 1, per: 20, options: {}) ⇒ Object



398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
# File 'lib/elasticsearch/git/repository.rb', line 398

def search_commit(query, page: 1, per: 20, options: {})
  page ||= 1

  fields = %w(message^10 sha^5 author.name^2 author.email^2 committer.name committer.email).map {|i| "commit.#{i}"}

  query_hash = {
    query: {
      bool: {
        must: [{
          multi_match: {
            fields: fields,
            query: "#{query}",
            operator: :or
          }
        }]
      }
    },
    aggs: {
      commitRepositoryFaset: {
        terms: {
          field: "commit.rid",
          all_terms: true,
          size: repositories_count
        }
      }
    },
    size: per,
    from: per * (page - 1)
  }

  if query.blank?
    query_hash[:query][:bool][:must] = { match_all: {}}
    query_hash[:track_scores] = true
  end

  if options[:repository_id]
    query_hash[:query][:bool][:filter] = {
      terms: {
        'commit.rid' => [options[:repository_id]].flatten
      }
    }
  end

  if options[:highlight]
    es_fields = fields.map { |field| field.split('^').first }.inject({}) do |memo, field|
      memo[field.to_sym] = {}
      memo
    end

    query_hash[:highlight] = {
        pre_tags: ["gitlabelasticsearch→"],
        post_tags: ["←gitlabelasticsearch"],
        fields: es_fields
    }
  end

  options[:order] = :default if options[:order].blank?

  order = case options[:order].to_sym
          when :recently_indexed
            { _timestamp: { order: :desc, mode: :min } }
          when :last_indexed
            { _timestamp: { order: :asc,  mode: :min } }
          else
            {}
          end

  query_hash[:sort] = order.blank? ? [:_score] : [order, :_score]

  res = self.__elasticsearch__.search(query_hash)
  {
    results: res.results,
    total_count: res.size,
    repositories: res.response["aggregations"]["commitRepositoryFaset"]["buckets"]
  }
end

#search_file_names(query, page: 1, per: 20, options: {}) ⇒ Object



564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
# File 'lib/elasticsearch/git/repository.rb', line 564

def search_file_names(query, page: 1, per: 20, options: {})
  query_hash = {
    fields: ['blob.path'],
    query: {
        fuzzy: {
            'repository.blob.path' => { value: query }
        },
    },
    filter: {
        term: {
            'repository.blob.rid' => [options[:repository_id]].flatten
        }
    },
    size: per,
    from: per * (page - 1)
  }

  self.__elasticsearch__.search(query_hash)
end