My query for matching multiple words is as following,
{"query":
{"bool":{"must":[{"terms":{"my_field":"word1 word2"}}]}
upon execution, the result set is empty though data exists for the following query.
Instead of above query, if I use
{"bool":{"must":[{"terms":{"my_field":"word1"}}]}
then elastic-search is returning data.
How to match the complete sentence?
Based on your comment on the above answer, I believe you should simply use two term queries inside your must query array.
{
"query":
{ "bool" :
{
"must":[
{"term":{"my_field": "word1" } },
{"term":{"my_field": "word2" } }
]
}
}
}
you can try to put the words in an array and see if it works.
Like this:
{"query": {"bool":{"must":[{"terms":{"my_field":["word1", "word2"]}}]}
here is the documentation: https://www.elastic.co/guide/en/elasticsearch/guide/current/_finding_multiple_exact_values.html
Hope it works =)
Related
Is it possible to put a comment into an Elasticsearch query JSON? I want to be able to add some extra text to the query that's human-readable but ignored by Elasticsearch.
For example, if I have the following query:
{ "query": { "match_all": {} } }
I would like to be able to add a comment, maybe something like this:
{ "query": { "match_all": {} }, "comment": "This query matches all documents." }
Hacky workarounds (e.g., a query clause that has no effect on the results) would also be appreciated.
Seems like Elasticsearch does allow Javascript comments (/* */ and //) in JSON (Despite the JSON standard not supporting comments). So that's another option.
One solution to make this work is to use named queries, i.e. each query can be named
{
"query": {
"match_all": {
"_name": "This query matches all documents."
}
}
}
by inserting # [hash symbol] ,yes you can put comment for elastic search queries in console
When I call this URL:
http://192.168.x.x:9200/identities/work/_search?q=joh*
ES is returning a limited (5) set of matches, starting with some indexes of people names John and Johnny etc. That seems to be the correct result.
But when I send this JSON to ES:
{
"query": {
"match": {
"_all": "joh*"
}
}
}
I get results that I can't even logically explain. Seems rather random, and a lot of indexes too (hundreds, not a lot of johns and johnny's either ;))
Is this not the equivalent of the URL mentioned above? What am I doing wrong?
When you call the following URL, what ES does implicitly is to create a query_string query not a match query
http://192.168.x.x:9200/identities/work/_search?q=joh*
So the equivalent JSON query would be:
{
"query": {
"query_string": {
"query": "joh*"
}
}
}
Moreover, match queries do not handle wildcards as in joh*, the * is considered and matched as a real character, not as a wildcard.
I know this works:
{
"query": {
"query_string": {
"query": "reading,debating"
}
}
It searchs for all occurances of the words reading or debating. I want to know if there is a way so that I instead of having a comma separated string we can insert an array of strings? To sort of look like this:
{
"query": {
"query_string": {
"query": ["reading","debating"]
}
}
In your first example it is a QueryString query where elasticsearch internally handled the query understanding, and by default comma is a delimiter which is how your first query worked.
In this case probably what do you want is two or multiple term queries combined by a boolean(use "should" if you want OR logic and "must" if you want AND logic) query.
Here is how you can do it from curl:
http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html
If you want to use it programmatically in Java, you can use the QueryBuilders to construct a term queries and boolean query
We used the following query to get exact match on wildcard field:
"query_string": {
"query": "\"abcd\"",
"fields": [
"*.Name"
]
}
}
but it returned results also for values, such as: abcde, abcd1, etc...
Does anyone has an idea how to get an exact match in such case? We tried to add "analyzer:simple", which made things worse and sometimes didn't return results at all.
Thanks in advance.
I'm trying to use ElasticSearch to find all records containing a particular string. I'm using a match query for this, and it's working fine.
Now, I'm trying to sort the results based on a particular field. When I try this, I get some very unexpected output, and none of the records even contain my initial search query.
My request is structured as follows:
{
"query":
{
"match": {"_all": "some_search_string"}
},
"sort": [
{
"some_field": {
"order": "asc"
}
}
] }
Am I doing something wrong here?
In order to sort on a string field, your mapping must contain a non-analyzed version of this field. Here's a simple blog post I found that describes how you can do this using the multi_field mapping type.