Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

elasticsearch - search with regex involving space

I want to perform searching using regular expression involving whitespace in elasticsearch. I have already set my field to not_analyzed. And it's mapping is just like

"type1": {
   "properties": {
      "field1": {
         "type": "string",
         "index": "not_analyzed",
         "store": true
      }
   }
}

And I input two value for test,

"field1":"XXX YYY ZZZ"
"field1":"XXX ZZZ YYY"

And i do some case using regex query /XXX YYY/
(I want to use this query to find record1 but not record2)

{
    "query": {
        "query_string": {
           "query": "/XXX YYY/"
        }
    }
}

But it return 0 results.

However if I search without using regex (without the forward slash '/'), both record1 and record2 are returned.

Is that in elasticsearch, i cannot search using regex query involving space?

like image 715
user3488854 Avatar asked Nov 26 '22 05:11

user3488854


1 Answers

What you need is a ''term'' query that doesn't tokenise the search query by breaking it down into smaller parts. More about the term query here: https://www.elastic.co/guide/en/elasticsearch/reference/2.0/query-dsl-term-query.html

There's a special breed of term queries that allows you to use regexes called regexp queries. That should match any whitespaces as well: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-regexp-query.html

like image 133
Jens Egholm Avatar answered Dec 06 '22 17:12

Jens Egholm