Skip to content

Commit

Permalink
REST: Unify query_string parameters parsing
Browse files Browse the repository at this point in the history
There currently are small differences between search api and count, exists, validate query, explain api when it comes to reading query_string parameters.  `analyze_wildcard`, `lowercase_expanded_terms` and `lenient` are only read by the search api and ignored by all other mentioned apis. Unified code to fix this and make sure it doesn't happen again. Also shared some code when it comes to printing out the query as part of SearchSourceBuilder conversion to ToXContent.

Extended REST spec to include all the supported params (some that were already supported weren't listed), and added REST tests (also some basic tests for count and search_exists which weren't tested at all).

Closes elastic#11057
  • Loading branch information
javanna committed May 11, 2015
1 parent 252aa35 commit c9128a1
Show file tree
Hide file tree
Showing 14 changed files with 604 additions and 45 deletions.
30 changes: 30 additions & 0 deletions rest-api-spec/api/count.json
Expand Up @@ -41,6 +41,36 @@
"routing": {
"type" : "string",
"description" : "Specific routing value"
},
"q": {
"type" : "string",
"description" : "Query in the Lucene query string syntax"
},
"analyzer": {
"type" : "string",
"description" : "The analyzer to use for the query string"
},
"analyze_wildcard": {
"type" : "boolean",
"description" : "Specify whether wildcard and prefix queries should be analyzed (default: false)"
},
"default_operator": {
"type" : "enum",
"options" : ["AND","OR"],
"default" : "OR",
"description" : "The default operator for query string query (AND or OR)"
},
"df": {
"type" : "string",
"description" : "The field to use as default where no field prefix is given in the query string"
},
"lenient": {
"type" : "boolean",
"description" : "Specify whether format-based query failures (such as providing text to a numeric field) should be ignored"
},
"lowercase_expanded_terms": {
"type" : "boolean",
"description" : "Specify whether query terms should be lowercased"
}
}
},
Expand Down
26 changes: 26 additions & 0 deletions rest-api-spec/api/indices.validate_query.json
Expand Up @@ -40,6 +40,32 @@
"q": {
"type" : "string",
"description" : "Query in the Lucene query string syntax"
},
"analyzer": {
"type" : "string",
"description" : "The analyzer to use for the query string"
},
"analyze_wildcard": {
"type" : "boolean",
"description" : "Specify whether wildcard and prefix queries should be analyzed (default: false)"
},
"default_operator": {
"type" : "enum",
"options" : ["AND","OR"],
"default" : "OR",
"description" : "The default operator for query string query (AND or OR)"
},
"df": {
"type" : "string",
"description" : "The field to use as default where no field prefix is given in the query string"
},
"lenient": {
"type" : "boolean",
"description" : "Specify whether format-based query failures (such as providing text to a numeric field) should be ignored"
},
"lowercase_expanded_terms": {
"type" : "boolean",
"description" : "Specify whether query terms should be lowercased"
}
}
},
Expand Down
30 changes: 30 additions & 0 deletions rest-api-spec/api/search_exists.json
Expand Up @@ -41,6 +41,36 @@
"routing": {
"type" : "string",
"description" : "Specific routing value"
},
"q": {
"type" : "string",
"description" : "Query in the Lucene query string syntax"
},
"analyzer": {
"type" : "string",
"description" : "The analyzer to use for the query string"
},
"analyze_wildcard": {
"type" : "boolean",
"description" : "Specify whether wildcard and prefix queries should be analyzed (default: false)"
},
"default_operator": {
"type" : "enum",
"options" : ["AND","OR"],
"default" : "OR",
"description" : "The default operator for query string query (AND or OR)"
},
"df": {
"type" : "string",
"description" : "The field to use as default where no field prefix is given in the query string"
},
"lenient": {
"type" : "boolean",
"description" : "Specify whether format-based query failures (such as providing text to a numeric field) should be ignored"
},
"lowercase_expanded_terms": {
"type" : "boolean",
"description" : "Specify whether query terms should be lowercased"
}
}
},
Expand Down
37 changes: 37 additions & 0 deletions rest-api-spec/test/count/10_basic.yaml
@@ -0,0 +1,37 @@
---
"count with body":
- do:
indices.create:
index: test
- do:
index:
index: test
type: test
id: 1
body: { foo: bar }

- do:
indices.refresh:
index: [test]

- do:
count:
index: test
type: test
body:
query:
match:
foo: bar

- match: {count : 1}

- do:
count:
index: test
type: test
body:
query:
match:
foo: test

- match: {count : 0}
79 changes: 79 additions & 0 deletions rest-api-spec/test/count/20_query_string.yaml
@@ -0,0 +1,79 @@
---
"count with query_string parameters":
- do:
indices.create:
index: test
body:
mappings:
test:
_all:
enabled: false
properties:
number:
type: integer

- do:
index:
index: test
type: test
id: 1
body: { field: foo bar}

- do:
indices.refresh:
index: [test]

- do:
count:
index: test
q: bar
df: field

- match: {count : 1}

- do:
count:
index: test
q: field:foo field:xyz

- match: {count : 1}

- do:
count:
index: test
q: field:foo field:xyz
default_operator: AND

- match: {count : 0}

- do:
count:
index: test
q: field:bars
analyzer: snowball

- match: {count : 1}

- do:
count:
index: test
q: field:BA*
lowercase_expanded_terms: false

- match: {count : 0}

- do:
count:
index: test
q: field:BA*
analyze_wildcard: true

- match: {count : 1}

- do:
count:
index: test
q: number:foo
lenient: true

- match: {count : 0}
93 changes: 93 additions & 0 deletions rest-api-spec/test/explain/30_query_string.yaml
@@ -0,0 +1,93 @@
---
"explain with query_string parameters":
- do:
indices.create:
index: test
body:
mappings:
test:
_all:
enabled: false
properties:
number:
type: integer

- do:
index:
index: test
type: test
id: 1
body: { field: foo bar}

- do:
indices.refresh:
index: [test]

- do:
explain:
index: test
type: test
id: 1
q: bar
df: field

- is_true: matched

- do:
explain:
index: test
type: test
id: 1
q: field:foo field:xyz

- is_true: matched

- do:
explain:
index: test
type: test
id: 1
q: field:foo field:xyz
default_operator: AND

- is_false: matched

- do:
explain:
index: test
type: test
id: 1
q: field:bars
analyzer: snowball

- is_true: matched

- do:
explain:
index: test
type: test
id: 1
q: field:BA*
lowercase_expanded_terms: false

- is_false: matched

- do:
explain:
index: test
type: test
id: 1
q: field:BA*
analyze_wildcard: true

- is_true: matched

- do:
explain:
index: test
type: test
id: 1
q: number:foo
lenient: true

- is_false: matched
70 changes: 70 additions & 0 deletions rest-api-spec/test/indices.validate_query/20_query_string.yaml
@@ -0,0 +1,70 @@
---
"validate_query with query_string parameters":
- do:
indices.create:
index: test
body:
mappings:
test:
_all:
enabled: false
properties:
field:
type: string
number:
type: integer

- do:
indices.validate_query:
index: test
q: bar
df: field

- is_true: valid

- do:
indices.validate_query:
index: test
q: field:foo field:xyz

- is_true: valid

- do:
indices.validate_query:
index: test
q: field:foo field:xyz
default_operator: AND

- is_true: valid

- do:
indices.validate_query:
index: test
q: field:bars
analyzer: snowball

- is_true: valid

- do:
indices.validate_query:
index: test
q: field:BA*
lowercase_expanded_terms: false

- is_true: valid

- do:
indices.validate_query:
index: test
q: field:BA*
analyze_wildcard: true

- is_true: valid

- do:
indices.validate_query:
index: test
q: number:foo
lenient: true

- is_true: valid

0 comments on commit c9128a1

Please sign in to comment.