Alphabetical sort using elasticsearch - javascript

I wounder if there any way or setting to perform an alphabetical sort in elasticsearch. I've got a field and I want to perform sort in descending order over it. Elastic performs it lexicographically. What I get:
Company name
Customer name
company address
What I want to get:
Company name
company address
Customer name
I found that I can create a custom analyser, but maybe there can be a better option?

use multifields to index the text field as lowercased keyword with fielddata true where you can sort.
{
"settings": {
"analysis": {
"analyzer": {
"keyword_lowercase": {
"tokenizer": "keyword",
"filter": ["lowercase"]
}
}
}
},
"mappings": {
"my_type": {
"properties": {
"text": {
"type": "text",
"fields": {
"raw": {
"type": "text",
"analyzer": "keyword_lowercase",
"fielddata": true
}
}
}
}
}
}
}
Query
{
"sort": [
{
"text.raw": {
"order": "asc"
}
}
]
}

Related

Graphql - How to get type (String, Int, ID etc..) of a selected field

I have the following graphql query
const GET_USER = gql`
query user() {
user {
firstName
lastName
}
}
`
I traverse through the query to find the type of the firstName & the lastName field using visit function provided by graphql
visit(GET_USER, {
Field(node) {
console.log(node)
}
}
it looks like the fields contain only the following information.
{
kind: 'Field',
alias: undefined,
name: { kind: 'Name', value: 'firstName' },
arguments: [],
directives: [],
selectionSet: undefined
},
{
kind: 'Field',
alias: undefined,
name: { kind: 'Name', value: 'lastName' },
arguments: [],
directives: [],
selectionSet: undefined
}
which doesn't tell me the type of firstName & lastName
I am expecting that I will probably have to make use of the related schema file to get the types of that query but I am not sure how to do that, if anyone can help me with that, that would be great.
Use Introspection
Not sure what platform you're using but introspection is what you use to get information on a particular type.
Introspection is the ability to query which resources are available in the current API schema.
Example for User type:
{
__type(name: "User") {
name
kind
}
}
Example Response:
{
"data": {
"__type": {
"name": "User",
"kind": "OBJECT"
}
}
}
You can use introspection to drill into fields as well:
{
__type(name: "User") {
name
fields {
name
type {
name
kind
}
}
}
}
Example Response:
{
"data": {
"__type": {
"name": "User",
"fields": [
{
"name": "id",
"type": {
"name": null,
"kind": "NON_NULL",
"ofType": {
"name": "ID",
"kind": "SCALAR"
}
}
},
{
"name": "name",
"type": {
"name": null,
"kind": "NON_NULL",
"ofType": {
"name": "String",
"kind": "SCALAR"
}
}
}
]
}
}
}

Exact search with ElasticSearch 7.x

I am trying to find an exact search for an url with ElasticSearch ("#elastic/elasticsearch": "^7.5.0").
I have configured my mapping like so:
const schema = {
userId: {
type: "keyword"
},
url: {
type: "keyword",
index: false,
analyzer: 'keyword'
},
pageTitle: {
type: 'text',
},
pageText: {
type: 'text',
}
};
await client.indices.putMapping({
index,
type,
include_type_name: true,
body: {
properties: schema
}
})
I have tried different queries, and they looks like this:
body: {
query: {
bool: {
must: {
match: {
query: 'test stack',
analyzer: 'keyword',
}
}
}
}
}
Or second attempt:
body: {
query: {
constant_score: {
filter: {
bool: {
must: {
term: {
url: 'test stack'
}
}
}
}
},
}
}
None of them work. I want to get only the results where the exact string 'test/stack' is found. Any help would be highly appreciated.
Example of data I'm trying to add:
[
{"url": "test stack",
"userId": "anotherTest",
"pageTitle": "not important",
"pageText": "not important",
"log": [1, 3, 7]
},
{"url": "test stack",
"userId": "anotherTest",
"pageTitle": "not important",
"pageText": "not important",
"log": [1, 3, 7]
},
{"url": "test stack",
"userId": "anotherTest",
"pageTitle": "not important",
"pageText": "not important",
"log": [1, 3, 7]
}
]
Thanks.
I managed to make this work. Steps are:
1. Delete the index.
2. Delete the custom mapping function.
3. Create the index (with client.indices.create)
4. Index the first item (with client.index).
5. At this point, you can check in postman the dynamic mappings created by ElasticSearch (only visible after 1st item is indexed, by what I could tell). You can make a get request at http://localhost:9200/history/_mappings, and the response should look something like this:
{
"history": {
"mappings": {
"properties": {
"fullTitle": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"log": {
"properties": {
"startTime": {
"type": "long"
},
"timeSpent": {
"type": "long"
}
}
},
"protocol": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"text": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"totalTimeSpent": {
"type": "long"
},
"totalVisits": {
"type": "long"
},
"url": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"userId": {
"type": "long"
}
}
}
}
}
As you can see, any field indexed as text has attached another field, called keyword, which can be used for exact matches.
6. The query to get the exact matches looks like this:
const result = await esClient.search({
index: 'history',
body: {
query: {
term: {
'url.keyword': {
value: toInsert.url
}
}
}
}
})
At this point you should receive results only in case of exact match for the field "url" in my case. Hope this helps somebody else. Thanks #ibexit for trying to help me.
I see two problems:
The mapping defined for the url field says
url: {
type: "keyword",
index: false,
analyzer: 'keyword'
},
If you define index: false, the field will not be searchable at all. Using the following mapping should work properly:
url: {
type: "keyword"
}
See https://www.elastic.co/guide/en/elasticsearch/reference/current/keyword.html for more detailed information
The keyword mapped fields will not match using the match query which is designed to query text fields. Please use the term query instead for keyword fields. Please notice the example below using the Elasticseaech Query API:
GET /_search
{
"query": {
"term": {
"url": { <<= the field to search
"value": "test stack" <<= the searched value
}
}
}
}
Here is the according documentation: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-term-query.html
BTW: keep in mind that you need to reindex the data after a mapping change

How to add preference to mutliple fields in elastic search query?

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.

Best way to filter if a field present in nested object in elasticsearch exists?

I have 1,000,000 contacts which are from different groups. For example
{"gps_id": [{"gid": "G1"},{"gid": "G2"}],"is_active": true,"contact": "c1"}
{"gps_id": [{"gid": "G2"}],"is_active": true,"contact": "c2"}
....
{"gps_id": [{"gid": "G1"},{"gid": "G2"}],"is_active": true,"contact": "c1000000"}
Consider G1 has 500,000 contacts, G2 has 1,000,000 contacts out of it 500,000 contacts which are already present in G1.
I want to filter above document object based on the condition,
"fetch unique contact from all respective group by group id".
I tried Elastic script query as below. But it doesn't work:
{
"query": {
"bool": {
"must" : {
"script" : {
"script" : {
"inline": "for (int i = 0; i < params.gps_id.length; ++i) {ctx._source.gps_id.add(params.gps_id[i]) }",
"lang": "painless",
"params": {
"gps_id": [
{
"gid": "G1"
},
{
"gid": "G2"
}
]
}
}
}
},
"must": [
{
"match": {
"is_active": true
}
},
{
"nested": {
"path": "gps_id",
"query": {
"bool": {
"must": [
{
"match": {
"gps_id.gid": "G1"
}
}
]
}
}
}
}
]
}
}
}
Here Group and its contact may increase in size.
Please suggest best way to implement it using Elasticsearch -5.1.2

Nested Query/Select for MongoDB

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.

Categories