I have a hard time translating aggregation query for elastic search into elastic.js. I am reading the documentation but I just can not figure it out. And the examples that you can find online are mostly about deprecated facets feature, that is not very useful.
The JSON for example aggregation is as follows:
{
"aggs": {
"foo": {
"filter": {
"bool": {
"must": [
{
"query": {
"query_string": {
"query": "*"
}
}
},
{
"terms": {
"shape": [
"wc"
]
}
}
]
}
},
"aggs": {
"field": {
"terms": {
"field": "shape",
"size": 10,
"exclude": {
"pattern": []
}
}
}
}
}
},
"size": 0
}
This is how you would nest terms aggregation into filter aggregation with elasticjs
ejs.Request()
.size(0)
.agg(ejs.FilterAggregation("foo").filter(ejs.BoolFilter()
.must(ejs.TermsFilter('shape', 'wc'))
.must(ejs.QueryFilter(ejs.QueryStringQuery().query("*"))))
.agg(ejs.TermsAggregation("field").field("shape").size(10).exclude("my_pattern"))
)
BTW you are filtering on shape and then doing aggregations on it. I am not sure what exactly you are trying.
I found their documentation pretty good, Also they have a great tool to check if your query is valid and right. This would help you a lot
Hope this helps!!
It seems like you have misplaced your query under the aggs element. Try this:
{
"size": 0,
"query": {
"bool": {
"must": [
{
"query": {
"query_string": {
"query": "*"
}
}
},
{
"terms": {
"shape": [
"wc"
]
}
}
]
}
},
"aggs": {
"foo": {
"terms": {
"field": "shape",
"size": 10,
"exclude": {
"pattern": []
}
}
}
}
}
Related
Given the ElasticSearch document below:
{
"_id": "3330481",
"_type": "user",
"_source": {
"id": "3330481",
"project": "Cool_Project_One"
}
}
I'm building a UI component that will auto suggest to the user all the values in "project" field base on his text input
For example:
As the user types "Cool" i would like to show him all the values from the "project" field that starts with "Cool"
I've create this aggregation:
"aggs": {
"projects": {
"terms": {
"field": "project",
"size": 2
}
}
}
which returns me a list with all the values for the project field, but i can't understand how should i find only the values that are matching to a certain expression.
I've found this answer that shows how to add filter, but it seems that the filter returns only exact matches as i tried to do that:
{
"aggs": {
"projects": {
"filter": {
"term": {
"project": "Cool"
}
},
"aggs": {
"terms": {
"field": "project",
"size": 2
}
}
}
}
}
And it didn't worked.
Any help would be appreciated
Some notes:
term query will look for exact matches (casing included, if you want something more flexible you can use a regular "match" query
In general, you want to add the filter in the query, no aggregations
You can use aggregations to group by category type fields, but you can use regular queries to match against title type fields
I would suggest to use a suggestion field type for this, to also capture prefixes (proj in project por example).
https://www.elastic.co/guide/en/elasticsearch/reference/current/search-as-you-type.html
I just seen you have _type defined, so probably you are using an old version of Elasticsearch, and search_as_you_type is relatively new. I will add some examples with both query and aggs:
Mappings for search_as_you_type, text, and keyword fields :
PUT test_suggestions
{
"mappings": {
"properties": {
"project": {
"type": "text",
"fields": {
"suggestions": {
"type": "search_as_you_type"
},
"keyword": {
"type": "keyword"
}
}
}
}
}
}
Indexing document
POST test_suggestions/_doc
{
"project": "Cool Project"
}
search_as_you_type query, supports prefixes out of the box:
GET test_suggestions/_search
{
"query": {
"multi_match": {
"query": "coo",
"type": "bool_prefix",
"fields": [
"project",
"project._2gram",
"project._3gram"
]
}
},
"aggs": {
"project_categories": {
"terms": {
"field": "project.keyword",
"size": 10
}
}
}
}
Regular query, case insensitive and you can write just a portion of the field and will work
GET test_suggestions/_search
{
"query": {
"match": {
"project": "Cool"
}
},
"aggs": {
"project_categories": {
"terms": {
"field": "project.keyword",
"size": 10
}
}
}
}
Bonus: prefix search without search_as_you_type or setting up ngrams:
GET test_suggestions/_search
{
"query": {
"match_phrase_prefix": {
"project": "coo"
}
},
"aggs": {
"project_categories": {
"terms": {
"field": "project.keyword",
"size": 10
}
}
}
}
I'm trying to write a GraphQL query to fetch data from an AWS DataExchange provider based on their company's API specification.
However, every query I've written so far isn't returning any data.
I've verified that all my AWS credentials and IAM keys are correct.
Could you please help me write the correct query based on the API specification given by the data provider.
My current request 'body' looks like below:
Body: JSON.stringify({
content: {
"application/graphql": {
"schema": {
"$ref": "#/components/schemas/CatalogSearchRequest"
},
query: `query availablemetadatarecord(limit: 10 content: { catalogProperties: { vendor: { eq: "EYE" } } }) {
assignedId
}
}`
}
}
}),
The DataExchange provider's actual API specification looks like below.
{
"openapi": "3.0.1",
"info": {
"title": "image-services",
"description": "API Gateway for integration of Platform Services into AWS Data Exchange",
"version": "2022-03-03T20:04:18Z"
},
"servers": [
{
"url": ""
}
],
"paths": {
"/psdm/graphql": {
"post": {
"requestBody": {
"description": "GraphQL query for the Catalog in the form of:\n\n `{\n availablemetadatarecord(\n QUERYPARAMS\n ) { \n FIELDLIST\n } \n }`\n",
"content": {
"application/graphql": {
"schema": {
"$ref": "#/components/schemas/CatalogSearchRequest"
},
"examples": {
"Limit returned data": {
"description": "Limit the number of records in a response.\n\nIn your query, simply specify `limit: <int>` \n",
"value": "{\n availablemetadatarecord(\n limit: 10\n content: { catalogProperties: { vendor: { eq: \"EYE\" } } }\n ) {\n assignedId\n }\n}\n"
}
}
}
},
"required": true
},
"responses": {
"200": {
"description": "200 response",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/CatalogSearchResponse"
}
}
}
}
},
"security": [
{
"sigv4": []
}
]
}
}
},
"components": {
"schemas": {
"CatalogSearchRequest": {
"title": "CatalogSearchRequest",
"type": "object"
},
"CatalogSearchResponse": {
"title": "Catalog Search Response",
"required": [
"data",
"paginationInfo"
],
"type": "object",
"properties": {
"paginationInfo": {
"type": "object",
"properties": {
"recordsInPage": {
"type": "integer"
},
"nextOffset": {
"type": "integer"
}
}
},
"data": {
"type": "object",
"properties": {
"availablemetadatarecord": {
"type": "array",
"description": "Records returned by query",
"items": {
"type": "object"
}
}
}
}
}
}
},
"securitySchemes": {
"sigv4": {
"type": "apiKey",
"name": "Authorization",
"in": "header",
"x-amazon-apigateway-authtype": "awsSigv4"
}
}
}
}
I am trying to add a search function for users in my app. When I type the word "Josh" in the search bar people with the name "Joshua" do not show up. I have type the full name "Joshua". How can I add this look-ahead functionality?
Here is my current query:
"bool": {
"must": [
{
"multi_match": {
"fields": [
"first",
"first.edge",
"last",
"last.edge",
"title",
"title.edge"
],
"query": search,
"fuzziness": 1,
}
}
]
}
One way to solve it is using ngram.
PUT ngram_custom_example
{
"settings": {
"analysis": {
"analyzer": {
"ngram_analyzer": {
"tokenizer": "standard",
"filter": [ "3_5_grams" ]
}
},
"filter": {
"3_5_grams": {
"type": "edge_ngram",
"min_gram": 2,
"max_gram": 5
}
}
}
},
"mappings": {
"properties": {
"name":{
"type": "text",
"analyzer": "ngram_analyzer"
}
}
}
}
POST ngram_custom_example/_bulk
{"index":{}}
{"name":"josh"}
{"index":{}}
{"name":"joshua"}
GET ngram_custom_example/_search
{
"query": {
"bool": {
"must": [
{
"multi_match": {
"fields": [
"name"
],
"query": "josh"
}
}
]
}
}
}
while running script in elastic search, I got 504 Gateway timeout error.
{
"query": {
"bool": {
"filter": {
"script": {
"script": " doc['creted_date'].date.getMonthOfYear() == 12 "
}
}
}
},
"aggs": {
"test": {
"date_histogram": {
"field": "creted_date",
"interval": "month",
"format": "MMM"
},
"aggs": {
"cost": {
"sum": {
"field": "cost"
}
}
}
}
}
}
Error result :
{
"statusCode": 504,
"error": "Gateway Time-out",
"message": "Client request timeout"
}
whenever I am running this script over index having small number of documents , it gives perfect output. but on index having large number of documents , it gives above error.
Can we manually set the timeout for request in elastic search ? or Is there any other solution for this problem ?
Try this for Elasticsearch 6.x.
{
"query": {
"bool": {
"filter": {
"script": {
"script": {
"source": "doc['created_on'].date.getMonthOfYear() == params.month",
"params": {
"month": 5
}
}
}
}
}
}
}
Try this.
{
"query": {
"bool": {
"filter": {
"script": {
"lang": "expression",
"script": "doc['creted_date'].getMonth() == month-1",
"params": {
"month": 12
}
}
}
}
}
}
Is it possible to analyze a field that has already been analyzed?
For example, suppose we broke down /Health & Beauty/Vitamins & Supplements/Supplements into the following using a custom analysis with a hierarchical token:
/Health & Beauty
/Health & Beauty/Vitamins & Supplements
/Health & Beauty/Vitamins & Supplements/Supplements
Would it be possible to then run a separate analysis on each new string and store the results with the corresponding string?
How would we do that with the following mapping:
PUT /my_index
{
"settings": {
"analysis": {
"analyzer": {
"path-analyzer": {
"type": "custom",
"tokenizer": "path-tokenizer"
},
"url-analyzer": {
"type": "custom",
"char_filter" : ["urlFormat"],
"filter": ["lowercase"],
"tokenizer": "path-tokenizer"
},
"cat-analyzer": {
"type": "custom",
"char_filter" : ["catName"],
"tokenizer": "keyword"
}
},
"char_filter" : {
"urlFormat":{
"type":"pattern_replace",
"pattern":"[^a-z|A-Z|/]+",
"replacement":"-"
},
"catName":{
"type":"pattern_replace",
"pattern":"[^/]+/",
"replacement":""
}
},
"tokenizer": {
"path-tokenizer": {
"type": "path_hierarchy"
}
}
}
},
"mappings": {
"my_type": {
"dynamic": "strict",
"properties": {
"group_path": {
"type": "string",
"index_analyzer": "url-analyzer",
"search_analyzer": "keyword",
"fields": {
"name": {
"type": "string",
"index_analyzer": "cat-analyzer",
"search_analyzer": "keyword"
}
}
}
}
}
}
}
Thank you for your consideration