Elasticsearch Nest Query not returning result as expected - c#-4.0

I'm new to Elasticsearch. I'm trying a query and when giving full name I'm getting results. When I give part of it, it's not returning any results. Below is the sample that I have been trying.
{
"query": {
"multi_match": {
"query": "recharge",
"fields": ["category.*","categoryName^3","alterNames","categoryDescription"],
"type": "best_fields"
}
},size:1000
}
If I pass "rech" in the query, I'm not getting any results. Can any one help me here?

As far as I understand, you want to get the results with unfinished query, so you need a wildcard, like this:
{
"query": {
"multi_match": {
"query": "rech*",
"fields": ["category.*", "categoryName^3", "alterNames", "categoryDescription"],
"type": "best_fields"
}
}

Related

Facing difficulty using compound query with Elasticsearch JS

I am using the official Elasticsearch package from npm within my node.js application. I was attempting to perform search using compound queries ( bool), But I found that the compound search does not work as expected.
To debug the issue, I tried passing different sets of data for the search query. I found an abnormality wherein the elasticsearch library does not work as expected but the Elasticsearch API does. I'm unable to find this behavior documented anywhere else as well.
I executed 2 sets of code (with the same query) on
1) Node using the official elastic search library
2) Over the Elasticsearch API using Postman
I> Using Elastic Search JS
"index": "bank",
"type": "account",
"body": {
"query": {
"bool": {
"must": [{
"match": {
"address": "avenue"
}
}]
}
}
}
}
II> Using Elastic Search API
"query": {
"bool": {
"must": [{
"match": {
"address": "avenue"
}
}]
}
}
}
The results for the official library come in empty (Empty array), But the results using the elasticsearch API result in the correct set of data.
Another peculiar observation was the below query using elasticsearch JS which works for a single element, but not an array of elements
"index": "bank",
"type": "account",
"body": {
"query": {
"bool": {
"must": {
"match": {
"address": "avenue"
}
}
}
}
}
}
I'm breaking my head over where I'm going wrong, I tried going through docs, stackoverflow and a very little bit of code, And returned empty handed.
Would appreciate any help.
Thanks a lot

ElasticSearch search with querystring and verify another field

I need to translate the following SQL query to ES query:
SELECT *
FROM SKILL
WHERE SKILL.name LIKE 'text' and SKILL.type = 'hard'
I have tried the following using "elasticsearch" library for python3:
query = self.__es.search(index="skills",
body={"from" : skip, "size" : limit,
"query":
{"query_string":
{"query": 'text'}
})
and this worked well. But now, I don't know how to check that the field 'type' is equal to 'hard'.
How can I do that?
Thank you.
You have to use a bool query and in the "must" part put two queries, the full text one and a term one:
{
"query": {
"bool": [{
"match": {
"name": "this is a test"
}
}, {
"term": {
"type": "hard"
}
}]
}
}
Before this you have to store the type property as a keyword field.

Couchdb 2 _find query not using index

I'm struggling with something that should be easy but it's making no sense to me, I have these 2 documents in a database:
{ "name": "foo", "type": "typeA" },
{ "name": "bar", "type": "typeB" }
And I'm posting this to _find:
{
"selector": {
"type": "typeA"
},
"sort": ["name"]
}
Which works as expected but I get a warning that there's no matching index, so I've tried posting various combinations of the following to _index which makes no difference:
{
"index": {
"fields": ["type"]
}
}
{
"index": {
"fields": ["name"]
}
}
{
"index": {
"fields": ["name", "type"]
}
}
If I remove the sort by name and only index the type it works fine except it's not sorted, is this a limitation with couchdbs' mango implementation or am I missing something?
Using a view and map function works fine but I'm curious what mango is/isn't doing here.
With just the type index, I think it will normally be almost as efficient unless you have many documents of each type (as it has to do the sorting stage in memory.)
But since fields are ordered, it would be necessary to do:
{
"index": {
"fields": ["type", "name"]
}
}
to have a contiguous slice of this index for each type that is already ordered by name. But the query planner may not determine that this index applies.
As an example, the current pouchdb-find (which should be similar) needs the more complicated but equivalent query:
{
selector: {type: 'typeA', name: {$gte: null} },
sort: ['type','name']
}
to choose this index and build a plan that doesn't resort to building in memory for any step.

Unparseable Elasticsearch query

I have data with an integer year field. I'm trying to give a weight to more recent data, so that results that would otherwise tie are sorted by the year in reverse-chronological order.
{
"query": {
"function_score": {
"functions": [
{
"gauss": {
"year": {
"origin": "2016",
"scale": "50"
}
}
}
],
"query": "This is replaced by the main query",
"boost_mode": "sum"
},
"_source": 1
}
}
I'm getting this error:
parse_exception: failed to parse search source. expected field name but got [VALUE_NUMBER]
I can't tell what I'm doing wrong, so any help is appreciated.
Thanks.
I had the "_source": 1 at the wrong nesting level. It should've been one level up.

elasticsearch: distinct token matching

tl;dr: I want to have a query that matches every token once at max
Given I have an elasticsearch index with the following words:
["stackoverflow", "overflow", "awesome", "some"]
Is there any elasticsearch query, that matches
"stackoverflow" and "awesome" on the sentence "stackoverflow community is awesome" and doesn't match "overflow" and "some"?
I can't do it with score only, because there's also a misspelling detection included.
What I'm searching for is something like a consuming matching. Unfortunately, I couldn't find anything suitable so far :(
Thanks!
more details:
The indexed documents look like this
{"name": "stackoverflow",
"type": "brand"},
{"name": "awesome",
"type": "descriptor"},
{"name": "overflow",
"type": "brand"},
{"name": "some",
"type": "descriptor"}
My query looks like this:
{
"min_score": 1,
"query": {
"match": {
"name": {
"query": "stakoverflow community is awesom",
"fuzziness": 2
}
}
},
"rescore": {
"window_size": 10,
"query": {
"rescore_query": {
"match": {
"name": "stakoverflow community is awesom"
}
},
"query_weight": 0.9,
"rescore_query_weight": 1.1
}
}
}
So I basically try to catch misspellings in the first query and prefer non-misspelling in the rescore.
What I'd like to achieve:
For each token, I'd like to have at most 1 match:
INPUT stakoverflow community is awesom
OUTPUT stackoverflow <nothing> <nothing> awesome
My problem is, that I also get overflow and some returned. Overflow might even have a better score than awesome, because its not a misspelling.

Resources