Skip to content

Update filter query to support camel case for certain fields#14

Merged
bklaing2 merged 2 commits intomainfrom
support-camel-case-queries
Feb 12, 2026
Merged

Update filter query to support camel case for certain fields#14
bklaing2 merged 2 commits intomainfrom
support-camel-case-queries

Conversation

@bklaing2
Copy link
Copy Markdown
Member

@bklaing2 bklaing2 commented Feb 12, 2026

Purpose

closes: #10

Approach

Implements lupo's camel-case to snake-case replacement in Go.

Open Questions and Pre-Merge TODOs

Learning

Types of changes

  • Bug fix (non-breaking change which fixes an issue)

  • New feature (non-breaking change which adds functionality)

  • Breaking change (fix or feature that would cause existing functionality to change)

Reviewer, please remember our guidelines:

  • Be humble in the language and feedback you give, ask don't tell.
  • Consider using positive language as opposed to neutral when offering feedback. This is to avoid the negative bias that can occur with neutral language appearing negative.
  • Offer suggestions on how to improve code e.g. simplification or expanding clarity.
  • Ensure you give reasons for the changes you are proposing.

Summary by CodeRabbit

  • Bug Fixes
    • Improved search query field-name handling: queries now apply direct and pattern-based field-name rewrites (e.g., camelCase → snake_case) before execution, improving consistency and accuracy of query processing and field mapping.

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Feb 12, 2026

📝 Walkthrough

Walkthrough

Adds package-level camelCase→snake_case field translation maps and applies direct and regex-based rewrites to input query strings inside buildQueryString, using imported regexp and strings packages before building the final query_string payload.

Changes

Cohort / File(s) Summary
OpenSearch Query Field Translation
opensearch.go
Added camelCaseToSnakeCaseFields (string→string) and camelCaseToSnakeCaseFieldsRegex (string→string) package-level mappings. Updated buildQueryString to perform string replacements then regex-based rewrites on the input query before constructing the query_string. Imported regexp and strings to support this logic.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

🚥 Pre-merge checks | ✅ 4 | ❌ 1
❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: adding support for camelCase field names in filter queries by implementing camelCase to snake_case conversion.
Linked Issues check ✅ Passed The changes implement camelCase to snake_case field name conversion in buildQueryString, directly addressing issue #10's requirement to support camelCase queries like 'fundingReferences:*'.
Out of Scope Changes check ✅ Passed All changes are scoped to supporting camelCase field conversion in opensearch.go with no unrelated modifications introduced.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch support-camel-case-queries

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Fix all issues with AI agents
In `@opensearch.go`:
- Line 34: The global replacement mapping currently includes the entry `"/" →
"\\/"`, which over-broadly escapes all forward slashes and can corrupt values
like DOIs or URLs; remove that entry from the camelCase→snake_case transform map
and instead implement a separate, well-documented transformation (e.g. an
`escapeSlashesInQuery` step) that is applied only where needed; scope this new
step narrowly — call it from the query-building code that injects literal query
text and limit it to locations outside quoted terms or to specific value
positions (or provide an option/flag) so values (like `doi:10.1234/foo`) are
preserved; add a clear comment explaining why and when slashes are escaped.
🧹 Nitpick comments (1)
opensearch.go (1)

86-92: Go map iteration order is non-deterministic — currently safe but fragile.

Go randomizes map iteration order. Right now the replacement keys don't overlap, so this is fine. But if future entries introduce overlapping keys or regexes (e.g., a key that is a substring of another key's replacement), results could become non-deterministic. A slice of ordered pairs would be more robust.

Comment thread opensearch.go
Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Fix all issues with AI agents
In `@opensearch.go`:
- Around line 86-88: The current loop over camelCaseToSnakeCaseFields is
nondeterministic because Go maps iterate in random order, so perform
replacements in a deterministic sequence: replace the map with an ordered slice
of pairs (e.g., []struct{from,to string}) and iterate that slice when updating
query, or at minimum remove the "/"→"\\/" entry from camelCaseToSnakeCaseFields
and apply the slash-escaping as a separate deterministic step (e.g., call
strings.ReplaceAll(query, "/", "\\/") before or after the ordered field-name
replacements). Update references to camelCaseToSnakeCaseFields and the
replacement loop accordingly so replacements of field names and the "/" escaping
happen in a fixed, predictable order.
🧹 Nitpick comments (1)
opensearch.go (1)

90-92: Pre-compile regexps at package level instead of on every call.

regexp.MustCompile is called inside the loop on every invocation of buildQueryString. Compile once at init time to avoid redundant work per request.

♻️ Proposed fix

Replace the string-keyed regex map with pre-compiled regexps:

-var camelCaseToSnakeCaseFieldsRegex = map[string]string{
-	`(publisher\.)(name|publisherIdentifier|publisherIdentifierScheme|schemeUri|lang)`: "publisher_obj.$2",
-}
+var camelCaseToSnakeCaseFieldsRegex = map[*regexp.Regexp]string{
+	regexp.MustCompile(`(publisher\.)(name|publisherIdentifier|publisherIdentifierScheme|schemeUri|lang)`): "publisher_obj.$2",
+}

Then in buildQueryString:

 	for regex, field := range camelCaseToSnakeCaseFieldsRegex {
-		query = regexp.MustCompile(regex).ReplaceAllString(query, field)
+		query = regex.ReplaceAllString(query, field)
 	}

Comment thread opensearch.go
@bklaing2 bklaing2 merged commit c956de5 into main Feb 12, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Some open search queries not working

1 participant