2
votes

I'm working with ElasticSearch 5 and can't find a solution for the following: I want to search for a string with slashes (part of a url) in a document. But it won't return matching documents. I've read something that strings with slashes are splitted by ES and that's not what I want for this field. I've tried to set "not_analyzed" on the field with a mapping, but I can't seem to get it to work somehow.

"Create index": Put http://localhost:9200/test

{
    "settings" : {
        "number_of_shards" : 1
    },
    "mappings" : {
        "type1" : {
            "properties" : {
                "field1" : { "type" : "text","index": "not_analyzed" }
            }
        }
    }
}

"Add document":POST http://localhost:9200/test/type1/

{
    "field1" : "this/is/a/url/test"
}

"Search document" POST http://localhost:9200/test/type1/_search

{
    "size" : 1000,
    "query" : {
        "bool" : {
            "must" : [{
                    "term" : {
                        "field1" : {
                            "value" : "this/is/a/url/test"
                        }
                    }
                }
            ]
        }
    }
}

Response:

{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "failed": 0
  },
  "hits": {
    "total": 0,
    "max_score": null,
    "hits": []
  }
}

"The mapping response": GET http://localhost:9200/test/_mapping?pretty

{
  "test": {
    "mappings": {
      "type1": {
        "properties": {
          "field1": {
            "type": "text"
          }
        }
      }
    }
  }
}
2

2 Answers

2
votes

Using a term query for getting an exact match is correct. However, your initial mapping is wrong.

"type" : "text", "index": "not_analyzed"

should be this instead

"type": "keyword"

(Note: The keyword type in ES5 is equivalent to a not_analyzed string in ES 2.x)

You need to delete your index and re-create it with the corrected mapping. Then your term query will work.

1
votes

I suspect what you need is a Match query, not a Terms query. Terms is looking for a single "term"/word and is not breaking down your request with an analyzer.

{
    "size" : 1000,
    "query" : {
        "bool" : {
            "must" : [{
                    "match" : {
                        "field1" :  "this/is/a/url/test"                            
                    }
                }
            ]
        }
    }
}