OpenSearchQueryBuilderTest.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. use App\Services\OpenSearchService;
  3. /**
  4. * 调用 OpenSearchService 的 protected 查询构造方法(纯函数,不连 OpenSearch)
  5. */
  6. function buildSearchQuery(string $method, mixed ...$args): array
  7. {
  8. $service = new OpenSearchService;
  9. return (fn () => $this->{$method}(...$args))->call($service);
  10. }
  11. /**
  12. * 取出 bool.should 里所有 constant_score 的固定分
  13. *
  14. * @return list<float>
  15. */
  16. function constantScoreBoosts(array $query): array
  17. {
  18. return array_values(array_map(
  19. fn (array $clause): float => $clause['constant_score']['boost'],
  20. array_filter($query['bool']['should'], fn (array $clause): bool => isset($clause['constant_score'])),
  21. ));
  22. }
  23. it('scores pali fuzzy per word, rewarding word order and exact spelling', function () {
  24. $query = buildSearchQuery('buildPaliFuzzyQuery', 'evaṃ me sutaṃ');
  25. // 整句 20 + 相邻两对各 5 + 每词拼写 10、同义 2
  26. expect(constantScoreBoosts($query))->toEqual([20.0, 5.0, 5.0, 10.0, 2.0, 10.0, 2.0, 10.0, 2.0]);
  27. $phrase = $query['bool']['should'][0]['constant_score']['filter']['multi_match'];
  28. expect($phrase['type'])->toBe('phrase')
  29. ->and($phrase['query'])->toBe('evaṃ me sutaṃ')
  30. ->and($phrase['fields'])->toBe(['title.text.pali.exact', 'content.text.pali.exact']);
  31. expect($query['bool'])->not->toHaveKey('minimum_should_match');
  32. });
  33. it('drops punctuation-only words so they cannot block matches', function () {
  34. $query = buildSearchQuery('buildPaliFuzzyQuery', 'evaṃ – me .');
  35. $phrase = $query['bool']['should'][0]['constant_score']['filter']['multi_match'];
  36. expect($phrase['query'])->toBe('evaṃ me')
  37. ->and(constantScoreBoosts($query))->toEqual([20.0, 10.0, 2.0, 10.0, 2.0]);
  38. });
  39. it('has no order clauses for a single pali word', function () {
  40. $query = buildSearchQuery('buildPaliFuzzyQuery', 'vitakko');
  41. expect(constantScoreBoosts($query))->toEqual([10.0, 2.0]);
  42. });
  43. it('ranks definition by each matched single bold word, any word form', function () {
  44. $query = buildSearchQuery('buildDefinitionQuery', 'vitakko vicāro');
  45. $should = $query['bool']['should'];
  46. // 每词:单独黑体任一词形 100 + 原拼写 10、多词黑体任一词形 30 + 原拼写 5;再加整句在多词黑体里 30
  47. expect(constantScoreBoosts($query))->toEqual([100.0, 10.0, 30.0, 5.0, 100.0, 10.0, 30.0, 5.0, 30.0]);
  48. expect($should[0]['constant_score']['filter']['match_phrase']['bold_single'])->toBe('vitakko');
  49. expect($should[1]['constant_score']['filter']['match_phrase']['bold_single'])
  50. ->toBe(['query' => 'vitakko', 'analyzer' => 'standard']);
  51. expect($should[2]['constant_score']['filter']['match_phrase']['bold_multi'])->toBe('vitakko');
  52. // 正文部分整体缩小到 0.1,压不过一个黑体字
  53. $text = end($should);
  54. expect(constantScoreBoosts($text))->toEqual([2.0, 1.0, 0.2, 1.0, 0.2]);
  55. });
  56. it('routes cjk definition text to chinese fields', function () {
  57. $query = buildSearchQuery('buildDefinitionQuery', '寻');
  58. $text = end($query['bool']['should']);
  59. expect($text['multi_match']['fields'])->toBe(['title.text.zh^3', 'content.text.zh']);
  60. });
  61. it('keeps pali stopwords in word-order clauses only', function () {
  62. $query = buildSearchQuery('buildPaliFuzzyQuery', 'evaṃ ca kho');
  63. $should = $query['bool']['should'];
  64. // 整句 20 + 两对相邻词各 5 + 只有 evaṃ、kho 两个词单独计分
  65. expect(constantScoreBoosts($query))->toEqual([20.0, 5.0, 5.0, 10.0, 2.0, 10.0, 2.0]);
  66. expect($should[0]['constant_score']['filter']['multi_match']['query'])->toBe('evaṃ ca kho');
  67. expect(end($should)['match']['content.text.pali']['query'])->toBe('evaṃ kho');
  68. });
  69. it('skips adjacent pairs made only of stopwords', function () {
  70. $query = buildSearchQuery('buildPaliFuzzyQuery', 'na ca kho');
  71. // 「na ca」那对跳过,只剩「ca kho」
  72. expect(constantScoreBoosts($query))->toEqual([20.0, 5.0, 10.0, 2.0]);
  73. });
  74. it('treats an all-stopword query as normal words', function () {
  75. $query = buildSearchQuery('buildPaliFuzzyQuery', 'Eva');
  76. expect(constantScoreBoosts($query))->toEqual([10.0, 2.0]);
  77. });
  78. it('ignores stopwords for definition bold scoring', function () {
  79. $query = buildSearchQuery('buildDefinitionQuery', 'vitakko ca');
  80. // 只有 vitakko 四条 + 整句在多词黑体里 30
  81. expect(constantScoreBoosts($query))->toEqual([100.0, 10.0, 30.0, 5.0, 30.0]);
  82. });