How to use lucene analyzers with Elasticsearch java API - elasticsearch

I want to build elastisearch queries using JAVA API. I want to know how to can use Lucene analyzers in elasticsearch java programs. I have checked QueryBuilders and tried to use analyzers directly as below.
QueryBuilder builder = QueryBuilders.matchQuery(searchString, fields).analyzer("porterstem");
But, it turned out to be wrong. If any one tried it, could you please give me some information?

You should define your analyzer in mapping.
So the analyzer will be used at index time and at query time.

ANALYZERS are used to analyze the documents that your are indexed. Analysis means it Ll split,the text in to tokens, normalize it, and also Lower case your indexed doc text. This analysis process Ll b more helpful while you search and searching will be faster..
You can mention analyzer while you query . But analyze the stored documents during query time. Ll b expensive. So analyze the document during indexing time. ES will analysis the doc during indexed and query time will b less and faster result.
So mention analyzers in mapping and searching efficiently..
For more information about analyzer refer
https://lucene.apache.org/core/4_0_0/core/org/apache/lucene/analysis/Analyzer.html

Related

Performance of match vs term query in elasticsearch?

I've been using a lot of match queries in my project. Now, I have just faced with term query in Elasticsearch. It seems the term query is more faster in case that keyword of your query is specified.
Now I have a question there..
Should I refactor my codes (it's a lot) and use term instead of match?
How much is the performance of using term better than match?
using term in my query:
main_query["query"]["bool"]["must"].append({"term":{object[..]:object[...]}})
using match query in my query:
main_query["query"]["bool"]["must"].append({"match":{object[..]:object[...]}})
Elastic discourages to use term queries for text fields for obvious reasons (analysis!!), but if you know you need to query a keyword field (not analyzed!!), definitely go for term/terms queries instead of match, because the match query does a lot more things aside from analyzing the input and will eventually end up executing a term query anyway because it notices that the queried field is a keyword field.
As far as I know when you use the match query it means your field is mapped as "text" and you use an analyzer. With that, your indexed word will generate tokens and when you run the query you go through an analyzer and the correspondence will be made for each of them.
Term will do the exact match, that is, it does not go through any analyzer, it will look for the exact term in the inverted index.
Because of this I believe that by not going through analyzers, Term is faster.
I use Term match to search for keywords like categories, tag, things that don't make sense use an analyzer.

Elasticsearch Aggregations. What are they?

can someone please in simple sentences explain what is elasticsearch aggregation exactly?
I searched but everywhere there are some explanations about how to use it, and about syntax.
but I can't understand the reason why they exist. what is their main purpose.
what kind of query they've build for ?
I need trend tags (suggest terms in search as you type ) in my search system
I faced elastic aggs , and I have no Idea what they are.
If you know SQL, elasticsearch aggregations are kind of group by clause of elasticsearch.
You can aggregate(group by) on field you want, can have document count on that field, can also have all the documents in that group, can have nested aggs(group by).
For suggest terms in search as you type aggs will not work .. you need to read about analysis document ... or read about fuzzy query in elasticsearch.

How range and phrase query work in elasticsearch?

If elastic search is using inverted index, I want to know how elasticsearch is able to support range queries and phrase queries.
Note: I saw that inverted index supports them but i am not clear on how they do it internally.
Found the link ..
Reference : https://blog.parse.ly/post/1691/lucene/
Here’s a snippet from Lucene in Action on the topic: “If you indexed your field with NumericField, you can efficiently search a particular range for that field using NumericRangeQuery. Under the hood, Lucene translates the requested range into the equivalent set of brackets in the indexed trie structure.”
This blog actually has some nice information on lucene indexes.

Getting ElasticSearch Percolator Queries

I'm trying to query ElasticSearch for all the percolator queries that are currently stored on the system. My first thought was to do a match_all with a type filter but from my testing they don't seem to be returned if I do a match_all query. I haven't for the life of me been able to find the proper way to query them or any documentation on it so any help is greatly appreciated.
Also any other information on how stored percolator queries are treated differently from other types is appreciated.
For versions 5.x and later
Percolator documents should be returned in a query as with any other document.
Documentation of this new behavior can be found here.
Please note that with the removal of mapping types in 6.x it is unclear what will happen with the percolator index type. The reader may assume that it will be removed and that percolators will/should be stored in separate indices. Separating percolators into isolated indices is usually suggested regardless. Also please note that this 6.x type removal should not affect the answer to this question.
For versions before 5.0
This will return all percolator documents stored in your elasticsearch cluster:
POST _all/.percolator/_search
This searches _all indexes (every index you have registered) for documents of the .percolator type.
It basically does what you describe above: "a match_all with a type filter". Yet it accomplishes it in a slightly different way.
I have not played around with this much more than this, but I assume this would actually allow you to perform a query/filter on percolators if you are looking for a percolator of a particular type.

retaining case in elasticsearch faceted search

Is there a way to do faceted searches using the elasticsearch Search API maintaining case (as opposed to having the results be converted to lowercase).
Thanks in advance, Chuck
Assuming you are using the "terms" facet, the facet entries are exactly the terms in the index. Briefly, analysis is the process of converting a field value into a sequence of terms, and lowercasing is a step in the default analyzer; that's why you're seeing lowercased terms. So you will want to change your analysis configuration (and perhaps introduce a multi_field if you want to run several different analyzers.)
There's a great explanation in Lucene in Action (2nd Ed.); it's applicable to ElasticSearch, too.

Resources