Elasticsearch, how to get a search result in all circumstances - javascript

I've been dealing with a project. My goal is to get result from search engine in all circumstances for example, although i enter a keyword which is not include the keys inside data or is a empty string, I still need to get some result.How can i reach my goal?
you can see the query below :
query: {
regexp: {
title: "something to not found .*",
},

Try use "prefix" or "query_string"
You also can use title.keyword for exact value
1 -
{
"query": {
"prefix": {
"title": {
"value": "<data>"
}
}
}
}
2 -
{
"query": {
"query_string": {
"default_field": "title",
"query": "<data>*^0"
}
}
}

Related

google docs api documents.get multiple fields ? (nodejs)

How can I select multiple fields when using the documents.get ?
Right now I am getting the documenmt like this:
const doc = await docs.documents.get({
documentId: copiedFile.data.id,
fields: 'body/content'
});
which returns this:
"data": {
"body": {
"content": [...]
}
}
However, I need to also get the inlineObject and the only way so far I have been able to do so, is by removing the fields proeprty completely
const doc = await docs.documents.get({
documentId: copiedFile.data.id,
});
Then I get this:
"data": {
"title": "Some document title",
"body": {
"content": [...]
},
"headers": {
},
"footers": {
},
"documentStyle": {
},
"namedStyles": {
},
"lists": {
},
"revisionId": "some-long-id",
"suggestionsViewMode": "SUGGESTIONS_INLINE",
"inlineObjects": {
},
"documentId": "some-long-id"
}
But I am really only interested in data.body.content and data.inlineObjects
When selecting everything the response is many thousands of lines of json larger, which I don't want.
I have tried fields: ['body/content', 'inlineObjects'] but that only returns body.content and not the inlineObjects - also the documentation doesn't mention this syntax anywhere, it was just to experiment.
I think it doesn't return any inlineObjects when you don't have any inlineObjects in the document. To confirm if the actual format is working and the statement above is true, try using other fields where a value is confirmed to be returned such as revisionId or title.
Test:
const doc = await docs.documents.get({
documentId: copiedFile.data.id,
fields: 'body/content,inlineObjects'
});
Output:

How do I query an index properly with Dynamoose

I'm using Dynamoose to simplify my interactions with DynamoDB in a node.js application. I'm trying to write a query using Dynamoose's Model.query function that will search a table using an index, but it seems like Dynamoose is not including all of the info required to process the query and I'm not sure what I'm doing wrong.
Here's what the schema looks like:
const UserSchema = new dynamoose.Schema({
"user_id": {
"hashKey": true,
"type": String
},
"email": {
"type": String,
"index": {
"global": true,
"name": "email-index"
}
},
"first_name": {
"type": String,
"index": {
"global": true,
"name": "first_name-index"
}
},
"last_name": {
"type": String,
"index": {
"global": true,
"name": "last_name-index"
}
}
)
module.exports = dynamoose.model(config.usersTable, UserSchema)
I'd like to be able to search for users by their email address, so I'm writing a query that looks like this:
Users.query("email").contains(query.email)
.using("email-index")
.all()
.exec()
.then( results => {
res.status(200).json(results)
}).catch( err => {
res.status(500).send("Error searching for users: " + err)
})
I have a global secondary index defined for the email field:
When I try to execute this query, I'm getting the following error:
Error searching for users: ValidationException: Either the KeyConditions or KeyConditionExpression parameter must be specified in the request.
Using the Dynamoose debugging output, I can see that the query winds up looking like this:
aws:dynamodb:query:request - {
"FilterExpression": "contains (#a0, :v0)",
"ExpressionAttributeNames": {
"#a0": "email"
},
"ExpressionAttributeValues": {
":v0": {
"S": "mel"
}
},
"TableName": "user_qa",
"IndexName": "email-index"
}
I note that the actual query sent to DynamoDB does not contain KeyConditions or KeyConditionExpression, as the error message indicates. What am I doing wrong that prevents this query from being written correctly such that it executes the query against the global secondary index I've added for this table?
As it turns out, calls like .contains(text) are used as filters, not query parameters. DynamoDB can't figure out if the text in the index contains the text I'm searching for without looking at every single record, which is a scan, not a query. So it doesn't make sense to try to use .contains(text) in this context, even though it's possible to call it in a chain like the one I constructed. What I ultimately needed to do to make this work is turn my call into a table scan with the .contains(text) filter:
Users.scan({ email: { contains: query.email }}).all().exec().then( ... )
I am not familiar with Dynamoose too much but the following code below will do an update on a record using node.JS and DynamoDB. See the key parameter I have below; by the error message you got it seems you are missing this.
To my knowledge, you must specify a key for an UPDATE request. You can checks the AWS DynamoDB docs to confirm.
var params = {
TableName: table,
Key: {
"id": customerID,
},
UpdateExpression: "set customer_name= :s, customer_address= :p, customer_phone= :u, end_date = :u",
ExpressionAttributeValues: {
":s": customer_name,
":p": customer_address,
":u": customer_phone
},
ReturnValues: "UPDATED_NEW"
};
await docClient.update(params).promise();

How do you check if value exists and then either add or update values in firebase with JavaScript?

I have an app where users can log in and add trips they've been on. my fb db structure is like so
{
"users": {
"randomId": {
"Place1": {
"name": "Place1",
"somedata": "oianoiasnfianafs",
"moredata": "asdasdadasdas",
"THISKEYIWANTTOUPDATE": [
{
"data": "asasfas"
}
]
}
}
}
}
you can see the key above I want to update. but my problem is, I first need to check if Place1 already exists in the database? how do I achieve this? I know I could call set but not sure if this is the best thing to do? I'm also aware of update but I want logic like this pseudo code below
if (place1) {
// call update()
} else {
// cat set()
}
I'm not sure how to do the if(place1) part though? and if it's best practice in firebase to check if it exists?
You could try use Object.keys(obj) to get attribute of object.
assume that userObj is
"users": {
"randomId": {
"Place1": {
"name": "Place1",
"somedata": "oianoiasnfianafs",
"moredata": "asdasdadasdas",
"THISKEYIWANTTOUPDATE": [
{
"data": "asasfas"
},
]
},
}
}
and you have mentioned that it could be mulitple PlaceN under randomID, then I suggest that the pattern might be :
multiple case, have Place2 and value of Place1 is null:
"randomId": {
"Place1": {
},
"Place2": {
"name": "Place2",
"somedata": "oianoiasnfianafs",
"moredata": "asdasdadasdas",
"THISKEYIWANTTOUPDATE": [
{
"data": "asasfas"
},
]
},
}
and the check function:
checkPlace1ExistOrNot=(userObj)=>{
let tempRandomId = userObj[Object.keys(userObj)[0]] //It should be vale of "RandomId"
if(tempRandomId[Object.keys(tempRandomId)[0]])
{
return true // users.randomId.Place1 is exist
}
else
{
return false // users.randomId.Place1 is not exist
}
}

graphQl - Argument has invalid value Expected \"contentfulStranNaslovQueryString_2\", found not an object

I am trying to query some data from a contentful API using gatsby's built-in graphiQL.
EDIT: after a suggestion from the comments, I made a introspection query to get the schema information:
{
"name": "contentfulStranNaslovQueryString_2",
"kind": "INPUT_OBJECT"
}
When I run this query:
{
contentfulStran {
id
naslov
}
}
I get the expected result (the first entry for the data model):
{
"data": {
"contentfulStran": {
"id": "c2tD44y2tDe8QC4yqkwMOgo",
"naslov": "Novice"
}
}
}
But now I would like to pass in a query parameter that only gets data specified on the naslov field. I tried this:
{
contentfulStran(naslov: "Ponudba") {
id
naslov
}
}
But I am getting the following error:
{
"errors": [
{
"message": "Argument \"naslov\" has invalid value \"Ponudba\".\nExpected \"contentfulStranNaslovQueryString_2\", found not an object.",
"locations": [
{
"line": 2,
"column": 27
}
]
}
]
}
What am I doing wrong?
Miha answered his own question in the comments. The correct way to filter is:
{
contentfulStran(naslov: {eq: "Ponudba"})
{
id
naslov
zaporedje
tekst
{
tekst
}
}
}
Note the {eq: "param"} object instead of just giving the param.

Elastic Search query for email

I am trying to implement email search with ElasticSearch.
Here are the example documents.
{
...
"email": "valeri#gmail.com"
},
{
...
"email": "tom#gmail.com"
}
So when I use the match query: { "match": { "email": "valeri#gmail.com"
} }
I get both of "valeri#gmail.com"and "tom#gmail.com" but the result must be only "valeri#gmail.com".
I think it is because of # character.
Any good solution to resolve this issue?
You need to use the Email Tokenizer as specified here:
https://www.elastic.co/guide/en/elasticsearch/reference/current/analysis-uaxurlemail-tokenizer.html
Use this to make your request 👍
GET my_index/_search
{
"query": {
"match_phrase_prefix" : {
"email": "valery#gmail.com"
}
}
}
You will have the expecting result

Categories