MUST and MUST_NOT query in Elasticsearch - elasticsearch

I have indexed documents with metadata "User_Id" containing data "A"
and "B". I'm trying to check documents "A NOT B". I am not able to get the desired output. I am restricted to not use "query string query" and use "NOT" operator.
Doesn't must_not support multi_match?
{
"from": 0,
"size": 24,
"query": {
"bool": {
"must": [
{
"multi_match": {
"query": "A",
"fields": ["User_Id"],
"fuzziness": "AUTO"
}
}
],
"must_not" :[
{
"multi_match": {
"query": "B",
"fields": ["User_Id"],
"fuzziness": "AUTO"
}
}
]
}
}
}

You need to remove the fuzziness auto. This setting allows approximation in the string: a query "AUTO13273" with fuzziness "AUTO" will match AUTO13272 and AUTO13273 since the distance between those two strings is only 1.
See the fuzziness documentation here

Related

Elastic Search exact match query with wildcard search for multiple fields

I have elastic search data store like below, and I need to write multi search ES query through these data with exact match and exact match + *(wildcard)
[
{
"id": "123",
"name": "test123 abc bct",
"externalObj": {
"id": "abc 123"
}
},
{
"id": "124",
"name": "test124 abc bct",
"externalObj": {
"id": "abc 124"
}
}
]
currently i have written query like below,
{
"query": {
"query_string": {
"fields": [
"name^5",
"id",
"externalObj.*"
],
"query": "(test124 abc)",
"default_operator": "AND"
}
}
}
Above query is working fine with exact match but I need to get the data for partial search and maximum relevant score for the response as well. that thing doesn't work with this query.
e.g: "query": "test124 ab"
Can anyone help me out for above problem ?
There are 2 options to achieve what you want. You can choose one of them to use:
Set default_operator to OR (or just simply remove it since the default value is OR).
{
"query": {
"query_string": {
"fields": [
"name^5",
"id",
"externalObj.*"
],
"query": "test124 a"
}
}
}
Change your query into test124 a*
{
"query": {
"query_string": {
"fields": [
"name^5",
"id",
"externalObj.*"
],
"query": "test124 a*",
"default_operator": "AND"
}
}
}

Get ElasticSearch simple_query_string to support fuzzy

I have a record in my ElasticSearch index with the term "cleveland". When I do this search:
"query": {
"multi_match": {
"fields": [
"firstname^3",
"lastname^3",
"home_address",
"home_city"
],
"query": "clevela",
"fuzziness": "AUTO"
}
},
it successfully finds the term. The missing two characters are within the fuzziness threshold. But I'd like to support the extended query syntax of simple_query_string (+, -, phrase search, etc.) So I tried this syntax:
"query": {
"simple_query_string": {
"query": "clevela",
"fields": [
"firstname^3",
"lastname^3",
"home_address",
"home_city"
],
"lenient": true
}
},
and it does not find the term. Fuzziness appears to be turned off. How do I turn it on?
In a simple query string, you need to specify the fuzziness parameter, by adding ~N (N is the max edit distance) after the search term. Modify your search query as
{
"query": {
"simple_query_string": {
"query": "clevela~2", // note this
"fields": [
"firstname^3",
"lastname^3",
"home_address",
"home_city"
],
"lenient": true
}
}
}

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

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
}
}

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

How can Elasticsearch search characters like #

I face the problem about writing Elasticsearch query.
My query is like below
{
"query": {
"query_string": {
"default_field": "content",
"query": "#lin1"
}
},
"from": 0,
"size": 1000,
"sort": [
{
"time": "desc"
}
]
}
And I am using query_string
http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html
But the # character can not match.
It will come out this kind of result: lin1 or 「lin1」
So how should I write the Elasticsearch query to match #lin1?
It all depends on the analyzer you are using. For all you know, you are using the standard analyzer which discards the '#' symbol from the index. In that case, you'll never be able to search for the '#' symbol. But if that is not the case and you do have '#' indexed, you can modify the query_string section of your query to below:
"query_string": {
"default_field": "content",
"query": "#lin1",
"analyzer": "whitespace"
}

Resources