I want to query elasticsearch for one or two key:value pair such as:
http://localhost:9200/indexname/_search?pretty=1&q=Date:%222000-12-30T10:11:25%22&q=id:%22unique_id%22
This query takes only the unique_id into consideration. If I change the Date to any invalid value, still it gives me all values based on the Unique_id.
Any idea how to make an AND condition with both the queries? It should consider both queries and provide result accordingly? Please advice. Thanks.
According to documentation, it should work
See http://www.elasticsearch.org/guide/reference/query-dsl/query-string-query.html
That said, you can create your query as below:
http://localhost:9200/indexname/_search?pretty=1&q=%2BDate%3A2000-12-30T10:11:25+%2Bid%3Aunique_id
Note : %2B is decoded as '+' whereas '+' is decoded as ' '
Try this.
GET /index/type/_search
{
"query": {
"match": {"Date":"2015-09-17 03:45:00-04"}
},
"filter" : {
"and" : [
{
"match": {"unique_id" : "6324"}
}
]
}
}
Reference link - https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-and-query.html
You can choose this query also ,(easy way)
GET myindex/mytype/_search
{
"query": {
"bool" : {
"must" :[
{
"term" : { "unique_id" : "6324"}
},
{
"term" : { "Date":"2015-09-17 03:45:00-04"}
}
],
}
}
}
Related
I am new to Elasticsearch.
Does anybody have an idea on how to change this query from an
currently _msearch query to an _search query?
It would also be convenient if I didn't have to make a separate query for every "CAR" but instead solve it with only one query. I would like to use the bucket aggregation instead.
This is my the query where I search for every Car separately:
GET /index/_msearch
{}
{"query": {"match": {"name": "CAR_RED"}},"size": 1,"sort": {"time":{"order": "desc"}}}
{}
{"query": {"match": {"name": "CAR_BLACK"}},"size": 1,"sort": {"time":{"order": "desc"}}}
{}
{"query": {"match": {"name": "CAR_WHITE"}},"size": 1,"sort": {"time":{"order": "desc"}}}
At the moment I am trying to solve it with the bucket aggregation but I always get an error.
GET /index/_search
{
"size": 0,
"aggs" : {
"CARS":{
"terms":{"field":"name.keyword"}
},
"bucket_sort": {
"sort": [
{"time":{"order": "desc"}}
],
"size": 1
}
}
}
It would be awesome if anyone could help me with this query.
GET /index/_search
{
"size": 0,
"query" : {"terms" : {"name.keyword" : ["CAR_RED","CAR_BLACK","CAR_WHITE"] }},
"aggs" : {
"CARS":{
"terms":{"field":"name.keyword"},
"aggs" : {
"top_cars_by_time" : {
"top_hits" : {
"sort" : [
{
"time" : {order: "desc"}
}
]
}
}
}
}
}
}
What this query does:
Filters in the query itself, the RED,BLACK AND WHITE CARS
Aggregates the results by car color.
Then for each bucket in the aggregation, sorts the results by the descending order of the time field.
SO for every car color, you will get the hit with the greatest time first, and so on.
Read up on top_hits, its the aggregation you need.
Top Hits Aggregation
HTH.
I need to search through a database with 3 keywords(3 queries), and I need to tell the user which of the keywords (query) that gave a result.
I've been looking into Named Queries as a possible solution.
https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-named-queries-and-filters.html
I was wondering if it is possible to apply named queries to a nested query?
According to the documentation:
The search response will include for each hit the matched_queries it
matched on.
So I tried with just one easy query to see how it works, I got a result, but no "matched_queries". Did I do something wrong?
This is my query in Kibana: (Im not using actual name)
GET database/document/_search
{
"query": {
"nested": {
"path": "first_path",
"query": {
"nested" : {
"path" : "second_path",
"query" : {
"match": {
"match_field": {
"query": "First query",
"_name" : "query"
}
}
}
}
},
"inner_hits": {}
}
}
}
From what I can see in
https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-named-queries-and-filters.html
The search response will include for each hit the matched_queries it
matched on. The tagging of queries and filters only make sense for the
bool query.
It looks like you should use "bool" query in your inner-most query:
.
.
.
"query" : {
"bool": {
"should" {
…
}
}
}
I'm having an issue with a search on an index. Here's my index creation:
curl -XPUT localhost:9200/my_supder_index -d '{
"mappings":
{
"doctype_I_index":
{
"properties":
{
"field_I_index":
{
"type":"string",
"term_vector":"yes"
}
}
}
}
}'
Here is a sample piece of content in that index:
{
_index:"my_super_index",
_type:"doctype_I_index",
_id:"676078",
_version:1,
found:true,
_source:{
created:"2015-05-02T00:24:03",
field_I_index:[
"21E0",
"19E0",
"5E0",
"6E0",
"4E0"
],
id:676078
}
}
Now when I do a search like this:
curl -XGET 'http://127.0.0.1:9200/my_super_index/_search' -d '{
"sort":[
{
"created":{
"order":"desc"
}
}
],
"query":{
"bool":{
"must":[
{
"terms":{
"field_I_index":[
"21E0"
],
"minimum_should_match":1
}
}
]
}
}
}'
I get zero results. It's not doing a match on the text. Can anyone point me in the right direction please?
On checking how analysis happens for this value , following are results -
curl -XPOST 'localhost:9200/news/_analyze?pretty' -d '21E0'
{
"tokens" : [ {
"token" : "21e0",
"start_offset" : 0,
"end_offset" : 4,
"type" : "<ALPHANUM>",
"position" : 1
} ]
}
Here you can see that the text is lower cased.
Also as term query does not apply analyzer for the search text , it looks for the exact match of 21E0 , but 21e0 is what is indexed.
Hence in this case , if you use match query , instead of term query , it should work.
But i would recommend to use not_analyzed to the field and then use term query on top of that. It might be a better approach.
I want to get objects according to an ID they have in an array in a json file in mongodb.
I tried a lot of ways to get them with no success:
db.collection.find({"Id":"2"})
db.collection.find({"Messages.Id":"2"})
db.collection.find({"Messages":{$elemMatch:{"Id":"2"}}})
db.collection.find({"Messages.Id":{$elemMatch:{"Id":"2"}}})
{
"Messages" : [
{
"text":"aaa",
"Id" : [ "1", "2" ]
},
{
"texts" : "bbb",
"Id" : [ "1", "3" ]
}
]
}
Even though that's how it's supposed to be done according to the mongodb documentation.
So I thought something was wrong with my json design (I tried changing it but that didn't help either).
Can anyone suggest to me a good design or query to get the objects with a certain id will work?
UPDATE:
I want for example that if in the query i request the id 2
only the first message and all of it will be displayed (I don't mind if the Id field wont be displayed)
{
"text":"aaa",
"Id":["1","2"]
}
To find single elements that match you will need to utilize the positional operator ($).
db.collection.find({"Messages.Id": "2"}, {"Messages.$": 1, _id: 0})
For finding multiple matches, you would use the aggregation pipeline:
db.collection.aggregate([
{ $unwind: "$Messages" },
{ $match: {"Messages.Id": "1"}},
{ $group: { _id: null, messages: { $push: "$Messages"}}}
])
I have a Collection named StudentCollection with two documents given below,
> db.studentCollection.find().pretty()
{
"_id" : ObjectId("52d7c0c744b4dd77efe93df7"),
"regno" : 101,
"name" : "Ajeesh",
"gender" : "Male",
"docs" : [
"voterid",
"passport",
"drivinglic"
]
}
{
"_id" : ObjectId("52d7c6a144b4dd77efe93df8"),
"regno" : 102,
"name" : "Sathish",
"gender" : "Male",
"dob" : ISODate("2013-12-09T21:05:00Z")
}
Why does the below query returns a document when it doesn't fulfil the criteria which I gave in find command. I know it's a bad & stupid query for AND comparison. I tried this with MySQL and it doesn't return anything as expected but why does NOSQL makes problem. I hope it's considering the last field for comparison.
> db.studentCollection.find({regno:101,regno:102}).pretty()
{
"_id" : ObjectId("52d7c6a144b4dd77efe93df8"),
"regno" : 102,
"name" : "Sathish",
"gender" : "Male",
"dob" : ISODate("2013-12-09T21:05:00Z")
}
Can anyone brief why does Mongodb works this way?
MongoDB leverages JSON/BSON and names should be unique (http://www.ietf.org/rfc/rfc4627.txt # 2.2.) Found this in another post How to generate a JSON object dynamically with duplicate keys? . I am guessing the value for 'regno' gets overridden to '102' in your case.
If what you want is an OR query, try the following:
db.studentCollection.find ( { $or : [ { "regno" : "101" }, {"regno":"102"} ] } );
Or even better, use $in:
db.studentCollection.find ( { "regno" : { $in: ["101", "102"] } } );
Hope this helps!
Edit : Typo!
MongoDB converts your query to a Javascript document. Since you have not mentioned anything for $and condition in your document, your query clause is getting overwritten by the last value which is "regno":"102". Hence you get last document as result.
If you want to use an $and, you may use any of the following:
db.studentCollection.find({$and:[{regno:"102"}, {regno:"101"}]});
db.studentCollection.find({regno:{$gte:"101", $lte:"102"}});