Is it possible to use fuzziness for only one field in a multi_match query? - elasticsearch

I am using the following multi_match query in Elasticsearch and I am wondering if I can use fuzziness only for "friendly_name field". I have tried different things but doesn't seem to work. I am also wondering if it possible to use an analyzer to get a similar result as the fuzziness does:
"query": {
"multi_match": {
"query": "input query",
"fields": ["code_short", "code_word","friendly_name"],
"minimum_should_match": "2"
} }, "_source": ["code", "friendly_name"]
Any help would be appreciated. Thanks.

If you only need query on one field , you don't need multi match
"match": {
"name": {
"query": "your query",
"fuzziness": "1.5",
"prefix_length": 0,
"max_expansions": 100,
"minimum_should_match": "80%"
}
}
I don't believe that you can fully replace fuzziness, but you have 2 options to explore that might work for you. ngram filter or stemmer filter.
======
Well it wasn't very clear to me what you've intended. But you can do your query that way:
"query": {
"bool": {
"should": [
{
"match": {
"friendly_name": {
"query": "text",
"fuzziness": "1.5",
"prefix_length": 0,
"max_expansions": 100
}
}
},
{
"match": {
"code_word": {
"query": "text"
}
}
},
{
"match": {
"code_short": {
"query": "text"
}
}
}
],
"minimum_should_match" : 2
}
}

Related

Named multi_match query

I need a hand here. I have a cross_fields multi_match query and I need to use named queries for each field. I can rewrite it into a bool/should match but then I dont know how to reproduce the cross_fields condition. Any idea? Thanks!
Multimatch query: Relevance OK, but no named queries
GET test_index/_search
{
"query": {
"multi_match": {
"query": "example_query",
"fields": ["name","lastname"],
"type": "cross_fields"
}
}
}
Bool query: Named queries OK but bad relevance
GET test_index/_search
{
"query": {
"bool": {
"should": [
{
"match": {
"name": {
"query": "example query",
"_name": "name_match"
}
}
},
{
"match": {
"latname": {
"query": "example query",
"_name": "latname_match"
}
}
}
]
}
}
}
How does using dismax with tie_breaker: 1.0 work for your ES index?
Something like this:
GET vehicle/_search
{
"query": {
"dis_max": {
"tie_breaker": 1.0,
"queries": [
{
"match": {
"make": {
"query": "Lamborghini",
"_name": "make"
}
}
},
{
"match": {
"model": {
"query": "Diablo",
"_name": "model"
}
}
}
]
}
}
}
The Dismax query is very similar to the multi_match query in that it compares scores across sub clauses/queries. The tie_breaker parameter controls how much losing/non-max fields contributed to the final summation of clause scores, any losing fields scores are multiplied by the tie_breaker. The default tie_breaker: 0.0, which is most similar to type: best_fields in multi_match.

Apply different boosting values when searching for phrase in Elasticsearch?

I want to search for a phrase to Elasticsearch like "personal tax date". I want the returned results to give more weight to the term "tax".
So far I know how to boost entire index or boost for different fields but still don't know how to boost different terms? Any help??
Using function score we can boost by fields
GET <index_name>/_search
{
"query": {
"function_score": {
"query": {
"query_string": {
"query": "*personal tax date*",
"fields": [
"field_1",
"field_2"
]
}
},
"boost": "5",
"functions": [
{
"filter": { "match": { "field": "tax" } },
"weight": 30
},
{
"filter": { "term": { "ent_name": "tax" } },
"weight": 25
}
],
"score_mode": "multiply",
"boost_mode": "sum"
}
}
You can use query_string query and boost the term using query string syntax as below:
{
"query": {
"query_string": {
"query": "personal tax^2 date"
}
}
}

elasticsearch cross fields query alternative for fuzziness?

I have a cross-fields query, and I understand already that you cant use fuzziness with cross-fields queries, but I dont understand the alternative...
this is my simple query:
{
"query": {
"bool": {
"should": [
{
"multi_match": {
"query": "John Legend",
"fields": [
"fname^-4.0",
"lname^-1.0",
"city^-1.0",
],
"type": "cross_fields",
"lenient": "true",
"operator": "AND"
}
}
],
"minimum_should_match": "1"
}
},
"from": 0,
"size": 20
}
I want to be able to find:
John Legend
Joh
John Lege
is that possible?

Adding fuzziness conditionally in ElasticSearch

I have ten or so fields in all my documents: One in particular is product_code which is unique per document and of type string.
I have a match query on _all that works well, but I would like to perform a "fuzzy match" while preserving the ability to search for exact product_code
Here's what I've attempted:
"query": {
"bool": {
"should": [
{
"match": {
"product_code": {
"query": searchString,
"operator": "AND"
}
}
},
{
"match": {
"_all": {
"query": searchString,
"operator": "AND"
"fuzziness": 2,
"prefix_length": 2
}
}
}
]
}
}
The problem with this approach is that the fuzziness is being applied to searches for product_code as well because it's included in _all.
Is there a way to either perform the search on product_code first and if no results are found, perform the search on _all, or exclude product_code from the _all query?
Any help is greatly appreciated.
yes you can exlude product_code from _all using the following mappings.
PUT index_name
{
"settings": {
"analysis": {
"analyzer": {},
"filter": {}
}
},
"mappings": {
"type_name": {
"properties": {
"product_code": {
"type": "string",
"include_in_all": false
}
}
}
}
}
Alternatively you can use query_string search which also offer fuzziness.
Use the following query which use query string with AND operator and fuzziness settings
{
"query": {
"bool": {
"should": [{
"query_string": {
"fields": ["product_code", "other_field"],
"query": "this is my string",
"default_operator": "AND",
"fuzziness": 2,
"fuzzy_prefix_length": 2
}
}, {
"match": {
"product_code": {
"query": "this is my string",
"operator": "AND"
}
}
}]
}
}
}
Hope this helps

Elasticsearch. filtered query with partial_fields possible?

Is it possible to exclude certain field from result ? I'm using filtered query like this:
{
"size": 10,
"query": {
"filtered": {
"query": {
"bool": {
"should": [
{
"text": {
"name": {
"query": "list",
"operator": "or",
"boost": 30
}
}
},
{
"text": {
"field2": {
"query": "list",
"operator": "or",
"boost": 0.2
}
}
},
{
"text": {
"field1": {
"query": "list",
"operator": "or",
"boost": 0.02
}
}
}
]
}
},
"filter": {
"and": [
{
"term": {
"_type": "product"
}
}
]
}
},
"filter": {
"partial_fields": {
"exclude": "field3"
}
}
},
"sort": [
{
"_score": "desc"
}
]
}
I've added filter partial_fields but it does not seem to have any effect. I'm using ES 0.9
Keep in mind that partial_fields support has been deprecated as of 1.0.0beta -
http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-request-fields.html#partial
I know you're on 0.9 but at some point you'll need to upgrade and this approach won't work. I'd suggest upgrading to a 1.x release and using source filtering instead:
http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-request-source-filtering.html
partial_fields can return a partial representation of _source based on include and exclude patterns
So i guess you should specify a wildcard pattern for field name in exclude. If your field name is DATA then the exclude pattern should be DAT*..

Resources