find by query and push to array in Elastic search - elasticsearch

I store data in the elastic search like this:
{
"_index": "my_index",
"_type": "doc",
"_id": "6lDquGEBFRQVe0x93eHk",
"_version": 1,
"_score": 1,
"_source": {
"ID_Number": "6947503728601",
"Userrname":"Jack.m07",
"name": "Jack",
"photos": ["img/one.png"]
}
}
I want find user by ID_Number and push new value to Photos
e.g)
"photos": ["img/one.png","img/two.png"]
How can I implement this? What is the query?

I found answer,this is the query
POST my_index/_update_by_query?conflicts=proceed
{
"script": {
"inline": "ctx._source.photos.add(params.new_photos)",
"params": {
"new_photos": "img/two.png"
}
},
"query": {
"terms": {
"ID_Number": "6947503728601"
}
}
}

Related

Difference between match vs wild card query

What is the difference between the Match and Wild card query? If the requirement is to search a combination of words in a paragraph or log which approach is better?
Match query is used to find all those documents that have the exact search term (ignore the case), whereas Wildcard query returns the documents that contain the search term.
Adding a working example
Index Data:
{
"name":"breadsticks with soup"
}
{
"name":"multi grain bread"
}
Search Query using Match query:
{
"query": {
"match": {
"name": "bread"
}
}
}
Search Result will be
"hits": [
{
"_index": "67706115",
"_type": "_doc",
"_id": "1",
"_score": 0.9808291,
"_source": {
"name": "multi grain bread"
}
}
]
Search Query using wildcard query:
{
"query": {
"wildcard": {
"name": "*bread*"
}
}
}
Search Result will be
"hits": [
{
"_index": "67706115",
"_type": "_doc",
"_id": "1",
"_score": 1.0,
"_source": {
"name": "multi grain bread"
}
},
{
"_index": "67706115",
"_type": "_doc",
"_id": "2",
"_score": 1.0,
"_source": {
"name": "breadsticks with soup"
}
}
]

Elastic search negate phrase and words in simple query string

I'm trying to negate some words and phrases in an Elastic Search request using the simple query string.
This is what I do:
&q=-"the witcher 3"-game-novel
So basically, trying to negate a phrase AND the words after it. But that doesn't seem to work.
If I try to negate the words alone it works.
How can I negate phrases and sentences in a simple query string?
Adding a working example with index data,search query, and search result.
Index Data:
{
"name":"test"
}
{
"name":"game"
}
{
"name":"the witcher"
}
{
"name":"the witcher 3"
}
{
"name":"the"
}
Search Query:
{
"query": {
"simple_query_string" : {
"query": "-(game | novel) -(the witcher 3)",
"fields": ["name"],
"default_operator": "and"
}
}
}
Search Result:
"hits": [
{
"_index": "stof_64133051",
"_type": "_doc",
"_id": "4",
"_score": 2.0,
"_source": {
"name": "the"
}
},
{
"_index": "stof_64133051",
"_type": "_doc",
"_id": "3",
"_score": 2.0,
"_source": {
"name": "the witcher"
}
},
{
"_index": "stof_64133051",
"_type": "_doc",
"_id": "1",
"_score": 2.0,
"_source": {
"name": "test"
}
}
]

elasticsearch query for finding id in fields in json file

I have a json file that I indexed on elasticsearch and I need a query to retrieve "_id_osm". can you help me plz.
and this is one line of my json file:
{
"index": {
"_index": "pariss",
"_type": "sig",
"_id": 1
}
}{
"fields": {
"_id_osm": 416747747,
"_categorie": "",
"_name": [
""
],
"_location": [
36.1941834,
5.3595221
]
}
}
Based on the comments in the answer updated the answer,
If you have store true in your mapping for _id_osm then you can use below query to fetch the field value.
{
"stored_fields" : ["_id_osm"],
"query": {
"match": {
"_id": 1
}
}
}
Above call returns below response and you can notice the fields section in the response which contains the field name and value.
"hits": [
{
"_index": "intqu",
"_type": "_doc",
"_id": "1",
"_score": 1.0,
"fields": {
"_id_osm": [
416747747
]
}
}
]
If you don't have store true which is default, then use _source filtering to get the data.
{
"_source": [ "_id_osm" ],
"query": {
"match": {
"_id": 1
}
}
}
which returns below response, you can see _source has the data.
"hits": [
{
"_index": "intqu",
"_type": "_doc",
"_id": "1",
"_score": 1.0,
"_source": {
"_id_osm": 416747747
}
}
]

elastic search query term returns all the data

I am trying to query my ES, here is my data,
you can just run this in sense, it creates index fst and fills it with 4 items.
then you can see that it returns the wrong result number
i want only one result, as should be the case.
PUT fst/objects/ggg
{
"frameAttributes": {
"identities": [
{ "_id": "DSC00263", "_score": 0.655822},
{ "_id": "DSC00262", "_score": 0.59957 },
{ "_id": "DSC00244", "_score": 0.220819},
{ "_id": "DSC00300", "_score": 0.191191},
{"_id": "DSC00276", "_score": 0.124561}
]
}
}
PUT fst/objects/ffffff
{
"frameAttributes": {
"identities": [
{"_id": "DSC00222","_score": 0.191009},
{"_id": "DSC00261","_score": 0.146157},
{"_id": "DSC00329","_score": 0.14518},
{"_id": "DSC00225","_score": 0.12622},
{"_id": "DSC00295","_score": 0.12396}
]
}
}
PUT fst/objects/aaaa
{
"frameAttributes": {
"identities": [
{"_id": "DSC00229","_score": 0.223149},
{"_id": "DSC00240","_score": 0.178388},
{"_id": "DSC00228","_score": 0.173769},
{"_id": "DSC00257","_score": 0.166746},
{"_id": "DSC00226","_score": 0.153071}
]
}
}
put fst/objects/abcdef
{
"frameAttributes": {
"identities": [
{ "_id": "DSC00262","_score": 0.427957},
{"_id": "DSC00263","_score": 0.408772},
{"_id": "DSC00282","_score": 0.284546 },
{ "_id": "DSC00283","_score": 0.191374},
{"_id": "DSC00299", "_score": 0.165478}
]
}
}
My Query should return only one result
get fst/_search
{
"query": {
"term": {
"frameAttributes.identities._id": {
"value": "DSC00229"
}
}
}
}
You will have to set the required field as not_analyzed. In your case, the field is _id. You can do that while creating the index. For example:
PUT /gb/_mapping/tweet
{
"properties" : {
"tag" : {
"type" : "string",
"index": "not_analyzed"
}
}
}
Check this link for reference: https://www.elastic.co/guide/en/elasticsearch/guide/current/_finding_exact_values.html
Took a while
but I learned something new elastic search doesn't like you to create a fields called _id. I changed it to personId and now it just works.
Thanks all :)

Is it possible to perform user count / cardinality with logical relationship in ElasticSearch?

I have documents of Users with the following format:
{
userId: "<userId>",
userAttributes: [
"<Attribute1>",
"<Attribute2>",
...
"<AttributeN>"
]
}
I want to be able to get the number of unique users that answer a logic statement, for example How many users have attribute1 AND attribute2 OR attribute3?
I've read about the cardinality function in cardinality-aggregation but it seems to work for a single value, lacking the logic abilities of "AND" and "OR".
Note that I have around 1,000,000,000 documents and I need the results as fast as possible, this why I was looking at the cardinality estimation.
What about this attempt, considering the userAttributes as a simple array of strings (analyzed in my case, but single lowercase terms):
POST /users/user/_bulk
{"index":{"_id":1}}
{"userId":123,"userAttributes":["xxx","yyy","zzz"]}
{"index":{"_id":2}}
{"userId":234,"userAttributes":["xxx","yyy","aaa"]}
{"index":{"_id":3}}
{"userId":345,"userAttributes":["xxx","yyy","bbb"]}
{"index":{"_id":4}}
{"userId":456,"userAttributes":["xxx","ccc","zzz"]}
{"index":{"_id":5}}
{"userId":567,"userAttributes":["xxx","ddd","ooo"]}
GET /users/user/_search
{
"query": {
"query_string": {
"query": "userAttributes:(((xxx AND yyy) NOT zzz) OR ooo)"
}
},
"aggs": {
"unique_ids": {
"cardinality": {
"field": "userId"
}
}
}
}
which gives the following:
"hits": [
{
"_index": "users",
"_type": "user",
"_id": "2",
"_score": 0.16471066,
"_source": {
"userAttributes": [
"xxx",
"yyy",
"aaa"
]
}
},
{
"_index": "users",
"_type": "user",
"_id": "3",
"_score": 0.04318809,
"_source": {
"userAttributes": [
"xxx",
"yyy",
"bbb"
]
}
},
{
"_index": "users",
"_type": "user",
"_id": "5",
"_score": 0.021594046,
"_source": {
"userAttributes": [
"xxx",
"ddd",
"ooo"
]
}
}
]

Resources