0

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.

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the suggestion. Is there way to accomplish this in the query itself as opposed to changing the mapping?
which version of elasticsearch you are using?
then you alternatively use query_string, i have edited my answer for query string query with fuzziness
This doesn't seem to be working for me. The query is returning all results. I'll bite the bullet and change the mapping to not include the product_code in _all. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.