Conditionally retrieve fields from elasticsearch _source based on value of another field - elasticsearch

I have a type with a field indicating if to reveal another field. For example, for a document, if ifRevealAge is true, value of the age field should be retrieved from _source and included in query result for the document. If ifRevealAge is false, then age field should not be included in the query result for the document. Is it possible by query API?
If it is not, how to do it through java API? I think the method toXContent(XContentBuilder builder, ToXContent.Params params) of GetResponse is supposed to do so but I can't find any documentation or references for how to use the method for filtering / modifying response result conditionally. The api documentation is just too concise.
Many thanks

Related

Best way to add data elastic search

I am posting data so I can search it later in elastic search.
I am posting it like this:
POST https://localhost:9200/superheroes/_doc/27
body:
{
"name": "Flash",
"super_power": "Super Speed"
}
This is automatically stored on a _source object... but I read the _source field itself is not indexed (and thus is not searchable) online, and the entire purpose of this application is quick search by value... like if I wanna know which superheroes have super speed and I write super speed on the search bar.
you are right about the _source field, but if you don't define the mapping for your fields, Elasticsearch generates the default mapping with default param for these fields . You can read more about the mapping param and in your case, if you want field to be searchable it needs to be the part of the inverted index and this is controlled by the index param.
You can override the value of these params by defining your own mapping(recommended otherwise Elasticsearch will guess the field type based on the first data you index in a field).
Hope this helps and clears your doubt, in short if you are not defining your mapping, your text data is searchable by default.

How to only store the index,not the original text in ES

I am using elastic search 7.10.1. I would store and search against my blogs. The blog has id,title and content fields.
I would like to search against id, title and content, but since the content of blog is too big, so that I would like to save the original content text outside of Elastic Search, such as HBase.
I would ask how to achieve this in ES?
If you are using a static mapping then simply don't define your content field in your index mapping, and don't populate it while indexing your document to ES.
Refer to Mapping param for more info, and specifically, store param default false which means you can't retrieve field value if _source(true by default) is also disabled.
index param default true, which controls whether the field is searchable or not, in your case if you don't want to search and retrieve it you have to disable these two params.

Elasticsearch Query where KEY is Null

Im having problems getting a query to return the results i require. Im wanting to search for all documents which have a Field name called 'Title' but only them where the value is null.
By default Elasticsearch does not store fields with null value in its index, so you are not able to distinguish documents without Title field at all from documents where this field contains null. To cope with this issue you have explicitly choose some value that will indicate that field is empty. Look at this article for details.

How to query elasticsearch without specifying which property to match

I have an elasticsearch document with multiple properties, like title, body, author.
I'd like to get results from a query that matches ANY of the fields.
Usually, I'll query by specifying a property to match. I'd rather write a more flexible query that will return the whole document if any of the properties are matched.

Stored field in elastic search

In the documentation, some types, such as numbers and dates, it specifies that store defaults to no. But that the field can still be retrieved from the json.
Its confusing. Does this mean _source?
Is there a way to not store a field at all, and just have it indexed and searchable?
None of the field types are stored by default. Only the _source field is. That means you can always get back what you sent to the search engine. Even if you ask for specific fields, elasticsearch is going to parse the _source field for you and give you back those fields.
You can disable the _source if you want but then you could only retrieve the fields that you explicitly stored, according to your mapping.

Resources