Skip to content

Commit

Permalink
Merge branch 'master' into expose-regex-query
Browse files Browse the repository at this point in the history
  • Loading branch information
ct-alex-au committed Apr 23, 2024
2 parents 1cb2a56 + 41f72b2 commit 1d88abf
Show file tree
Hide file tree
Showing 7 changed files with 235 additions and 105 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ jobs:

# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@df5a14dc28094dc936e103b37d749c6628682b60 # v2.2.5
uses: github/codeql-action/init@8f596b4ae3cb3c588a5c46780b86dd53fef16c52 # v2.2.5
with:
languages: ${{ matrix.language }}
# If you wish to specify custom queries, you can do so here or in a config file.
Expand All @@ -60,7 +60,7 @@ jobs:
# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
# If this step fails, then you should remove it and run the build manually (see below)
- name: Autobuild
uses: github/codeql-action/autobuild@df5a14dc28094dc936e103b37d749c6628682b60 # v2.2.5
uses: github/codeql-action/autobuild@8f596b4ae3cb3c588a5c46780b86dd53fef16c52 # v2.2.5

# ℹ️ Command-line programs to run using the OS shell.
# 📚 See https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsrun
Expand All @@ -73,6 +73,6 @@ jobs:
# ./location_of_script_within_repo/buildscript.sh

- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@df5a14dc28094dc936e103b37d749c6628682b60 # v2.2.5
uses: github/codeql-action/analyze@8f596b4ae3cb3c588a5c46780b86dd53fef16c52 # v2.2.5
with:
category: "/language:${{matrix.language}}"
10 changes: 5 additions & 5 deletions .github/workflows/publish.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ jobs:
args: --release --sdist -o dist -i 3.8 3.9 3.10 3.11 3.12

- name: Upload wheels
uses: actions/upload-artifact@5d5d22a31266ced268874388b861e4b58bb5c2f3 # 4.3.1
uses: actions/upload-artifact@65462800fd760344b1a7b4382951275a0abb4808 # 4.3.3
with:
name: wheels
path: dist
Expand Down Expand Up @@ -84,7 +84,7 @@ jobs:
args: --release -o dist

- name: Upload wheels
uses: actions/upload-artifact@5d5d22a31266ced268874388b861e4b58bb5c2f3 # 4.3.1
uses: actions/upload-artifact@65462800fd760344b1a7b4382951275a0abb4808 # 4.3.3
with:
name: wheels
path: dist
Expand Down Expand Up @@ -116,7 +116,7 @@ jobs:
args: --release -o dist

- name: Upload wheels
uses: actions/upload-artifact@5d5d22a31266ced268874388b861e4b58bb5c2f3 # 4.3.1
uses: actions/upload-artifact@65462800fd760344b1a7b4382951275a0abb4808 # 4.3.3
with:
name: wheels
path: dist
Expand Down Expand Up @@ -151,7 +151,7 @@ jobs:
with:
python-version: 3.8

- uses: actions/download-artifact@c850b930e6ba138125429b7e5c93fc707a7f8427 # v4.1.4
- uses: actions/download-artifact@9c19ed7fe5d278cd354c7dfd5d3b88589c7e2395 # v4.1.6
with:
name: wheels
path: wheels
Expand All @@ -175,7 +175,7 @@ jobs:
with:
egress-policy: audit # TODO: change to 'egress-policy: block' after couple of runs

- uses: actions/download-artifact@c850b930e6ba138125429b7e5c93fc707a7f8427 # v4.1.4
- uses: actions/download-artifact@9c19ed7fe5d278cd354c7dfd5d3b88589c7e2395 # v4.1.6
with:
name: wheels
path: wheels
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/scorecards.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,14 @@ jobs:
# Upload the results as artifacts (optional). Commenting out will disable uploads of run results in SARIF
# format to the repository Actions tab.
- name: "Upload artifact"
uses: actions/upload-artifact@5d5d22a31266ced268874388b861e4b58bb5c2f3 # v4.3.1
uses: actions/upload-artifact@65462800fd760344b1a7b4382951275a0abb4808 # v4.3.3
with:
name: SARIF file
path: results.sarif
retention-days: 5

# Upload the results to GitHub's code scanning dashboard.
- name: "Upload to code-scanning"
uses: github/codeql-action/upload-sarif@df5a14dc28094dc936e103b37d749c6628682b60 # v3.25.0
uses: github/codeql-action/upload-sarif@8f596b4ae3cb3c588a5c46780b86dd53fef16c52 # v3.25.2
with:
sarif_file: results.sarif
3 changes: 2 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ mod snippet;
use document::Document;
use facet::Facet;
use index::Index;
use query::Query;
use query::{Occur, Query};
use schema::Schema;
use schemabuilder::SchemaBuilder;
use searcher::{DocAddress, Order, SearchResult, Searcher};
Expand Down Expand Up @@ -87,6 +87,7 @@ fn tantivy(_py: Python, m: &PyModule) -> PyResult<()> {
m.add_class::<Query>()?;
m.add_class::<Snippet>()?;
m.add_class::<SnippetGenerator>()?;
m.add_class::<Occur>()?;

m.add_wrapped(wrap_pymodule!(query_parser_error))?;

Expand Down
62 changes: 61 additions & 1 deletion src/query.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,56 @@
use crate::{get_field, make_term, to_pyerr, Schema};
use pyo3::{exceptions, prelude::*, types::PyAny, types::PyString};
use pyo3::{
exceptions, prelude::*, types::PyAny, types::PyString, types::PyTuple,
};
use tantivy as tv;

/// Custom Tuple struct to represent a pair of Occur and Query
/// for the BooleanQuery
struct OccurQueryPair(Occur, Query);

impl<'source> FromPyObject<'source> for OccurQueryPair {
fn extract(ob: &'source PyAny) -> PyResult<Self> {
let tuple = ob.downcast::<PyTuple>()?;
let occur = tuple.get_item(0)?.extract()?;
let query = tuple.get_item(1)?.extract()?;

Ok(OccurQueryPair(occur, query))
}
}

/// Tantivy's Occur
#[pyclass(frozen, module = "tantivy.tantivy")]
#[derive(Clone)]
pub enum Occur {
Must,
Should,
MustNot,
}

impl From<Occur> for tv::query::Occur {
fn from(occur: Occur) -> tv::query::Occur {
match occur {
Occur::Must => tv::query::Occur::Must,
Occur::Should => tv::query::Occur::Should,
Occur::MustNot => tv::query::Occur::MustNot,
}
}
}

/// Tantivy's Query
#[pyclass(frozen, module = "tantivy.tantivy")]
pub(crate) struct Query {
pub(crate) inner: Box<dyn tv::query::Query>,
}

impl Clone for Query {
fn clone(&self) -> Self {
Query {
inner: self.inner.box_clone(),
}
}
}

impl Query {
pub(crate) fn get(&self) -> &dyn tv::query::Query {
&self.inner
Expand Down Expand Up @@ -110,4 +153,21 @@ impl Query {
Err(e) => Err(to_pyerr(e)),
}
}

#[staticmethod]
#[pyo3(signature = (subqueries))]
pub(crate) fn boolean_query(
subqueries: Vec<(Occur, Query)>,
) -> PyResult<Query> {
let dyn_subqueries = subqueries
.into_iter()
.map(|(occur, query)| (occur.into(), query.inner.box_clone()))
.collect::<Vec<_>>();

let inner = tv::query::BooleanQuery::from(dyn_subqueries);

Ok(Query {
inner: Box::new(inner),
})
}
}
Loading

0 comments on commit 1d88abf

Please sign in to comment.