How to apply fuzziness in a multi_match query to only specified fields in elasticsearch? - elasticsearch

If I have a search query like below:
query: {
multi_match: {
query: searchQry,
fields: ["field1", "field2", "field3"],
fuzziness: 2,
fuzzy_transpositions: true,
},
},
How am I able to apply fuzziness only on selective fields such as "field1" or "field2" or "field1" and "field3" instead of all of them, which is the standard behaviour?

Using MultiMatch is not possible.
You can mix multi-match with matches with separated fuzziness.
Example:
{
"query": {
"bool": {
"should": [
{
"multi_match": {
"query": "xpto",
"fields": [
"field1",
"field2"
]
}
},
{
"match": {
"fiedl1": {
"query": "xpto",
"fuzziness": 1
}
}
},
{
"match": {
"fiedl2": {
"query": "xpto",
"fuzziness": 2
}
}
}
]
}
}
}

Related

ElasticSeach combine multi_match and match_phrase

I use ES 7, I want to search over multi fields, but on this field (title) must be shown firstly if it matches exactly. For now I tried :
{
"query": {
"bool": {
"must": {
"bool": {
"should": [
{
"match_phrase": {
"titre": {
"query": "test",
"boost": "20"
}
}
},
{
"multi_match": {
"fields": ["titre", "description^4", "subtitle^3"],
"query": "test",
"type": "most_fields"
}
}
]
}
}
}
}
}
It works, but I would like to order the match_phrase before other results.
The idea is the user type the exact phrase of a title, this result will appear before other based on multi_match.
Is it possible ?

how to perform partial match elasticsearch

i want to perform both exact match and partial match. for example, "Alize", so if i type "Ali" it should return the result of "Alize" as well. for this case i only can return the result if i type exact word "Alize".
POST /ecommerce/_search
'{
"query": {
"multi_match": {
"fields": [
"name"
],
"operator": "AND",
"query": "Ali*"
}
},
"size": 20,
"stored_fields": [
"uid",
"_source"
]
}`
You can use querystring query as following
"query": {
"query_string": {
"query": "Ali*",
"fields": ["name"]
}
}
Or use wildcard
"query": {
"filtered": {
"filter": {
"bool": {
"must": [
{"query": {"wildcard": {"name": {"value": "Ali*"}}}},
]
}
}
}
}
Wildcard document
This solutions work perfectly for django_elasticsearch_dsl
search_keyword = search_keyword + "*"
query = document_class.search().query(
{
"query_string": {
"query": search_keyword,
"fields": ["name", "code"]
}
}

Searching in specific fields of types

Consider the following query:
{
"query" : {
"match_phrase" : {
"_all" : "Smith"
}
}
}
How would I specify in which fields of which types it may search, instead of searching in everything? (field names may be non-unique across types)
I've tried the query below, but it didn't work (it doesn't return results, it does when I remove person. from all fields):
{
"query": {
"multi_match": {
"query": "Smith",
"fields": [
"person.first_name",
"person.last_name",
"person.age"
],
"lenient": true
}
}
}
I'm sending these queries to http://localhost:9200/tsf-model/_search.
If you can build your query dynamically, I think you can use a combination of your multi_match query and a type query for each type, in order to achieve what you want:
{
"query": {
"bool": {
"minimum_should_match": 1,
"should": [
{
"bool": {
"filter": [
{
"type": {
"value": "type1"
}
},
{
"multi_match": {
"query": "Smith",
"fields": [
"field1",
"field3",
"field5"
]
}
}
]
}
},
{
"bool": {
"filter": [
{
"type": {
"value": "type2"
}
},
{
"multi_match": {
"query": "Smith",
"fields": [
"field2",
"field4",
"field6"
]
}
}
]
}
}
]
}
}
}

Relevance by type on same field in elasticsearch

Is there any way to boost search results on same field depending on type?
My basic boosting is something like:
GET _search
{
"query": {
"simple_query_string": {
"query": "mangan",
"fields":["_all", "title^6"]
}
}
}
But for some other documents I want title to be less important, so I tried to prefix it with type:
GET _search
{
"query": {
"simple_query_string": {
"query": "mangan",
"fields":[
"_all",
"DocumentationPage.title^6",
"DocumentationPage.title^6"]
}
}
}
But then it does not boost at all. As a last resort I could use Funcsion/Script Score bu would like to avoid it.
For sake of example, assume that document contains just title field.
A simple way to achieve this is re-writing the query in the OP as a dis-max query.
Example for elasticsearch 5.x:
{
"query": {
"dis_max": {
"queries": [
{
"simple_query_string": {
"fields": [
"_all"
],
"query": "mangan"
}
},
{
"bool": {
"filter": {
"type": {
"value": "DocumentationPage"
}
},
"must": [
{
"simple_query_string": {
"fields": [
"title^6"
],
"query": "mangan"
}
}
]
}
}
]
}
}
}

How to use multi_match with indices in elasticsearch?

I have 2 indices USER and URL. I want to run a query on different fields based on the index.
In USER index, query should search in name and id field.
But in URL search has to be performed on title and id field.
POST /_search
{
"query":{
"indices":[
{
"indices":[
"URL"
],
"query":{
"multi_match":{
"query":"SMU ",
"fields":[
"title",
"id"
]
}
}
},
{
"indices":[
"USER"
],
"query":{
"multi_match":{
"query":"SMU ",
"fields":[
"name",
"id"
]
}
}
}
]
}
}
The above query is not working. What is the change required to make it work.
How can I merge multi_match search with indices search?
The indices query is deprecated in ES 5, but it still works, you just have a bad structure in yours, i.e. you need to put each indices query in a bool/filter clause.
{
"query": {
"bool": {
"minimum_should_match": 1,
"should": [
{
"indices": {
"indices": [
"URL"
],
"query": {
"multi_match": {
"query": "SMU ",
"fields": [
"title",
"id"
]
}
}
}
},
{
"indices": {
"indices": [
"USER"
],
"query": {
"multi_match": {
"query": "SMU ",
"fields": [
"name",
"id"
]
}
}
}
}
]
}
}
}
Since the indices query is deprecated, the new idea is to use a simple term query on the _index field instead. Try this:
{
"query": {
"bool": {
"minimum_should_match": 1,
"should": [
{
"bool": {
"filter": [
{
"term": {
"_index": "URL"
}
},
{
"multi_match": {
"query": "SMU ",
"fields": [
"title",
"id"
]
}
}
]
}
},
{
"bool": {
"filter": [
{
"term": {
"_index": "USER"
}
},
{
"multi_match": {
"query": "SMU ",
"fields": [
"name",
"id"
]
}
}
]
}
}
]
}
}
}

Resources