How can i use elasticsearch query for exact match?


#1

i have web app, where iam trying to move more and more queries from mysql to elasticsearch, even the ones which are not Full text search, to lower the system load.

there are one type of queries which i need to match exactly with uploader username.

lets say, usernames are

tom
tom.bom
tom_bom
tom-bom

when i search for tom

i dont want other username matches to show up.

how can i do that ?

right now i am doing it like this way

Array
(
    [index] => datasets
    [type] => content
    [body] => Array
        (
            [sort] => Array
                (
                    [upload_date] => Array
                        (
                            [order] => desc
                        )
                )
            [query] => Array
                (
                    [bool] => Array
                        (
                            [must] => Array
                                (
                                    [match] => Array
                                        (
                                            [uploader] => tom
                                        )
                                )
                        )
                )
            [from] => 0
            [size] => 30
        )
)

i just want to make this future proof, i dont have those users right now.

Thanks for your time,

here is my current mapping

{
	"mappings": {
    "content": { 
      "properties": { 
        "title":{ 
			"type":     "text",
			"fields": {
				"raw": { 
					"type":  "keyword"
				}
			}
		},
        "tags":    		{ "type": "text" },
        "category":     { "type": "keyword" },
        "sub_category":	{ "type": "keyword" },
        "size":      	{ "type": "long" },
        "uploaders":      { "type": "integer" },
        "downloaders":		{ "type": "integer" },
        "upload_date":	{
			"type":   "date",
			"format": "yyyy-MM-dd HH:mm:ss"
        },
		"uploader":{
					"type":     "text",
					"fields":	{
								"raw": { 
									"type":  "keyword"
								}
					}
		}
      }
	}
  }
}

(Gabriel Tessier) #2

Hi,

What about using term, according to the documentation:

The term query finds documents that contain the exact term specified in the inverted index. For instance:

https://www.elastic.co/guide/en/elasticsearch/reference/6.3/query-dsl-term-query.html

Just replace match by term and according to your mapping add ".raw" to your field

Array
(
    [index] => datasets
    [type] => content
    [body] => Array
        (
            [sort] => Array
                (
                    [upload_date] => Array
                        (
                            [order] => desc
                        )
                )
            [query] => Array
                (
                    [bool] => Array
                        (
                            [must] => Array
                                (
                                    [term] => Array
                                        (
                                            [uploader.raw] => tom
                                        )
                                )
                        )
                )
            [from] => 0
            [size] => 30
        )
)

#3

Thank you, looks like i need to complete the documentation.


(system) #4

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.