Skip to content

Commit

Permalink
implement phrase operator
Browse files Browse the repository at this point in the history
  • Loading branch information
yukikaoru committed Jan 1, 2020
1 parent e35e64d commit d1addb8
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
17 changes: 17 additions & 0 deletions csrestructuredquery/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,20 @@ def query(self) -> str:
if self.boost:
q += f" boost={self.boost}"
return f"{q} {ExpressionValue(self.value)})"


@dataclasses.dataclass(frozen=True)
class Phrase(SpecializedOperator):
field: str
value: CsValue
boost: int = dataclasses.field(default=0)

@property
def name(self) -> str:
return "phrase"

def query(self) -> str:
q = f"({self.name} field={self.field}"
if self.boost:
q += f" boost={self.boost}"
return f"{q} {ExpressionValue(self.value)})"
9 changes: 8 additions & 1 deletion tests/test_query.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from datetime import datetime

from csrestructuredquery.query import Query, And, Or, Not, Near
from csrestructuredquery.query import Query, And, Or, Not, Near, Phrase


def test_文字列型と日時型は引用符で括られる():
Expand Down Expand Up @@ -33,3 +33,10 @@ def test_near演算子():
assert operator.query() == "(near field=foo 'hoge')"
operator = Near(field="foo", value="hoge", distance=2, boost=4)
assert operator.query() == "(near field=foo distance=2 boost=4 'hoge')"


def test_phrase演算子():
operator = Phrase(field="foo", value="hoge")
assert operator.query() == "(phrase field=foo 'hoge')"
operator = Phrase(field="foo", value="hoge", boost=4)
assert operator.query() == "(phrase field=foo boost=4 'hoge')"

0 comments on commit d1addb8

Please sign in to comment.