Code search with Jina Code v2
The Jina Code v2 model is built for semantic code search across 30+ programming languages. It excels at:
- Finding code snippets from a natural-language description
- Searching for similar code patterns (even across languages)
- Linking documentation to code
- Code-to-code similarity search
The snippets below assume you've already created an AI client (and, in Go, a
ctx). See Client libraries for connection setup.
1. Create the code store
Use the Jina Code model for both the index and query side, so stored code and your searches land in the same space:
CREATESTORE code_repo QUERYMODEL jina-embeddings-v2-base-code INDEXMODEL jina-embeddings-v2-base-code2. Index your code snippets
Store each snippet as raw text with a little metadata (language, file). The proxy embeds it with the index model.
- Python
- Node.js
- Go
- Rust
- CLI
from ahnlich_client_py.grpc.ai import query as ai_query
from ahnlich_client_py.grpc import keyval, metadata
from ahnlich_client_py.grpc.ai import preprocess
await client.set(
ai_query.Set(
store="code_repo",
inputs=[
keyval.AiStoreEntry(
key=keyval.StoreInput(
raw_string="fn fibonacci(n: u32) -> u32 { if n <= 1 { n } else { fibonacci(n-1) + fibonacci(n-2) } }"
),
value=keyval.StoreValue(
value={"language": metadata.MetadataValue(raw_string="rust")}
),
),
],
preprocess_action=preprocess.PreprocessAction.ModelPreprocessing,
)
)
import { Set } from "ahnlich-client-node/grpc/ai/query_pb";
import { AiStoreEntry, StoreInput, StoreValue } from "ahnlich-client-node/grpc/keyval_pb";
import { MetadataValue } from "ahnlich-client-node/grpc/metadata_pb";
import { PreprocessAction } from "ahnlich-client-node/grpc/ai/preprocess_pb";
await client.set(
new Set({
store: "code_repo",
inputs: [
new AiStoreEntry({
key: new StoreInput({
value: { case: "rawString", value: "fn fibonacci(n: u32) -> u32 { if n <= 1 { n } else { fibonacci(n-1) + fibonacci(n-2) } }" },
}),
value: new StoreValue({
value: { language: new MetadataValue({ value: { case: "rawString", value: "rust" } }) },
}),
}),
],
preprocessAction: PreprocessAction.MODEL_PREPROCESSING,
})
);
inputs := []*keyval.AiStoreEntry{
{
Key: &keyval.StoreInput{
Value: &keyval.StoreInput_RawString{RawString: "fn fibonacci(n: u32) -> u32 { if n <= 1 { n } else { fibonacci(n-1) + fibonacci(n-2) } }"},
},
Value: &keyval.StoreValue{Value: map[string]*metadata.MetadataValue{
"language": {Value: &metadata.MetadataValue_RawString{RawString: "rust"}},
}},
},
}
_, err := client.Set(ctx, &aiquery.Set{
Store: "code_repo",
Inputs: inputs,
PreprocessAction: preprocess.PreprocessAction_ModelPreprocessing,
})
use std::collections::HashMap;
use ahnlich_client_rs::prelude::*;
client.set(
"code_repo".to_string(),
vec![StoreInput::RawString(
"fn fibonacci(n: u32) -> u32 { if n <= 1 { n } else { fibonacci(n-1) + fibonacci(n-2) } }".to_string(),
)],
PreprocessAction::ModelPreprocessing,
None,
HashMap::from([("language".to_string(), "rust".to_string())]),
).await?;
SET fib "fn fibonacci(n: u32) -> u32 { if n <= 1 { n } else { fibonacci(n-1) + fibonacci(n-2) } }" WITH {"language": "rust"} IN code_repo3. Search the code store
The query is just raw text — it can be a natural-language description or a code snippet. Same call either way; only the query string changes.
- Natural language:
"implement recursive fibonacci sequence" - Code:
"def fib(n): return n if n <= 1 else fib(n-1) + fib(n-2)"
- Python
- Node.js
- Go
- Rust
- CLI
from ahnlich_client_py.grpc.ai import query as ai_query
from ahnlich_client_py.grpc import keyval
from ahnlich_client_py.grpc.algorithm import algorithms
response = await client.get_sim_n(
ai_query.GetSimN(
store="code_repo",
search_input=keyval.StoreInput(raw_string="implement recursive fibonacci sequence"),
closest_n=5,
algorithm=algorithms.Algorithm.CosineSimilarity,
)
)
import { GetSimN } from "ahnlich-client-node/grpc/ai/query_pb";
import { StoreInput } from "ahnlich-client-node/grpc/keyval_pb";
import { Algorithm } from "ahnlich-client-node/grpc/algorithm/algorithm_pb";
const response = await client.getSimN(
new GetSimN({
store: "code_repo",
searchInput: new StoreInput({
value: { case: "rawString", value: "implement recursive fibonacci sequence" },
}),
closestN: 5,
algorithm: Algorithm.COSINE_SIMILARITY,
})
);
resp, err := client.GetSimN(ctx, &aiquery.GetSimN{
Store: "code_repo",
SearchInput: &keyval.StoreInput{
Value: &keyval.StoreInput_RawString{RawString: "implement recursive fibonacci sequence"},
},
ClosestN: 5,
Algorithm: algorithms.Algorithm_CosineSimilarity,
})
let results = client.get_sim_n(
"code_repo".to_string(),
vec![StoreInput::RawString("implement recursive fibonacci sequence".to_string())],
Condition::new(NonLinearAlgorithm::CosineSimilarity),
5, // top 5 results
PreprocessAction::ModelPreprocessing,
None,
HashMap::new(),
).await?;
GETSIMN 5 WITH ["implement recursive fibonacci sequence"] USING cosinesimilarity IN code_repoThe natural-language query above still surfaces the Rust fibonacci function — semantic code search matches meaning, not exact tokens, so it works across languages.
Use cases
- Documentation search — index code examples, search them with plain questions.
- Code discovery — find similar implementations across different languages.
- Refactoring detection — spot duplicate or near-duplicate patterns.
- Code review assistance — pull up related snippets for context.
- IDE integration — power semantic code search in developer tools.