ElasticSearch API POST/PUT DeDupe - elasticsearch

I am looking to use the Elastic Search RESTful API to send data to my ES instance. Here is some sample data:
[{"subject":"matt","predicate":"likes","object":"coffee","label":"1_10"}]
[{"subject":"james","predicate":"likes","object":"water","label":"1_10"}]
[{"subject":"leo","predicate":"likes","object":"liquor","label":"1_10"}]
[{"subject":"matt","predicate":"likes","object":"coffee","label":"1_10"}]
[{"subject":"matt","predicate":"likes","object":"coffee","label":"1_10"}]
My post call looks like:
"http://" + url + "/something/quads/
With the JSON payload.
I was looking at a put call and was trying the following:
"http://" + url + "/something/quads/_create
from this documentation: https://www.elastic.co/guide/en/elasticsearch/guide/current/create-doc.html
The problem is that it created the ID in ES as _create Is there something I am doing wrong?

If you use POST calls with URLs like /something/quads/ then ES will automatically generate IDs for your documents.
If, instead, you want to use PUT calls, you need to provide document IDs yourself in the URL /something/quads/123, /something/quads/456, etc.
In your second URL /something/quads/_create, you're missing the document ID. It should be /something/quads/123/_create. Check the docs you've linked to again and you'll see.
Also note that the difference between the two following commands
PUT /something/quads/123
PUT /something/quads/123/_create
is that the second will fail if a document with the ID 123 already exists. The first command, however, will always succeed and overwrite the document with ID 123 if one exists.

Related

Get full URL with parameters in Thymeleaf?

I've got a paginated list of cars, on a Spring Boot server, with the parameters sort, range, desc, page, etc to filter and sort by and am generating the URL in Thymeleaf that looks like:
example.com/cars?page=5&sort=mileage
I am wanting to be able to add more parameters to a URL with a few of these already but I'm quite new to this and don't really know how to get the current URL with all the parameters to add more params without losing the previous ones look like
example.com/cars?page=5&sort=mileage&desc=true
I've found an answer to do something like this on Spring but would ideally want to do it on the Thymeleaf template, is this possible?
Get full current url thymeleaf with all parameters
I found that you can get hold of specific parameters in Thymeleaf using ${param.sort} to get hold of the param sort, could something similar to this get hold of all the params currently?
Thanks
If someone is still looking for a thymeleaf template only solution, you could use ${#request.getRequestURI()} with ${#request.getQueryString()} and add your additional parameters via concatenation:
<a th:href="#{${url}}" th:with="url=${#request.getRequestURI()+'?'+#request.getQueryString()+'&foo=bar'}">Link</a>
If you need to escape query parameters, you can use #uris.escapeQueryParam():
<a th:href="#{${url}}" th:with="url=${#request.getRequestURI()+'?'+#request.getQueryString()+'&foo='+#uris.escapeQueryParam('b a r')}">Link</a>
Some further details:
You have to use th:with, otherwise the parser will throw TemplateProcessingException: Access to request parameters is forbidden in this context. in newer thymeleaf versions.
It also works when the current query is empty, the url generator will create a valid url, in my example including one ? and no & in the query part.

Why I can't search for UUID in elasticsearch database using filters

I have Django app which use elasticsearch database, I use elasticsearch-dsl and all my filters and queries works. But I have a problem with one parameter, it's UUID. I always got 0 results from my request in shell:
s = Search(index='my_index_name').filter('term', UUID='0deaa49b-15b6-4c10-acb7-d98df800e0df')
response=s.execute()
response
I use django-rest-elasticsearch and I have the same issue, I got correct REST result with all my filters, but not with UUID request. Something like this works, but I need to use filtering.
q = Q("multi_match", query="0deaa49b-15b6-4c10-acb7-d98df800e0df", fields=["UUID",])
response=s.execute()
response
Maybe someone know hot to use UUID in my REST, because UUID=0deaa49b-15b6-4c10-acb7-d98df800e0df don't work.

Spring JPA With mongo Supporting Contains and LessThan

I am using Spring JPA with mongo.I have a requirement to use contains query on one of the fields.If end users(UI/Service) hits the GET Request and looks for information in EMAIL Field,I need to search based on text.Pretty much it is like.
https://docs.spring.io/spring-data/jpa/docs/current/reference/html/
This link explains that We can Use Contains to get the Data.
List<ScheduledNotification> findByMobileNumberContaining(String mobileNumber);
List<ScheduledNotification> findByEmailIdIgnoreCaseContaining(String emailId);
But when I am using this API,I am not able to get the data.So ,has any one done something like using Contains.
well ,I was not sending content type in header attribute in GET request.So yes it works,alright.There was no issue with method signature.

How can i get the 'id' when i want to match url like ''/index?id=xxx'' in koa-router

How can i get the 'id' when i want to match url like '/index?id=xxx' in koa-router? most articles on the web usually use url lick '/index/:id',but the back-end interfaces are not the case.
It does work the same on a back-end API, when you make a GET request to a web server it parses the query-string and uses the values to give you the intended response.
When handling a GET request using koa-router you can specify the query string, such as '/index/:id', and to consume the value in your code you would simply do
var id = ctx.request.query.id;

Can you do an elasticsearch geo_distance query (or a filter) as a uri request

I would like to run an elasticsearch query to find items within 10mi of a given point.
I know how to do it with a post, but I would like to use a get with everything in the uri.
I found the below example but it does not work.
http://localhost:9200/items/item/_search?{%22query%22:{%22filtered%22:{%22query%22:{%22match_all%22:{}},%22filter%22:{%22geo_distance%22:{%22distance%22:%220.1km%22,%22location%22:{%22lat%22:46.884106,%22lon%22:-71.377042}}}}}}
Any way to do this or am I stuck using a post?
The key is the source= parameter. Not to be confused with _source.
http://localhost:9200/items/item/_search?source={%22query%22:{%22filtered%22:{%22query%22:{%22match_all%22:{}},%22filter%22:{%22geo_distance%22:{%22distance%22:%220.1km%22,%22location%22:{%22lat%22:46.884106,%22lon%22:-71.377042}}}}}}
I had tried ?q= and a few other parameters listed on http://www.elasticsearch.org/guide/reference/api/search/uri-request/ with no luck (source is not listed).
I found http://www.elasticsearch.org/guide/reference/api/ and at the very bottom it says
request body in query string
For libraries that don’t accept a request body for non-POST requests,
you can pass the request body as the source query string parameter
instead.
So structure your query/filter request, set it all on one line and send it into the source parameter.
Do not use the q= parameter with source= or it will conflict and break the query, however I tried size= and from= and they work with source just fine.

Resources