how does Lucene index not_analyzed fields - elasticsearch

I know for analyzed field, Lucene would tokenized the clause then store the tokens as an inverted index for searching. But how does Lucene index the Not_Analyzed fields, I don't believe it is still a inverted index. Is it BTree or Hash?

Not analyzed fields are also stored in the inverted index the same way analyzed fields are, they are simply... not analyzed. This means the field value will not be tokenized, etc, before being indexed.
So if your not_analyzed field contains the value New York, then that value will go unmodified and untokenized in the inverted index and you'll still be able to search for the documents containing that exact value. It's somehow similar to having an analyzed field whose analyzer is a keyword analyzer

Related

What on earth does elasticsearch _all field contains?

I have been using elasticsearch in work but confused by _all field for quite e long time. The document says that
The _all field is a special catch-all field which concatenates the
values of all of the other fields into one big string, using space as
a delimiter, which is then analyzed and indexed, but not stored
But do these "all fields" contains fields not analyzed, or not even indexed?
If anyone knows the answer, please kindly tell me, thanks in advance.
The _all field is a field which concatenates the values of all of the other fields into one big string, using space as a delimiter, which is then analyzed and indexed, but not stored. This means that it can be searched, but not retrieved.
The _all field allows you to search for values in documents without knowing which field contains the value.
Example
suppose you have indexed a document as below
{
"first_name": "sunder",
"last_name": "r",
"date_of_birth": "1996-03-20"
}
ok then the all index field for this document will be generated which will be as follow
"sunder r 1996 03 20"
which is then analyzed and indexed(The _all field is just a text field, and accepts the same parameters that other string fields accept, including analyzer, term_vectors, index_options, and store.)
and the _all field is not present in the _source field and it is not stored or enabled by default
Note
The _all field is Deprecated in ES 6.0.0.
_all may no longer be enabled for indices created in 6.0+, use a custom field and the mapping copy_to parameter

Are ElasticSearch document IDs scored?

If the mapping of a document is set to analyzed, it's scored and then relevant results are returned on a query. Are document IDs (_id) scored too?
I need to perform an exact ID match on a analyzed document and cannot change the type to non-analyzed since I'm unable to dump the data already in that index.
Will making a query with a specific ID return the exact match?
Document ID's are not scored in elasticsearch.
So index/type/id will fetch the exact document with given id
From docs:
Each document indexed is associated with a _type (see the section
called “Mapping Typesedit”) and an _id. The _id field is not indexed.

Difference between Query Context and Filter Context while Querying

What is the difference between the Query Context and the Filter Context in the Elastic Search in Query DSL.
My Understanding is Query Context- How well the document matches the query parameters.
Ex:
{ "match": { "title": "Search" }}
If I am searching for the documents with title 'Search' then if I contains two documents
i)title:"Search"
ii)title:"Search 123"
Then first document is a perfect match and document two is a semi-match. Then the first document is given in the first place and the second document given the second place. Is my understanding correct?
Filter Context:
Ex:
{ "term": { "status": "published" }}
If I am searching for the documents with status 'published' then if I contains two documents
i)status:"published"
ii)status:"published 123"
Then the first document is perfect so it is returned and the second match is not a perfect match so it is not returned. Is my understanding correct?
Basically in Query context, the elastic search scans all the documents and tries to find out how well the documents match the query, means the score will will be calculated for each documents. Where as in filter context,it will just checks whether the documents matches the query or not i.e, only yes or no will be returned. The filter queries does not contribute to the score of the document.
Next coming to the difference between the match and term queries , if you mapped a field to keyword then that field will be not analysed and its inverted index contains the whole term as it is, i.e is if status is mapped to keyword then if you insert "published 123" in status field , then its inverted index contains ["published 123"] and if status is mapped to text then while inserting data to status filed it is analysed for ex: if you insert "published 123" then its inverted index will be ["published","123"].
So whenever you use term query for keyword fields the query string will not be analysed and it tries to find exact term in the inverted index and if you use match query it analyses the query string and it returns all the doc's that contain the one of the analysed string of query in it's inverted index
Your understanding about the difference between term and match queries is correct at the most basic level but like Jettro commented in the filter query you mentioned both the documents will be selected. When doing a term query it really depends what kind of analyzer you are using and how that affects the terms that are stored in inverted index that lucene uses.
To quote an example from the Elasticsearch: Th Definitive Guide "if you were to index ["Foo","Bar"] into an exact value not_analyzed field, or Foo Bar into an analyzed field with the whitespace analyzer, both would result in having the two terms Foo and Bar in the inverted index."
Now under the hood the term query will search all the terms in the inverted index for your query term and even if one of them matches it will be returned as a result.
So in the first case there is only "published" in the inverted index but in the second case too there are both terms "published" and "123", so both documents will be returned as matches.
It also is important to remember that the term query looks in the inverted index for the exact term only; it won’t match any variants like "Published" or "publisheD" with "published".

How fields are associtated with terms in inverted index in elasticsearch?

As per my understanding, elasticsearch uses a structure called inverted index to provide full text search. It is clear that inverted index has terms and ids of the documents which has that term but the document can have any number of fields and the field name can be used in the query time to look/search only on that field. In that case how elasticsearch restricts/limits search only to a particular field? I would like to know if inverted index contains fields name or field id along with terms and document id.
Similar thing happens when you sort based on any field. So there could be a way to associate terms with field names. Please help me understand the intricacies involved here.
Thanks in advance.
I would like to know if inverted index contains fields name or field id
along with terms and document id.
Quoting from Lucene Docs
The same string in two different fields is considered a different term. Thus terms are represented as a pair of strings, the first naming the field, and the second naming text within the field.
In that case how elasticsearch restricts/limits search only to a
particular field?
Each segment index maintains Term Vectors : For each field in each document, the term vector is stored. A term vector consists of term text and term frequency.
Hence, the indexes are maintained for each field in each document.
We have a inverted index per field per index.
And there is something called field data cache ( or doc values ) which has the inverted "inverted index". All doc to field value lookup happens here.
I was also having this question
I can share my understanding here with you.
Elasticsearch creates an inverted index for each full-text field of the document. So if an index has 10 fields that allow full-text search then Elasticsearch will create 10 different inverted index for the 10 fields and store the analyzer results in those inverted indices for each field.
Thus when you perform a search operation and specify what all fields you want to search then Elasticsearch will search on the inverted indices of those specific fields only
Thus to summarize, an inverted index is created at the field level.
I hope that helps
Thanks

Is there a way to denote in search query that "omit length of the field" in elasticsearch?

The only solution for omitting the length of the fields is to put/change mapping of the document and re-indexing the whole data which I do not want.
Is there a way to ignore length of the field while querying?
If the length filter is being carried out by the Length Token Filter within an analyzer then terms filtered out won't be in your inverted index.
So you will not be able to search for the missing terms using a query - you'd need to update your analyzer and re-index.

Resources