Best way to index base64 encoded string into ElasticSearch - elasticsearch

I'm using Spring-Data-ElasticSearch to index a whole object as a document in Elastic. One of the field is a String typed base64 encoding of user upload file.
#Document(indexName = "user_record")
public class UserRecord {
private String base64UserUploadFile;
...
Currently this base64 string is indexed directly into Elastic so not searchable, so I'm wondering what's my options here if I want to be able to search the actual content from that file without having to convert this field to be the actual file content string in my class?

You might want to use the mapper-attachments plugin and declare your field with the Attachment field type
#Document(indexName = "user_record")
public class UserRecord {
#FieldType(type = FieldType.Attachment, store = false)
private String base64UserUploadFile;
...
That way the Base64 content will be indexed and searchable. I suggest not to store the content (hence store=false) if you don't want to inflate your index unnecessarily.

Related

Problem indexing LongField from custom FieldBridge

for a search using lucene, I made a bridge,
public class EntityIDFieldBridge implements FieldBridge {
#Override
public void set(String name, Object value, Document document, LuceneOptions luceneOptions) {
BaseEntity baseEntity = (BaseEntity) value;
if(value !=null){
Field field = new LongField(name, baseEntity.getId(),Field.Store.NO);
document.add(field);
}
}
}
when I search for the value, I dont get the correct documents. when I search term:* I do get the ones that are not null, so I see that its getting indexed.... StringField is working fine. But I think it should be a long field. Any ideas?
Based on the little information you provided, I am assuming that you are not trying to get a value whihc is null.
Field Bridge provided more information on what it is, what lucene supports and how it works:
In Lucene all index fields have to be represented as Strings. For this
reason all entity properties annotated with #Field have to be indexed
in a String form. For most of your properties, Hibernate Search does
the translation job for you thanks to a built-in set of bridges. In
some cases, though you need a more fine grain control over the
translation process.
Also for Null values
null elements are not indexed. Lucene does not support null elements
and this does not make much sense either.

Spring Data find substring containing special characters

How to retrieve data from elasticsearch containing special characters like . / - ... using spring data ?
I have document defined like this:
#Document(indexName = "audit-2018.135", type = "audit")
public class Trace {
#Id
private String id;
#Field(type = FieldType.Text)
private String uri;
// setters & getters
}
I need to retrieve data from elasticSearch by field uri.
Here's an example how data looks:
martin-int-vip.vs.cz:5080/kib/api/runtime/case/CASE0000000000324223
When using kibana I can use:
uri: "martin-int-vip.vs.cz:5080/kib"
and I get back the record above which contains desired substring.
Now I need to achieve the same from java however in spring data it's not working as expected.
I have elasticSearchRepository defined like this:
public interface TraceRepository extends ElasticsearchRepository<Trace, String> {
List<Trace> findByUriContaining(String uri);
}
when I call method findByUriContaining with parameter uri as:
martin-int-vip.vs.cz:5080\/kib
or even this
martin-int
I get back 0 results. When I send "kib" as parameter it returns correctly all records containing word "kib" however it's not working with special characters like . / - etc. How should I query my elasticSearch from java to get all records which contains my desired substring ? Thanks
I found out that by default elasticSearch analyzer tokenize your query. Instead of "martin-int-vip.vs.cz:5080/kib" it looks for a field which contains martin and int and vip...
In order to query these kind of fields you need to change behaviour of analyzer or you can use elasticSearchTemplate and Criteria to build more flexible queries.

Spring Elastic Search Custom Field names

I am new to Elastic Search and I am trying to implement it using Spring-data-elasticsearch.
I have fields with names such as "Transportation", "Telephone_Number" in our elastic search documents.
When I try to map my #Domain object fields with those, I don't get any data for those as I couldn't successfully map those fields.
Tried to use #Field, was disappointed as it didn't have 'name' property in it to map with custom field name.
Tried different variations of a GETTER function, none of those seem to be mapping to those fields.
I started wondering if there's something I'm missing here.
How does a domain object field look like which should map to a filed called something like "Transportation" ?
Any help appreciated
You can use custom name. Spring Data ES use Jackson. So, you can use #JsonProperty("your_custom_name") to enable custom name in ES Mapping
for example:
#Document(indexName = "your_index_name", type = "your_type_name")
public class YourEntity {
....
#JsonProperty("my_transportation")
#Field(type = FieldType.String, searchAnalyzer = "standard", indexAnalyzer = "standard", store = true) // just for example
private String myTransportation;
....
}
Note: I'm sorry anyway, my english is bad.. :D

Elasticsearch NEST - SortAsceding doesn't sorts documents

I am trying to sort the result set based on a field name. But Sort doesn't works with string type.
Tried Code:-
public class Company
{
public long Number { get; set; }
public string Name{ get; set; }
}
My problem is : Sorting is not done when I use SortAscending API, like below
var resultSet = client.Search<Article>(s => s
.Type("Company")
.From(0)
.Size(200)
.QueryString("Stack OverFlow")
.SortAscending(f => f.Name));
Note: Documents are listed as Sorted if I set field name as Number(f => f.Number)
Please help
Your issue with sorting on the name field in your index is probably related to the fact that the field is being analyzed/tokenized. From the Elasticsearch Sort Guide:
For string based types, the field sorted on should not be analyzed / tokenized.
Therefore, you need to provide an additional field that is not analyzed/tokenized to perform your sort against. You can accomplish this by adding an additional field to your documents and setting the mapping for that type/field to not_analyzed or you can leverage multi_field (now just fields in version 1.x) on your existing name field. Please refer to the following for guidance on how to accomplish either of these options:
Multi-Fields (or Fields in v1.X)
Mapping

How do I post more than one parameter using spring #requestparam/#requestbody annotation in json format

I want to upload an item object with it's image is it possible that i pass a complete object of my Item along with it's image from my Controller if yes then how may i Do it? and I need a Json Pattern also which will be coming from client side.
What would be the Json pattern for my following Item Object.
Class Item{
String ItemID;
String ItemName;
String CategoryID;
List<Image> imagesList;
}
Class Image{
String imageId;
String imageTitle;
byte[] image;
}
and one more thing. Please tell me how can I pass two parameters in my Controller. e.g. if I want to pass Item object in the Request Body along with an extra parameter say String UserID. HOw may I write the following code.
#RequestMapping(method = RequestMethod.POST)
#ResponseStatus(HttpStatus.CREATED)
public void add(#RequestBody Item myItem, String UserID)
{ ...... }
and please tell me what if I pass in the Request in Json format. What will be it looks like?
{ "Item" : { "ItemID": "1", ......}, "UserID" : "hello"}
will it be like this or what? because i tried it i'm getting the Object and string as Null. I also tried it with #RequestParam but same got NULL while debugging.
Currently i'm dealing with this by this.
Class ItemUserPost{
Item item;
String UserID;
}
I'm storing this object in MongoDB. Will be my whole collection of Item will stored along with the images? or i have to use a MultiPart and GridFS API for this. Kindly please refer if related question already posted.
Please Help Urgent.
Thanks in Advance.
The Request Body it's just ONE body. You cannot pass multiple objects or parameters in the same body.
There is 2 turnovers for this:
If you're using REST, you can put the UserID on #RequestMapping(value = "{userId}") and get it from the URL. Or, you can simply put the UserId as query parameter on your url.
Keep using your ItemUserPost object.
--
And for the second question:
Yes, It will store your collection with the images.

Resources