I have this document in my elastic DB.I am using this package
https://www.npmjs.com/package/#elastic/elasticsearch
I am searching on title.
I am doing like this.
const result= await client.search({
index: 'products',
"query": {
"bool": {
"should": [
{
"wildcard": { "title": "*" + search + "*" }
}
],
"minimum_should_match": 1
}
}
when I search "title" this issue . it give me one result but When I am seacrhing i have issue . it is giving zero or 0 result.why ? is it possible to fetch that data.issue keyword is present in first collection why it is not picking ?
[
{
"_index": "products",
"_id": "wZRh3n8Bs9qQzO6fvTTS",
"_score": 1.0,
"_source": {
"title": "laptop issues",
"description": "laptop have issue present in according"
}
},
{
"_index": "products",
"_id": "wpRh3n8Bs9qQzO6fvzQM",
"_score": 1.0,
"_source": {
"title": "buy mobile",
"description": "mobile is in Rs 250"
}
},
{
"_index": "products",
"_id": "w5Rh3n8Bs9qQzO6fvzTz",
"_score": 1.0,
"_source": {
"title": "laptop payment",
"description": "laptop payment is given in any way"
}
}
]
how to search on keywords? any keyword match . I need to pick that whole colloection
If you want to match only some of the word from query then you can use match query with operator set to or.
{
"query": {
"match": {
"title": {
"query": "i have issues",
"operator": "or"
}
}
}
}
Update
To run the query which you mentioned in comment, You can use match_bool_prefix query.
{
"query": {
"match_bool_prefix": {
"title": {
"query": "i have issu"
}
}
}
}
Related
I have the following structure of Student collection in MongoDb:
{
"_id": "st1",
"student_courses": [
{
"_id": "c1",
"course_name": "Node",
"image": [
{
"_id": "c1img1",
"image": "img1.jpeg"
}
]
},{
"_id": "c2",
"course_name": "React",
"image": [
{
"_id": "c2img2",
"image": "img2.jpeg"
}
]
}
],
}
Now, I want to update the image name of img1.jpeg in all the documents that have same image name. So what I am doing is this:
Student.updateMany(
{ "student_courses._id": "c1"},
{
$set: {
"student_courses.$.course_name": "Node Crash Course",
"student_courses.$.image[0].image": complete_image_name,
},
}
);
Unexpectedly, this is updating course_name field but image. I have tried using $ positional argument instead of [0] but got the error Too many positional arguments ...... I don't know how to do that. My expected output should look like this:
{
"_id": "st1",
"student_courses": [
{
"_id": "c1",
"course_name": "Node Crash Course",
"image": [
{
"_id": "c1img1",
"image": "complete_image_name.jpeg"
}
]
},
:::::::::::::::::::
:::::::::::::::::::
],
}
{
"_id": "st2",
"student_courses": [
{
"_id": "c1",
"course_name": "Node Crash Course",
"image": [
{
"_id": "c1img1",
"image": "complete_image_name.jpeg"
}
]
},
:::::::::::::::::::
:::::::::::::::::::
],
}
Moreover, I have implemented almost every method posted in similar questions. Thanks in advance for any help.
Try to change the way you are referring to the first element in the array:
Student.updateMany(
{ "student_courses._id": "c1"},
{
$set: {
"student_courses.$.course_name": "Node Crash Course",
"student_courses.$.image.0.image": complete_image_name,
},
}
);
Need to count distinct tags on all registers in a mongodb query, using JS.
Register structure:
{
"_id": {
"$oid": "62e593aed8fd9808777225e8"
},
"title": "“The world as we have created it is a process of our thinking. It cannot be changed without changing our thinking.”",
"author": {
"name": "Albert Einstein",
"url": "https://quotes.toscrape.com/author/Albert-Einstein"
},
"tag": [
"change",
"deep-thoughts",
"thinking",
"world"
]
}
This could be useful. In addition to get the different values for the field, it returns the number of appearances:
db.collection.aggregate([
{
"$unwind": "$tag"
},
{
"$group": {
"_id": "$tag",
"total": {
"$sum": 1
}
}
},
])
You can try here: https://mongoplayground.net/p/yXLYkJKO3Wf
I have the following data:
makeStr: xerox
modelStr: Designjet 1050C
I want it to match
xerox
Designjet 1050C Plus Printer
but it is matching
canon
DesignJet 1050C
and currently I have this query
"query": {
"bool": {
"should":
{
"multi_match": {
"query": modelStr,
"type": "most_fields",
"fields": ['model.alphanum']
}
}
,
"filter": [
{
"match": {
"make.blur": makeStr
}
},
{
"match": {
"model.blur": modelStr
}
}
]
}
},
"functions": [{
"field_value_factor": {
"field": "isMpsSupported",
"factor": 1,
"missing": 0
}
}],
"boost_mode": "sum"
}
How do I give preference for makeStr such that it considers both makeStr and modelStr during search.
More preference can be given by using boost. Refer here
Something like makeStr^2 should work.
How can i create body for elasticsearch like this
select * from table where full_name like '%q%' or address like '%q%' or description like '%q%' order by full_name , description , address
A wildcard query can be very expensive, especially if you search in several fields. The right way to do this is by using an nGram token filter on the fields you want to search only a part of.
First you create an index like below with a custom analyzer that will slice and dice your fields into searchable tokens:
curl -XPUT localhost:9200/tests -d '{
"settings": {
"analysis": {
"analyzer": {
"substring_analyzer": {
"tokenizer": "standard",
"filter": ["lowercase", "substring"]
}
},
"filter": {
"substring": {
"type": "nGram",
"min_gram": 1,
"max_gram": 15
}
}
}
},
"mappings": {
"test": {
"properties": {
"full_name": {
"type": "string",
"analyzer": "substring_analyzer"
},
"address": {
"type": "string",
"analyzer": "substring_analyzer"
},
"description": {
"type": "string",
"analyzer": "substring_analyzer"
}
}
}
}
}'
Then you can index a few docs:
curl -XPUT localhost:9200/tests/test/_bulk -d '
{"index":{"_id": 1}}
{"full_name": "Doe", "address": "1234 Quinn Street", "description": "Lovely guy"}
{"index":{"_id": 2}}
{"full_name": "Brennan", "address": "4567 Main Street", "description": "Not qualified"}
{"index":{"_id": 3}}
{"full_name": "Quantic", "address": "1234 Quinn Street", "description": "New friend"}
'
Finally, you can search with a query equivalent to your SQL query above and all three test documents will match:
curl -XPUT localhost:9200/tests/test/_search -d '{
"query": {
"bool": {
"should": [
{
"match": {
"full_name": "q"
}
},
{
"match": {
"address": "q"
}
},
{
"match": {
"description": "q"
}
}
]
}
}
}'
You can try the following. . .
https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-wildcard-query.html
`
{
"wildcard" : { "user" : "ki*y" }
}
`
I'm trying to query my MongoDB structure, specifically the users table to get a specific field, address, but am having difficulty because I'm not sure how to access something a couple levels in users > emails > address:
{
"_id": "BkWk7hq4MRyMAyK4mm",
"createdAt": ISODate("2015-11-15T19:46:41.633Z"),
"services": {
"password": {
"bcrypt": "$2a$10$voVzU3pIVZBd1bfJf1oX4.OMPnzi8zXawYY5REtovPayBJL7dZLWSC"
},
"resume": {
"loginTokens": []
}
},
"emails": [{
"address": "brutus#example.com",
"verified": false,
"provides": "default"
}],
"roles": {
"J8Bhq3uTtdgwZdx3rz": ["guest", "account/profile"]
}
} {
"_id": "3qfCgFz9r5wKjnmymQ",
"createdAt": ISODate("2015-12-15T19:49:05.236Z"),
"emails": [],
"roles": {
"J8aBhq3uTtdgwZx3rz": ["anonymous", "guest"]
},
"services": {
"resume": {
"loginTokens": [{
"when": ISODate("2015-12-15T19:49:05.280Z"),
"hashedToken": "c1ybS3U3+GeC8ZNzGQ3WOctWpudQvv4vND6EzlRygtCQ="
}]
}
}
}
I was trying to use the following:
db.users.find( { emails: { address : "brutus#example.com" } } )
You can query nested objects with dot notation.
db.users.find( { 'emails.address' : "brutus#example.com" } )
Your query is correct but it is querying documents for a complete match. So if your emails field had only address field it would work.
With dot notation you can check just one embedded field for matching.
Take a look at the documentation.