Comments (15)
Hi there! Do these tests help?
from elastic.
Got it, very useful. I have another question, does it support wrapper several kinds of query in a Query?
Thanks very much!
from elastic.
You mean something like boolean queries (docs on elastic.co)?
In case you missed it, Elasticsearch - The Definite Guide is a fantastic online book for learning many of the details of Elasticsearch. Elastic, the Go client, is simply a DSL to all that, and the tests in this repository will probably help you find out about the correct syntax.
Hope this helps :-)
from elastic.
I notice this, your code generate this json:
{"template":{"id":"find_user_tpl","params":{"query_string":"Lin"}}}
Actually, below json code is needed.
{ "template": { "id": "find_user_tpl" },
"params": { "query_string": "Lin" }
}
from elastic.
Are you sure this is correct? The docs state that params
is below template
and on the same level as id
.
from elastic.
I'm sure, maybe it has been changed since 1.5, you can find in the end of this official page below:
http://www.elastic.co/guide/en/elasticsearch/reference/current/search-template.html#_more_template_examples
I could not get result by
curl -XGET localhost:9200/users/_search/template -d '{"query":{"template":{"id":"find_user_tpl","params":{"query_string":"Lin"}}}}'
or request localhost:9200/users/_search url.
but I could get correct result using query below:
curl -XGET localhost:9200/users/_search/template -d '{
"template": {
"id": "find_user_tpl"
},
"params": {
"query_string": "Lin"
}
}'
from elastic.
I test the docs you provided, using the following command:
curl -XPUT localhost:9200/_search/template/my_template1 -d '{
"template": { "query": {"match": { "name": "{query_string}" }}}
}'
or
curl -XPUT localhost:9200/_search/template/my_template2 -d '{
"template": { "match": { "name": "{query_string}" }}
}'
curl -XGET localhost:9200/users/_search -d '
{
"query" : {
"template": {
"id": "my_template1",
"params": {
"query_string": "Lin"
}
}
}
}'
I cannot get any result. But using the query below, I can fetch the result.
curl -XGET localhost:9200/users/_search -d '{
"query": {"match": { "name": "Lin" }}
}'
Could you tell me how to correct my template or query command?
from elastic.
if I use this query,
curl -XGET localhost:9200/users/_search -d '
{
"query" : {
"template": {
"id": "my_template1",
"params": {
"query_string": "Lin"
}
}
}
}'
I find errors in elasticsearch logs:
[users] No query registered for [query]
[2015-04-24 14:35:50,330][DEBUG][action.search.type ] [Jack Flag] [users][0], node[5ulnMlaaTMG078xQ293FPw], [P], s[STARTED]: Failed to execute [org.elasticsearch.action.search.SearchRequest@67480ed4] lastShard [true]
org.elasticsearch.search.SearchParseException: [users][0]: from[-1],size[-1]: Parse Failure [Failed to parse source [
{
"query" : {
"template": {
"id": "tpa",
"params": {
"query_string": "Lin"
}
}
}
}]]
at org.elasticsearch.search.SearchService.parseSource(SearchService.java:721)
at org.elasticsearch.search.SearchService.createContext(SearchService.java:557)
at org.elasticsearch.search.SearchService.createAndPutContext(SearchService.java:529)
at org.elasticsearch.search.SearchService.executeQueryPhase(SearchService.java:291)
at org.elasticsearch.search.action.SearchServiceTransportAction$5.call(SearchServiceTransportAction.java:231)
at org.elasticsearch.search.action.SearchServiceTransportAction$5.call(SearchServiceTransportAction.java:228)
at org.elasticsearch.search.action.SearchServiceTransportAction$23.run(SearchServiceTransportAction.java:559)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Caused by: org.elasticsearch.index.query.QueryParsingException: [users] No query registered for [query]
at org.elasticsearch.index.query.QueryParseContext.parseInnerQuery(QueryParseContext.java:300)
at org.elasticsearch.index.query.TemplateQueryParser.parse(TemplateQueryParser.java:86)
at org.elasticsearch.index.query.QueryParseContext.parseInnerQuery(QueryParseContext.java:302)
at org.elasticsearch.index.query.IndexQueryParserService.innerParse(IndexQueryParserService.java:382)
at org.elasticsearch.index.query.IndexQueryParserService.parse(IndexQueryParserService.java:281)
at org.elasticsearch.index.query.IndexQueryParserService.parse(IndexQueryParserService.java:276)
at org.elasticsearch.search.query.QueryParseElement.parse(QueryParseElement.java:33)
at org.elasticsearch.search.SearchService.parseSource(SearchService.java:705)
... 9 more
[2015-04-24 14:35:50,330][DEBUG][action.search.type ] [Jack Flag] [users][4], node[5ulnMlaaTMG078xQ293FPw], [P], s[STARTED]: Failed to execute [org.elasticsearch.action.search.SearchRequest@67480ed4]
from elastic.
I see. Search templates are really confusing IMHO. Let's figure this out...
Can you successfully run this in your ES installation (notice that it creates a test
index and a my_template2
search template)?
# Set up the index and add some documents
echo Delete index...
curl -XDELETE 'localhost:9200/test/'
echo
echo Create index...
curl -XPUT 'localhost:9200/test/' -d '{"settings":{"index":{"number_of_shards":1,"number_of_replicas":0}}}'
echo
# Add some documents
echo Add documents...
curl -XPOST 'localhost:9200/test/testtype/1' -d '{"text":"value1_foo"}'
curl -XPOST 'localhost:9200/test/testtype/2?refresh=true' -d '{"text":"value2_foo value3_foo"}'
curl -XPOST 'localhost:9200/test/_refresh'
echo
# Now set up a template for querying by name via match
echo Delete search template...
curl -XDELETE 'localhost:9200/_search/template/my_template2'
echo
echo Create search template...
curl -XPUT 'localhost:9200/_search/template/my_template2' -d '{
"template": {
"query": {
"match": {
"text" : "{{my_value}}"
}
}
}
}'
curl -XPOST 'localhost:9200/_refresh'
echo
# Now use the search template to search by keyword
echo Searching with template...
curl -XGET 'localhost:9200/test/_search/template' -d '{
"template": {
"id": "my_template2"
},
"params": {
"my_value": "value1_foo"
}
}'
echo
It should return the first document in the index (value1_foo
).
The output should be something like this:
Delete index...
{"acknowledged":true}
Create index...
{"acknowledged":true}
Add documents...
{"_index":"test","_type":"testtype","_id":"1","_version":1,"created":true}{"_index":"test","_type":"testtype","_id":"2","_version":1,"created":true}{"_shards":{"total":1,"successful":1,"failed":0}}
Delete search template...
{"found":true,"_index":".scripts","_type":"mustache","_id":"my_template2","_version":3}
Create search template...
{"_id":"my_template2","_version":4,"created":true}{"_shards":{"total":117,"successful":116,"failed":0}}
Searching with template...
{"took":1,"timed_out":false,"_shards":{"total":1,"successful":1,"failed":0},"hits":{"total":1,"max_score":1.0,"hits":[{"_index":"test","_type":"testtype","_id":"1","_score":1.0,"_source":{"text":"value1_foo"}}]}}
from elastic.
BTW: There seems to be something wrong on the Elasticsearch documentation page. I posted this issue to clarify. IIRC Mustache tags must be written as {{...}}
. The Search Template docs use the {{...}}
syntax, while the Template Query docs use {...}
.
Can you check your code against this, too?
from elastic.
As far as i can tell it turns out that it works like this.
The template gets expanded as is. So there's two ways to make your query work.
a) Searching with the /_search/template
endpoint
# Register template
curl -XPUT 'localhost:9200/_search/template/my_template2' -d '{
"template": {
"query": {
"match": {
"text" : "{{my_value}}"
}
}
}
}'
# Search
curl -XGET 'localhost:9200/test/_search/template' -d '{
"template": {
"id": "my_template2"
},
"params": {
"my_value": "value1_foo"
}
}'
b) Searching with the /_search
endpoint
# Register template
curl -XPUT 'localhost:9200/_search/template/my_template3' -d '{
"template": {
"match": {
"text" : "{{my_value}}"
}
}
}'
# Search
curl -XGET 'localhost:9200/test/_search?pretty' -d '{
"query": {
"template": {
"id": "my_template3",
"params": {
"my_value": "value1_foo"
}
}
}
}'
Notice the subtle differences both in registering the template (the query
is missing in b)) and the way to pass parameters when searching.
Can you confirm my findings?
As far as I can see, Elastic (the Go client) currently only supports b).
from elastic.
Thanks very much for your explanation. I try several method include with two {{. Thanks to your comment, I notice there is no "query" following template when PUT the template.
from elastic.
Yes. When you think of it, it's pretty clear. The defined template just gets expanded and replaced in-place. So when you add a query
to it (like in a)), you get {"query":{"query":{...}}}
if you use it in b).
Does this solve your issues with search and search templates?
P.S.: I have some tests for the search templates here in case you need working code.
from elastic.
Nice, it's working in my project. Thanks for your useful golang client, and the clear test cases.
from elastic.
You're welcome. I'll close the issue then. Feel free to create another one when necessary.
from elastic.
Related Issues (20)
- Completely disabling backoff
- Does olivere support "REST API compatibility" HOT 3
- Size does not match with len(searchResult.Hits.Hits)
- panic.go:1038 +0x215 github.com/olivere/elastic/v7.(*TermsQuery).Source(0x0) HOT 1
- Load balance between multiple master nodes HOT 1
- Bulk index failures HOT 1
- I want to get the result body. Is there any way? HOT 1
- Generating nested histogram query HOT 1
- The es daterange query data has no expected effect
- how to get root cause with 400 error HOT 1
- ()elastic.Each Swallowed the error
- Unresolved reference 'Aggregation, Size, TrackTotalHits, Pretty, Sort, and Do'
- Append support max_concurrent_shard_requests in msearch
- Support for Version=0 in bulk_delete_request.go HOT 2
- The collapse query does not support multiple inner_hit conditions HOT 1
- elastic v6 not work correctly with http proxy
- elastic: Error 400 (Bad Request): only write ops with an op_type of create are allowed in data streams [type=illegal_argument_exception]
- Can't use REST compatibility headers
- The context field in the structure SearchSuggestionOption(in search.go) should change to be highlight
- How to search data with a 'knn query'?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from elastic.