Override search requirement in Elasticsearch - elasticsearch

We search on the following fields in our index:
individual_name (string)
organisation_name (string)
profile (string)
locations (string)
nationwide (boolean)
If a user searches for "optometrist" in "Hamilton", and an optometrist in our index has listed themselves as "nationwide" (but not specifically in Hamilton), desired behaviour is that the optometrist would show up with the Hamilton results - effectively ignoring the location requirement.
We're currently running a multi_match query, an example of which is below.
{
"query": {
"filtered" : {
"query" : {
"multi_match": {
"query": "optometrist",
"zero_terms_query": "all",
"operator": "and",
"fields": [
"individual_name^1.2",
"organisation_name^1.5",
"profile",
"accreditations"
]
}
},
"filter": {
"and": [{
"term": {
"locations" : "hamilton"
}
}],
}
}
}
}
How can this be modified so documents with "nationwide": "yes" are returned for this query, regardless of location?
I've tried an or query under the and, but of course that ignored the multi_match.

I think this will give you the desired results:
{
"query": {
"filtered" : {
"query" : {
"multi_match": {
"query": "optometrist",
"zero_terms_query": "all",
"operator": "and",
"fields": [
"individual_name^1.2",
"organisation_name^1.5",
"profile",
"accreditations"
]
}
},
"filter": {
"or": [
{"term": {"locations" : "hamilton"}},
{'term' : {'nationwide' : 'yes'}}
],
}
}
}
}

Related

Elasticsearch: Minimum_should_match donĀ“t return correctly

I'm new to the elastic universe and I have a question about a query. I'll try to describe it here:
I have a document called 'store' with several stores registered and within each store item a list of customers:
loja {
nome,
telefone,
email,
clientes : [
{
nomeCliente,
telefone,
email
}
]
}
I need a query where I would have to return at least 1 pair of customers from the same registered store
For example:
I research 'Ana Maria', 'Sandra Maria' and 'Alberto Braz', where I would need to return the stores that have [Ana Maria and Sandra Maria] or [Ana Maria and Alberto Braz] or [Sandra Maria and Alberto Braz].
I did the search according to the dsl below, but the minimum_should_match clause is not respecting the limit of 2 m and returning results with only 1 record found.
Am I doing something wrong in the query?
Could you help me out on this one?
Query:
{
"query": {
"bool": {
"must": [
{
"nested": {
"query": {
"bool":{
"should": {
"match": {
"clientes.nomeCliente" : {
"query" : "ANA MARIA",
"type" : "phrase",
"operator": "and",
"slop" : 40
}
}
},
"should": {
"match":{
"clientes.nomeCliente" : {
"query" : "SANDRA MARIA",
"type" : "phrase",
"operator": "and",
"slop" : 40
}
}
},
"should": {
"match":{
"clientes.nomeCliente" : {
"query" : "ALBERTO BRAZ",
"type" : "phrase",
"operator": "and",
"slop" : 40
}
}
}
},"minimum_should_match": 2
},
"path": "clientes",
"inner_hits" : {
"size" : 10
}
}
}
]
}
}
}
For the should you need to use an array instead of an object. So, you query need to be something like this :
{
"query": {
"bool": {
"must": [
{
"nested": {
"query": {
"bool": {
"should": [
{
"match": {
"clientes.nomeCliente": {
"query": "ANA MARIA",
"type": "phrase",
"operator": "and",
"slop": 40
}
}
},
{
"match": {
"clientes.nomeCliente": {
"query": "SANDRA MARIA",
"type": "phrase",
"operator": "and",
"slop": 40
}
}
},
{
"match": {
"clientes.nomeCliente": {
"query": "ALBERTO BRAZ",
"type": "phrase",
"operator": "and",
"slop": 40
}
}
}
],
"minimum_should_match": 2
}
},
"path": "clientes",
"inner_hits": {
"size": 10
}
}
}
]
}
}
}
I could not check the parameters of the match query because I don't have the mapping and sample data. But you can check that part with your index directly.

Querying fields with AND in ElasticSearch

In my ElasticSearch document index I have a property type like
type= LOCATION | PERSON | TIME
and a text field that represents the whole document.
To search for types like LOCATION and a specific text like `Mountain View" I do like
doc.text:Mountain View AND doc.type:LOCATION
If I want to do a OR query I would use instead the query_string approach like
"query": {
"query_string": {
"query": "entity.text: (Mountain View OR Menlo Park) AND entity.type:LOCATION"
}
}
This works as well. To do AND queries, like searching for item.text having both "Mountain View" and "Menlo Park" for a item.type=LOCATION, it does not work doing like
"query": {
"query_string": {
"query": "entity.text: (California AND Nevada) AND entity.type:LOCATION"
}
}
Other attempts were:
Using bool clause with should like:
{
"query": {
"bool": {
"should": [
{ "match": { "text": "Menlo Park" }},
{ "match": { "text": "Mountain View" }}
]
}
}
}
Using cross-fields with multi_match
"query": {
"multi_match": {
"query": "California Nevada",
"type": "cross_fields",
"operator": "AND",
"fields": [
"text"
]
}
}
Another approach was using must with the latter (in this case omitting the type by the way):
{
"query": {
"bool": {
"must": [
{
"multi_match" : {
"query": "Nevada",
"type": "cross_fields",
"fields": [ "text"],
}
},
{
"multi_match" : {
"query": "California",
"type": "cross_fields",
"fields": [ "text" ]
}
}
]
}
}
}
but it returns no results neither. Note that in the last case using should instead of must will produce an OR query that will work ok.
So how to perform an AND query on the same field text to match multiple values like California and Nevada?
If I understood the question right, I would do the following:
{
"query": {
"bool" : {
"must": [
"match" : {
"text" : {
"query" : "California Nevada",
"operator" : "and"
}
}
]
}
}
}
Documentation
Hope it helps!

ElasticSearch filtered bool query not working with multi_match

I have this installed on Ubuntu running ElasticSearch 6.2.3
I am new to ES so if this is duplicate or obvious I am sorry.
I just need to do a contains query that filters out items not marked as PUBLISHED.
Here is what i think should be returning the correct results but it returns nothing (0 hits):
GET /index/type/_search
{
"query": {
"bool" : {
"must" : {
"multi_match" : {
"query": "Funny black cat",
"operator": "and",
"fields": [ "title", "description"]
}
},
"filter": {
"term" : { "publishStatus" : "PUBLISHED" }
}
}
}
}
Furthermore, this simple filter query is also return 0 records...
GET /index/type/_search
{
"query": {
"bool": {
"filter": {
"term": {
"publishStatus": "PUBLISHED"
}
}
}
}
}
Well this is what worked for me (stopped using filter, changed to match):
GET index/type/_search
{
"query": {
"bool":{
"must":[
{
"multi_match" : {
"query": "dinner sydney",
"fields": [ "title^3", "description" ],
"operator": "and"
}
},{
"match": { "publishStatus": "PUBLISHED"}
}
]
}
}
}

Elastic : search two terms, one on _all, other one on a field

I would like to mix a search on a whole document (eg "developer") and a search on some field for another term (eg "php").
I can do each search separately but I can't mix them.
Here my example (simplified to show only my issue) :
{
"query": {
"function_score": {
"query": {
"match": {
"_all": "developer"
},
"multi_match": {
"query": "php",
"fields": [
"skills.description",
"skills.description",
"skills.details"
],
"operator": "or",
"type": "most_fields"
}
}
}
}
If I run this example I have an error :
Parse Failure [Failed to parse source
Is there a way to search on both _all and specific fields with two terms?
Thanks.
Yes, you're almost there, you need to combine them into a bool/must query:
{
"query": {
"function_score": {
"query": {
"bool": {
"must": [
{
"match": {
"_all": "developer"
}
},
{
"multi_match": {
"query": "php",
"fields": [
"skills.description",
"skills.description",
"skills.details"
],
"operator": "or",
"type": "most_fields"
}
}
]
}
}
}
}
}

Elasticsearch: Facet script and facet_filter

Is it possible to use facet script and facet filter in elasticsearch like this?
{
"facets": {
"judges": {
"terms": {
"field": "judges.untouched",
"size": 10,
"all_terms": false,
"script": { "script": "...", "params": { }}
},
"global_facets": false,
"facet_filter": {
"and": [
{
"query": {
"query_string": {
"query": "..... ",
"fields": [
"judges.analyzed"
],
"default_operator": "and",
"analyze_wildcard": true
}
}
}
]
}
}
}
}
Because when i run this query, elasticsearch raises error: Parse Failure [No facet type found for [and]]]; }.
Thanks
EDIT Incorrect answer. I'm leaving it because of context.
To clarify: and is an appropriate filter and should be accepted by facet_filter. Not sure what's up.
Untested, but from the docs: (http://www.elasticsearch.org/guide/reference/api/search/facets/)
All facets can be configured with an additional filter (explained in the Query DSL section)
So you need to put an appropriate query in facet_filter. And is NOT an appropriate filter (the error you receive could be clearer)
e.g:
"facet_filter" : {
"term" : { "user" : "kimchy"}
}
You'd probably want something like:
"facet_filter" : {
"query_string": {
"query": "..... ",
"fields": [
"judges.analyzed"
],
"default_operator": "and",
"analyze_wildcard": true
}
}
The syntax for an and filter is:
"facet_filter": {
"and": {
"filters": [
{
// filter definition
},
{
// another filter definition
}
]
}
}
But you're using only a single condition, so there's no need of an and filter.
You should just have:
"facet_filter": {
"query": {
"query_string": {
"query": "..."
}
}
}

Resources