Skip to content

Commit

Permalink
Merge pull request #2 from Trendyol/feature/query-string
Browse files Browse the repository at this point in the history
feature: Add query string
  • Loading branch information
bayraktugrul authored Sep 5, 2024
2 parents c44d1fc + b61b95d commit ee10b9e
Show file tree
Hide file tree
Showing 9 changed files with 956 additions and 8 deletions.
File renamed without changes.
File renamed without changes.
566 changes: 566 additions & 0 deletions es/query_string.go

Large diffs are not rendered by default.

325 changes: 325 additions & 0 deletions es/query_string_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,325 @@
package es_test

import (
"testing"

"github.com/Trendyol/es-query-builder/es"
"github.com/Trendyol/es-query-builder/test/assert"
)

func Test_QueryString_should_exist_on_es_package(t *testing.T) {
// Given When
assert.NotNil(t, es.QueryString[any])
}

func Test_QueryString_method_should_create_queryStringType(t *testing.T) {
// Given When
b := es.QueryString("value")

// Then
assert.NotNil(t, b)
assert.IsTypeString(t, "es.queryStringType", b)
}

func Test_QueryString_method_should_create_query_string_with_required_query(t *testing.T) {
// Given When
b := es.NewQuery(
es.QueryString("value"),
)

// Then
assert.NotNil(t, b)
bodyJSON := assert.MarshalWithoutError(t, b)
assert.Equal(t, "{\"query\":{\"query_string\":{\"query\":\"value\"}}}", bodyJSON)
}

func Test_QueryString_method_should_create_query_string_with_default_field(t *testing.T) {
// Given When
b := es.NewQuery(
es.QueryString("value").DefaultField("defaultField"),
)

// Then
assert.NotNil(t, b)
bodyJSON := assert.MarshalWithoutError(t, b)
assert.Equal(t, "{\"query\":{\"query_string\":{\"default_field\":\"defaultField\",\"query\":\"value\"}}}", bodyJSON)
}

func Test_QueryString_method_should_create_query_string_with_allow_leading_wildcard(t *testing.T) {
// Given When
b := es.NewQuery(
es.QueryString("value").AllowLeadingWildcard(false),
)

// Then
assert.NotNil(t, b)
bodyJSON := assert.MarshalWithoutError(t, b)
assert.Equal(t, "{\"query\":{\"query_string\":{\"allow_leading_wildcard\":false,\"query\":\"value\"}}}", bodyJSON)
}

func Test_QueryString_method_should_create_query_string_with_analyze_wildcard(t *testing.T) {
// Given When
b := es.NewQuery(
es.QueryString("value").AnalyzeWildcard(true),
)

// Then
assert.NotNil(t, b)
bodyJSON := assert.MarshalWithoutError(t, b)
assert.Equal(t, "{\"query\":{\"query_string\":{\"analyze_wildcard\":true,\"query\":\"value\"}}}", bodyJSON)
}

func Test_QueryString_method_should_create_query_string_with_analyzer(t *testing.T) {
// Given When
b := es.NewQuery(
es.QueryString("value").Analyzer("value"),
)

// Then
assert.NotNil(t, b)
bodyJSON := assert.MarshalWithoutError(t, b)
assert.Equal(t, "{\"query\":{\"query_string\":{\"analyzer\":\"value\",\"query\":\"value\"}}}", bodyJSON)
}

func Test_QueryString_method_should_create_query_string_with_auto_generate_synonyms_phrase_query(t *testing.T) {
// Given When
b := es.NewQuery(
es.QueryString("value").AutoGenerateSynonymsPhraseQuery(false),
)

// Then
assert.NotNil(t, b)
bodyJSON := assert.MarshalWithoutError(t, b)
assert.Equal(t, "{\"query\":{\"query_string\":{\"auto_generate_synonyms_phrase_query\":false,\"query\":\"value\"}}}", bodyJSON)
}

func Test_QueryString_method_should_create_query_string_with_boost(t *testing.T) {
// Given When
b := es.NewQuery(
es.QueryString("value").Boost(2.5),
)

// Then
assert.NotNil(t, b)
bodyJSON := assert.MarshalWithoutError(t, b)
assert.Equal(t, "{\"query\":{\"query_string\":{\"boost\":2.5,\"query\":\"value\"}}}", bodyJSON)
}

func Test_QueryString_method_should_create_query_string_with_default_operator(t *testing.T) {
// Given When
b := es.NewQuery(
es.QueryString("value").DefaultOperator("value"),
)

// Then
assert.NotNil(t, b)
bodyJSON := assert.MarshalWithoutError(t, b)
assert.Equal(t, "{\"query\":{\"query_string\":{\"default_operator\":\"value\",\"query\":\"value\"}}}", bodyJSON)
}

func Test_QueryString_method_should_create_query_string_with_enable_position_increments(t *testing.T) {
// Given When
b := es.NewQuery(
es.QueryString("value").EnablePositionIncrements(false),
)

// Then
assert.NotNil(t, b)
bodyJSON := assert.MarshalWithoutError(t, b)
assert.Equal(t, "{\"query\":{\"query_string\":{\"enable_position_increments\":false,\"query\":\"value\"}}}", bodyJSON)
}

func Test_QueryString_method_should_create_query_string_with_fields(t *testing.T) {
// Given When
b := es.NewQuery(
es.QueryString("value").Fields([]string{"field1", "field2"}),
)

// Then
assert.NotNil(t, b)
bodyJSON := assert.MarshalWithoutError(t, b)
assert.Equal(t, "{\"query\":{\"query_string\":{\"fields\":[\"field1\",\"field2\"],\"query\":\"value\"}}}", bodyJSON)
}

func Test_QueryString_method_should_create_query_string_with_fuzziness(t *testing.T) {
// Given When
b := es.NewQuery(
es.QueryString("value").Fuzziness("value"),
)

// Then
assert.NotNil(t, b)
bodyJSON := assert.MarshalWithoutError(t, b)
assert.Equal(t, "{\"query\":{\"query_string\":{\"fuzziness\":\"value\",\"query\":\"value\"}}}", bodyJSON)
}

func Test_QueryString_method_should_create_query_string_with_fuzzy_max_expansions(t *testing.T) {
// Given When
b := es.NewQuery(
es.QueryString("value").FuzzyMaxExpansions(50),
)

// Then
assert.NotNil(t, b)
bodyJSON := assert.MarshalWithoutError(t, b)
assert.Equal(t, "{\"query\":{\"query_string\":{\"fuzzy_max_expansions\":50,\"query\":\"value\"}}}", bodyJSON)
}

func Test_QueryString_method_should_create_query_string_with_fuzzy_prefix_length(t *testing.T) {
// Given When
b := es.NewQuery(
es.QueryString("value").FuzzyPrefixLength(1),
)

// Then
assert.NotNil(t, b)
bodyJSON := assert.MarshalWithoutError(t, b)
assert.Equal(t, "{\"query\":{\"query_string\":{\"fuzzy_prefix_length\":1,\"query\":\"value\"}}}", bodyJSON)
}

func Test_QueryString_method_should_create_query_string_with_fuzzy_transpositions(t *testing.T) {
// Given When
b := es.NewQuery(
es.QueryString("value").FuzzyTranspositions(false),
)

// Then
assert.NotNil(t, b)
bodyJSON := assert.MarshalWithoutError(t, b)
assert.Equal(t, "{\"query\":{\"query_string\":{\"fuzzy_transpositions\":false,\"query\":\"value\"}}}", bodyJSON)
}

func Test_QueryString_method_should_create_query_string_with_lenient(t *testing.T) {
// Given When
b := es.NewQuery(
es.QueryString("value").Lenient(true),
)

// Then
assert.NotNil(t, b)
bodyJSON := assert.MarshalWithoutError(t, b)
assert.Equal(t, "{\"query\":{\"query_string\":{\"lenient\":true,\"query\":\"value\"}}}", bodyJSON)
}

func Test_QueryString_method_should_create_query_string_with_max_determinized_states(t *testing.T) {
// Given When
b := es.NewQuery(
es.QueryString("value").MaxDeterminizedStates(5000),
)

// Then
assert.NotNil(t, b)
bodyJSON := assert.MarshalWithoutError(t, b)
assert.Equal(t, "{\"query\":{\"query_string\":{\"max_determinized_states\":5000,\"query\":\"value\"}}}", bodyJSON)
}

func Test_QueryString_method_should_create_query_string_with_minimum_should_match(t *testing.T) {
// Given When
b := es.NewQuery(
es.QueryString("value").MinimumShouldMatch("1"),
)

// Then
assert.NotNil(t, b)
bodyJSON := assert.MarshalWithoutError(t, b)
assert.Equal(t, "{\"query\":{\"query_string\":{\"minimum_should_match\":\"1\",\"query\":\"value\"}}}", bodyJSON)
}

func Test_QueryString_method_should_create_query_string_with_quote_analyzer(t *testing.T) {
// Given When
b := es.NewQuery(
es.QueryString("value").QuoteAnalyzer("value"),
)

// Then
assert.NotNil(t, b)
bodyJSON := assert.MarshalWithoutError(t, b)
assert.Equal(t, "{\"query\":{\"query_string\":{\"query\":\"value\",\"quote_analyzer\":\"value\"}}}", bodyJSON)
}

func Test_QueryString_method_should_create_query_string_with_phrase_slop(t *testing.T) {
// Given When
b := es.NewQuery(
es.QueryString("value").PhraseSlop(0),
)

// Then
assert.NotNil(t, b)
bodyJSON := assert.MarshalWithoutError(t, b)
assert.Equal(t, "{\"query\":{\"query_string\":{\"phrase_slop\":0,\"query\":\"value\"}}}", bodyJSON)
}

func Test_QueryString_method_should_create_query_string_with_quote_field_suffix(t *testing.T) {
// Given When
b := es.NewQuery(
es.QueryString("value").QuoteFieldSuffix("value"),
)

// Then
assert.NotNil(t, b)
bodyJSON := assert.MarshalWithoutError(t, b)
assert.Equal(t, "{\"query\":{\"query_string\":{\"query\":\"value\",\"quote_field_suffix\":\"value\"}}}", bodyJSON)
}

func Test_QueryString_method_should_create_query_string_with_rewrite(t *testing.T) {
// Given When
b := es.NewQuery(
es.QueryString("value").Rewrite("value"),
)

// Then
assert.NotNil(t, b)
bodyJSON := assert.MarshalWithoutError(t, b)
assert.Equal(t, "{\"query\":{\"query_string\":{\"query\":\"value\",\"rewrite\":\"value\"}}}", bodyJSON)
}

func Test_QueryString_method_should_create_query_string_with_time_zone(t *testing.T) {
// Given When
b := es.NewQuery(
es.QueryString("value").TimeZone("value"),
)

// Then
assert.NotNil(t, b)
bodyJSON := assert.MarshalWithoutError(t, b)
assert.Equal(t, "{\"query\":{\"query_string\":{\"query\":\"value\",\"time_zone\":\"value\"}}}", bodyJSON)
}

func Test_QueryString_method_should_create_query_string_with_all_parameters(t *testing.T) {
// Given When
b := es.NewQuery(
es.QueryString("value").
DefaultField("value").
AllowLeadingWildcard(false).
AnalyzeWildcard(true).
Analyzer("value").
AutoGenerateSynonymsPhraseQuery(true).
Boost(2.5).
DefaultOperator("value").
EnablePositionIncrements(true).
Fields([]string{"field1", "field2"}).
Fuzziness("value").
FuzzyMaxExpansions(2).
FuzzyPrefixLength(1.0).
FuzzyTranspositions(true).
Lenient(true).
MaxDeterminizedStates(2).MinimumShouldMatch("value").
QuoteAnalyzer("value").
PhraseSlop(2).
QuoteFieldSuffix("value").
Rewrite("value").
TimeZone("value"),
)

// Then
assert.NotNil(t, b)
bodyJSON := assert.MarshalWithoutError(t, b)
assert.Equal(t, "{\"query\":{\"query_string\":{\"allow_leading_wildcard\":false,\"analyze_wildcard\":true,"+
"\"analyzer\":\"value\",\"auto_generate_synonyms_phrase_query\":true,"+
"\"boost\":2.5,\"default_field\":\"value\",\"default_operator\":\"value\","+
"\"enable_position_increments\":true,\"fields\":[\"field1\",\"field2\"],"+
"\"fuzziness\":\"value\",\"fuzzy_max_expansions\":2,\"fuzzy_prefix_length\":1,"+
"\"fuzzy_transpositions\":true,\"lenient\":true,\"max_determinized_states\":2,"+
"\"minimum_should_match\":\"value\",\"phrase_slop\":2,\"query\":\"value\",\"quote_analyzer\":\"value\","+
"\"quote_field_suffix\":\"value\",\"rewrite\":\"value\",\"time_zone\":\"value\"}}}", bodyJSON)
}
5 changes: 3 additions & 2 deletions internal/testing/constants/constants.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package constants

const (
Zero = 0
True = "true"
Zero = 0
True = "true"
StatusOK = 200
)
10 changes: 10 additions & 0 deletions internal/testing/elasticsearch_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type ElasticsearchRepository interface {
Insert(indexName, docId, document string) error
Delete(indexName, docId string) error
DeleteByQuery(indexName, query string) error
Exists(indexName, docId string) bool
}

func NewElasticsearchRepository(client *elasticsearch.Client) ElasticsearchRepository {
Expand Down Expand Up @@ -106,3 +107,12 @@ func (e *elasticsearchRepository) DeleteByQuery(indexName, query string) error {
}
return err
}

func (e *elasticsearchRepository) Exists(indexName, docId string) bool {
request := esapi.ExistsRequest{
Index: indexName,
DocumentID: docId,
}
res, _ := request.Do(context.Background(), e.client)
return res.StatusCode == constants.StatusOK
}
1 change: 1 addition & 0 deletions internal/testing/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ replace github.com/Trendyol/es-query-builder => ./../..

require (
github.com/Trendyol/es-query-builder v0.2.0
github.com/bayraktugrul/go-await v1.1.1
github.com/docker/go-connections v0.5.0
github.com/elastic/elastic-transport-go/v8 v8.6.0
github.com/elastic/go-elasticsearch/v8 v8.15.0
Expand Down
2 changes: 2 additions & 0 deletions internal/testing/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 h1:UQHMgLO+TxOEl
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1/go.mod h1:xomTg63KZ2rFqZQzSB4Vz2SUXa1BpHTVz9L5PTmPC4E=
github.com/Microsoft/go-winio v0.6.2 h1:F2VQgta7ecxGYO8k3ZZz3RS8fVIXVxONVUPlNERoyfY=
github.com/Microsoft/go-winio v0.6.2/go.mod h1:yd8OoFMLzJbo9gZq8j5qaps8bJ9aShtEA8Ipt1oGCvU=
github.com/bayraktugrul/go-await v1.1.1 h1:ecNe3NhOoPQ2jEomimms++385uvFwFoFpO3gGapURbs=
github.com/bayraktugrul/go-await v1.1.1/go.mod h1:+YPkaRay7p91FGPcL/JVzYuPnvaE8ABhc/etCt9UYTw=
github.com/cenkalti/backoff/v4 v4.2.1 h1:y4OZtCnogmCPw98Zjyt5a6+QwPLGkiQsYW5oUqylYbM=
github.com/cenkalti/backoff/v4 v4.2.1/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE=
github.com/containerd/containerd v1.7.18 h1:jqjZTQNfXGoEaZdW1WwPU0RqSn1Bm2Ay/KJPUuO8nao=
Expand Down
Loading

0 comments on commit ee10b9e

Please sign in to comment.