2
0

OpenSearchQueryBuilderTest.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. use App\Facades\RedisCache;
  3. use App\Services\OpenSearchService;
  4. /**
  5. * 调用 OpenSearchService 的 protected 查询构造方法(纯函数,不连 OpenSearch)
  6. */
  7. function buildSearchQuery(string $method, mixed ...$args): array
  8. {
  9. return callSearchService($method, ...$args);
  10. }
  11. /**
  12. * 调用 OpenSearchService 的 protected 方法,返回值不限类型
  13. */
  14. function callSearchService(string $method, mixed ...$args): mixed
  15. {
  16. $service = new OpenSearchService;
  17. return (fn () => $this->{$method}(...$args))->call($service);
  18. }
  19. /**
  20. * 取出 bool.should 里所有 constant_score 的固定分
  21. *
  22. * @return list<float>
  23. */
  24. function constantScoreBoosts(array $query): array
  25. {
  26. return array_values(array_map(
  27. fn (array $clause): float => $clause['constant_score']['boost'],
  28. array_filter($query['bool']['should'], fn (array $clause): bool => isset($clause['constant_score'])),
  29. ));
  30. }
  31. it('scores pali fuzzy per word, rewarding word order and exact spelling', function () {
  32. $query = buildSearchQuery('buildPaliFuzzyQuery', 'evaṃ me sutaṃ');
  33. // 整句 20 + 相邻两对各 5 + 每词拼写 10、同义 2
  34. expect(constantScoreBoosts($query))->toEqual([20.0, 5.0, 5.0, 10.0, 2.0, 10.0, 2.0, 10.0, 2.0]);
  35. $phrase = $query['bool']['should'][0]['constant_score']['filter']['multi_match'];
  36. expect($phrase['type'])->toBe('phrase')
  37. ->and($phrase['query'])->toBe('evaṃ me sutaṃ')
  38. ->and($phrase['fields'])->toBe(['title.text.pali.exact', 'content.text.pali.exact']);
  39. expect($query['bool'])->not->toHaveKey('minimum_should_match');
  40. });
  41. it('drops punctuation-only words so they cannot block matches', function () {
  42. $query = buildSearchQuery('buildPaliFuzzyQuery', 'evaṃ – me .');
  43. $phrase = $query['bool']['should'][0]['constant_score']['filter']['multi_match'];
  44. expect($phrase['query'])->toBe('evaṃ me')
  45. ->and(constantScoreBoosts($query))->toEqual([20.0, 10.0, 2.0, 10.0, 2.0]);
  46. });
  47. it('has no order clauses for a single pali word', function () {
  48. $query = buildSearchQuery('buildPaliFuzzyQuery', 'vitakko');
  49. expect(constantScoreBoosts($query))->toEqual([10.0, 2.0]);
  50. });
  51. it('ranks definition by each matched single bold word, any word form', function () {
  52. $query = buildSearchQuery('buildDefinitionQuery', 'vitakko vicāro');
  53. $should = $query['bool']['should'];
  54. // 每词:单独黑体任一词形 100 + 原拼写 10、多词黑体任一词形 30 + 原拼写 5;再加整句在多词黑体里 30
  55. expect(constantScoreBoosts($query))->toEqual([100.0, 10.0, 30.0, 5.0, 100.0, 10.0, 30.0, 5.0, 30.0]);
  56. expect($should[0]['constant_score']['filter']['match_phrase']['bold_single'])->toBe('vitakko');
  57. expect($should[1]['constant_score']['filter']['match_phrase']['bold_single'])
  58. ->toBe(['query' => 'vitakko', 'analyzer' => 'standard']);
  59. expect($should[2]['constant_score']['filter']['match_phrase']['bold_multi'])->toBe('vitakko');
  60. // 正文部分整体缩小到 0.1,压不过一个黑体字
  61. $text = end($should);
  62. expect(constantScoreBoosts($text))->toEqual([2.0, 1.0, 0.2, 1.0, 0.2]);
  63. });
  64. it('routes cjk definition text to chinese fields', function () {
  65. $query = buildSearchQuery('buildDefinitionQuery', '寻');
  66. $text = end($query['bool']['should']);
  67. expect($text['multi_match']['fields'])->toBe(['title.text.zh^3', 'content.text.zh']);
  68. });
  69. it('keeps pali stopwords in word-order clauses only', function () {
  70. $query = buildSearchQuery('buildPaliFuzzyQuery', 'evaṃ ca kho');
  71. $should = $query['bool']['should'];
  72. // 整句 20 + 两对相邻词各 5 + 只有 evaṃ、kho 两个词单独计分
  73. expect(constantScoreBoosts($query))->toEqual([20.0, 5.0, 5.0, 10.0, 2.0, 10.0, 2.0]);
  74. expect($should[0]['constant_score']['filter']['multi_match']['query'])->toBe('evaṃ ca kho');
  75. expect(end($should)['match']['content.text.pali']['query'])->toBe('evaṃ kho');
  76. });
  77. it('skips adjacent pairs made only of stopwords', function () {
  78. $query = buildSearchQuery('buildPaliFuzzyQuery', 'na ca kho');
  79. // 「na ca」那对跳过,只剩「ca kho」
  80. expect(constantScoreBoosts($query))->toEqual([20.0, 5.0, 10.0, 2.0]);
  81. });
  82. it('treats an all-stopword query as normal words', function () {
  83. $query = buildSearchQuery('buildPaliFuzzyQuery', 'Eva');
  84. expect(constantScoreBoosts($query))->toEqual([10.0, 2.0]);
  85. });
  86. it('ignores stopwords for definition bold scoring', function () {
  87. $query = buildSearchQuery('buildDefinitionQuery', 'vitakko ca');
  88. // 只有 vitakko 四条 + 整句在多词黑体里 30
  89. expect(constantScoreBoosts($query))->toEqual([100.0, 10.0, 30.0, 5.0, 30.0]);
  90. });
  91. /**
  92. * 预置文档总数与各词的文档频率,让 wordRarityWeights 不用连 OpenSearch
  93. *
  94. * @param array<string, int> $frequencies 原始词 => 文档数
  95. */
  96. function seedDocumentFrequencies(int $total, array $frequencies): void
  97. {
  98. $index = config('mint.opensearch.index');
  99. RedisCache::put("/opensearch/{$index}/doc-count", $total);
  100. foreach ($frequencies as $word => $df) {
  101. RedisCache::put(callSearchService('documentFrequencyCacheKey', $index, $word), $df);
  102. }
  103. }
  104. it('weights rarer words higher and floors very common ones', function () {
  105. seedDocumentFrequencies(550000, ['rare' => 10, 'mid' => 100, 'common' => 200000]);
  106. $weights = buildSearchQuery('wordRarityWeights', ['rare', 'mid', 'common']);
  107. expect($weights['rare'])->toBe(1.0)
  108. ->and($weights['mid'])->toBeLessThan(1.0)->toBeGreaterThan(0.2)
  109. ->and($weights['common'])->toBe(0.2);
  110. });
  111. it('does not weight a single word', function () {
  112. expect(buildSearchQuery('wordRarityWeights', ['vitakko']))->toBe(['vitakko' => 1.0]);
  113. });
  114. it('multiplies per-word clauses by rarity but not word-order clauses', function () {
  115. $query = buildSearchQuery('buildPaliFuzzyQuery', 'evaṃ me sutaṃ', 1.0, ['evaṃ' => 0.5, 'me' => 1.0, 'sutaṃ' => 0.25]);
  116. // 整句 20 与相邻词对 5 不乘;拼写 10、同义 2 分别乘 0.5 / 1 / 0.25
  117. expect(constantScoreBoosts($query))->toEqual([20.0, 5.0, 5.0, 5.0, 1.0, 10.0, 2.0, 2.5, 0.5]);
  118. });