I'm new to Elasticsearch and I'm wondering how I can make a search specifing one or more fields.
With SQL I would write this query:
"SELECT field1, field2, field3 FROM tablename WHERE field1 = 'X' AND field2 != 'Y' AND field3 = 'Z'"
In Elasticsearch I'm starting from this:
{ "query": { "filtered": { "query": { "query_string": { "query": "*" } }, "filter": { "term" : { "field1" : "286" } } } } }
One of the most common queries in elasticsearch is the match query, which works on a single field. And there's another query with the very same options that works also on multiple fields, called multi_match. These queries support text analysis and work really well.
The multi_match query builds on the match query to allow multi-field queries: GET /_search { "query": { "multi_match" : { "query": "this is a test", "fields": [ "subject", "message" ] } } } The query string. The fields to be queried.
After indexing, you can search, sort, and filter complete documents—not rows of columnar data. This is a fundamentally different way of thinking about data and is one of the reasons ElasticSearch can perform a complex full-text search.
The sql query is equivalent to:
{ "query": { "bool": { "must": [ { "term": { "field1": "X" } }, { "term": { "field3": "Z" } } ], "must_not": { "term": { "field2": "Y" } } } } }
In any case I recommend you to read a bit the doc before starting with elasticsearch if you are new.
There are lots of types of queries and some of them depends on how you index your data, for example for strings, you can analyze strings (lowercase, stem words, remove stopwords, ...) at index time. The query I posted will never match a doc whose field1 is "X" if you analyze that field at index time and convert it to lower case.
Once you know a little bit better elasticsearch you can use filters for improving your queries.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With