ehsan shirzadi
ehsan shirzadi

Reputation: 4859

Why this simple elastic search query returns nothing?

I want to search my database for a URL. I have documents like this:

    {
        "_index": "test",
        "_type": "medhelp",
        "_id": "AVgPTB_dMXsxewkUBUNN",
        "_score": 1,
        "_source": {
            "title": "Pump minimed 404sp, 506,507,508",
            "text": "",
            "abstract": "\n            I am looking to buy one of these pump for testing purpose. It need to be in working condition. Email me if you have one for sale ***@****\n        ",
            "answers": [],
            "source": "medhelp",
            "link": "/posts/Diabetes---Type-1/Pump-minimed-404sp--506-507-508/show/2431714"
        }

When I search for source like this, I can get my results:

{
    "query": {
        "prefix": {
            "source": "medhelp"
        }
    }
}

but when I try to do the same for the "link" field with a URL, there is always nothing returned. What's wrong with this:

{
    "query": {
        "prefix": {
            "link": "/posts/Diabetes---Type-1/Pump-minimed-404sp--506-507-508/show/2431714"
        }
    }
}

And this is the result of above query:

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

Upvotes: 0

Views: 57

Answers (1)

Andrzej Martyna
Andrzej Martyna

Reputation: 475

I guess you use default "standard" analyzer so value for "link" is not treated as a whole but rather as many small pieces.

So the answer is to use "keyword" analyzer for your "link" field.

An example how it can be accomplished is in the following code (you can execute the code one instruction at a time using Sense plugin to Chrome browser).

The most important thing is "analyzer: keyword" for "link" field mapping. If you remove this line you will get exactly your case.

DELETE /test123
PUT /test123
{
      "mappings": {
          "medhelp" : {
              "properties": { 
                "link": { "type": "string"
                ,"analyzer": "keyword"
                },
                "source": { "type": "string"}
        }
    }
    }
}

GET /test123/_mapping

PUT /test123/medhelp/1
{
    "link":"/posts/Diabetes---Type-1/Pump-minimed-404sp--506-507-508/show/2431714",
    "source":"medhelp"
}

GET /test123/_search
{
    "query": {
        "prefix": {
            "link": "/posts/Diabetes---Type-1/Pump-minimed-404sp--506-507-508/show/2431714"
        }
    }
}

Upvotes: 1

Related Questions