Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SELECT with LIKE converted _ symbol into ? #885

Open
developer-devPHP opened this issue Feb 14, 2019 · 11 comments
Open

SELECT with LIKE converted _ symbol into ? #885

developer-devPHP opened this issue Feb 14, 2019 · 11 comments

Comments

@developer-devPHP
Copy link

@developer-devPHP developer-devPHP commented Feb 14, 2019

Query request

SELECT filed FROM index  WHERE field_2 LIKE 'a_b'

Explain responce

{
    "from": 0,
    "size": 1000,
    "query": {
        "bool": {
            "filter": [
                {
                    "bool": {
                        "must": [
                            {
                                "wildcard": {
                                    "field_2": {
                                        "wildcard": "a?b",
                                        "boost": 1
                                    }
                                }
                            }
                        ],
                        "adjust_pure_negative": true,
                        "boost": 1
                    }
                }
            ],
            "adjust_pure_negative": true,
            "boost": 1
        }
    },
    "_source": {
        "includes": [
            "filed"
        ],
        "excludes": []
    }
}

if I try to equal

SELECT filed FROM index  WHERE field_2 = 'a_b'

Explain output is ok

{
    "from": 0,
    "size": 1000,
    "query": {
        "bool": {
            "filter": [
                {
                    "bool": {
                        "must": [
                            {
                                "match_phrase": {
                                    "field_2": {
                                        "query": "a_b",
                                        "slop": 0,
                                        "zero_terms_query": "NONE",
                                        "boost": 1
                                    }
                                }
                            }
                        ],
                        "adjust_pure_negative": true,
                        "boost": 1
                    }
                }
            ],
            "adjust_pure_negative": true,
            "boost": 1
        }
    },
    "_source": {
        "includes": [
            "filed"
        ],
        "excludes": []
    }
}
@shi-yuan
Copy link
Member

@shi-yuan shi-yuan commented Feb 15, 2019

what‘s problem?

@developer-devPHP
Copy link
Author

@developer-devPHP developer-devPHP commented Feb 15, 2019

@shi-yuan Here is the problem.
When I am doing select like this

SELECT filed FROM index  WHERE field_2 LIKE 'a_b%'

It's converted to a?b* which means any symbol and not _ so it is converetd _ symbold into ?
But in other symbols for example - it is ok
if my query is

SELECT filed FROM index  WHERE field_2 LIKE 'a-b%'

It is converted to a-b*.
So when you have a time please have a look why _ changed to ? when query with LIKE search
Thanks

@shi-yuan
Copy link
Member

@shi-yuan shi-yuan commented Feb 15, 2019

SELECT filed FROM index  WHERE field_2 LIKE 'a&UNDERSCOREb%'
@developer-devPHP
Copy link
Author

@developer-devPHP developer-devPHP commented Feb 15, 2019

Thank you, it is working.

Can you please help one more thing?
How can we escape * and ? symbols? I tried standard backslash, but it is not working.
LIKE a\\*b and same way LIKE a\\?b but it is not working and show all results between a and b

@shi-yuan
Copy link
Member

@shi-yuan shi-yuan commented Feb 17, 2019

you should use query_string:

select filed from index where q=query('field_2:a\\?b\\*')
@developer-devPHP
Copy link
Author

@developer-devPHP developer-devPHP commented Feb 18, 2019

It is not working.
Here is example.
You have 2 doc

  1. asd
  2. a*d

your search query is

select filed from index where q=query('field_2:a\\*d')

This query result output all 2 docs and not only second one

@shi-yuan
Copy link
Member

@shi-yuan shi-yuan commented Feb 22, 2019

I test latest version,it's ok,just return a*d

@developer-devPHP
Copy link
Author

@developer-devPHP developer-devPHP commented Feb 27, 2019

I am using elastic search version 6.6.0,
plugin version current master (commit c430207)
in doc have data

  1. asd
  2. a*d

executing query

select filed from index where q=query('filed:a\\*d')

response object

{
    "took": 285,
    "timed_out": false,
    "_shards": {
        "total": 4,
        "successful": 4,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 2,
        "max_score": 0,
        "hits": [
            {
                "_index": "index",
                "_type": "_doc",
                "_id": "test_doc_1",
                "_score": 0,
                "_source": {
                    "filed": "a*d"
                }
            },
            {
                "_index": "index",
                "_type": "_doc",
                "_id": "test_doc_2",
                "_score": 0,
                "_source": {
                    "filed": "asd"
                }
            }
        ]
    }
}

But if it works it should return only data a*d

@shi-yuan
Copy link
Member

@shi-yuan shi-yuan commented Feb 28, 2019

My explain:

{
  "from": 0,
  "size": 1000,
  "query": {
    "bool": {
      "filter": [
        {
          "bool": {
            "must": [
              {
                "query_string": {
                  "query": "filed:a\\*d",
                  "fields": [],
                  "type": "best_fields",
                  "default_operator": "or",
                  "max_determinized_states": 10000,
                  "enable_position_increments": true,
                  "fuzziness": "AUTO",
                  "fuzzy_prefix_length": 0,
                  "fuzzy_max_expansions": 50,
                  "phrase_slop": 0,
                  "escape": false,
                  "auto_generate_synonyms_phrase_query": true,
                  "fuzzy_transpositions": true,
                  "boost": 1
                }
              }
            ],
            "adjust_pure_negative": true,
            "boost": 1
          }
        }
      ],
      "adjust_pure_negative": true,
      "boost": 1
    }
  },
  "_source": {
    "includes": [
      "filed"
    ],
    "excludes": []
  }
}
@shi-yuan
Copy link
Member

@shi-yuan shi-yuan commented Feb 28, 2019

What's yours?

@yangshuhao
Copy link

@yangshuhao yangshuhao commented May 21, 2020

我的内容jsoncontent:{"name":"test","page":88,"isNonProfit":true} 我想搜索:name是test; 要如何写?
jsoncontent 是一个字符串filed,里面是压缩后的json字符串

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.