ElasticSearch cannot find an exact value that exis

2019-09-15 14:36发布

问题:

When I do an empty query to list some results, I see the values are there. Ex:

{
    "took": 6,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
    },
    "hits": {
        "total": 3024,
        "max_score": 1.0,
        "hits": [{
            "_index": "geotiff_test",
            "_type": "geometa",
            "_id": "AVtt5-j6uVuoyTMhX204",
            "_score": 1.0,
            "_source": {
                "thumbnail": "thumbnail1029.png"
            }
        }, {
            "_index": "geotiff_test",
            "_type": "geometa",
            "_id": "AVtt5-mTuVuoyTMhX205",
            "_score": 1.0,
            "_source": {
                "thumbnail": "thumbnail1321.png"
            }
        }, {
            "_index": "geotiff_test",
            "_type": "geometa",
            "_id": "AVtt5-xkuVuoyTMhX209",
            "_score": 1.0,
            "_source": {
                "thumbnail": "thumbnail2567.png"
            }
        }, {
            "_index": "geotiff_test",
            "_type": "geometa",
            "_id": "AVtt5zr8uVuoyTMhX20F",
            "_score": 1.0,
            "_source": {
                "thumbnail": "thumbnail2122.png"
            }
        }, {
            "_index": "geotiff_test",
            "_type": "geometa",
            "_id": "AVtt5z23uVuoyTMhX20L",
            "_score": 1.0,
            "_source": {
                "thumbnail": "thumbnail1823.png"
            }
        }, {
            "_index": "geotiff_test",
            "_type": "geometa",
            "_id": "AVtt5-53uVuoyTMhX21A",
            "_score": 1.0,
            "_source": {
                "thumbnail": "thumbnail1616.png"
            }
        }, {
            "_index": "geotiff_test",
            "_type": "geometa",
            "_id": "AVtt5_AXuVuoyTMhX21C",
            "_score": 1.0,
            "_source": {
                "thumbnail": "thumbnail3002.png"
            }
        }, {
            "_index": "geotiff_test",
            "_type": "geometa",
            "_id": "AVtt5_j3uVuoyTMhX21U",
            "_score": 1.0,
            "_source": {
                "thumbnail": "thumbnail3039.png"
            }
        }, {
            "_index": "geotiff_test",
            "_type": "geometa",
            "_id": "AVtt5_yQuVuoyTMhX21d",
            "_score": 1.0,
            "_source": {
                "thumbnail": "thumbnail1136.png"
            }
        }, {
            "_index": "geotiff_test",
            "_type": "geometa",
            "_id": "AVtt5zbquVuoyTMhX20C",
            "_score": 1.0,
            "_source": {
                "thumbnail": "thumbnail166.png"
            }
        }, {
            "_index": "geotiff_test",
            "_type": "geometa",
            "_id": "AVtt5zfsuVuoyTMhX20E",
            "_score": 1.0,
            "_source": {
                "thumbnail": "thumbnail2767.png"
            }
        }, {
            "_index": "geotiff_test",
            "_type": "geometa",
            "_id": "AVtt59jQuVuoyTMhX20p",
            "_score": 1.0,
            "_source": {
                "thumbnail": "thumbnail2852.png"
            }
        }, {
            "_index": "geotiff_test",
            "_type": "geometa",
            "_id": "AVtt5_PpuVuoyTMhX21J",
            "_score": 1.0,
            "_source": {
                "thumbnail": "thumbnail1392.png"
            }
        }, {
            "_index": "geotiff_test",
            "_type": "geometa",
            "_id": "AVtt58YguVuoyTMhX20N",
            "_score": 1.0,
            "_source": {
                "thumbnail": "thumbnail603.png"
            }
        }, {
            "_index": "geotiff_test",
            "_type": "geometa",
            "_id": "AVtt5_38uVuoyTMhX21h",
            "_score": 1.0,
            "_source": {
                "thumbnail": "thumbnail416.png"
            }
        }, {
            "_index": "geotiff_test",
            "_type": "geometa",
            "_id": "AVtt5-JFuVuoyTMhX20y",
            "_score": 1.0,
            "_source": {
                "thumbnail": "thumbnail896.png"
            }
        }, {
            "_index": "geotiff_test",
            "_type": "geometa",
            "_id": "AVtt6B1NuVuoyTMhX22i",
            "_score": 1.0,
            "_source": {
                "thumbnail": "thumbnail846.png"
            }
        }, {
            "_index": "geotiff_test",
            "_type": "geometa",
            "_id": "AVtt6B3vuVuoyTMhX22k",
            "_score": 1.0,
            "_source": {
                "thumbnail": "thumbnail1214.png"
            }
        }, {
            "_index": "geotiff_test",
            "_type": "geometa",
            "_id": "AVtt6B90uVuoyTMhX22o",
            "_score": 1.0,
            "_source": {
                "thumbnail": "thumbnail1536.png"
            }
        }, {
            "_index": "geotiff_test",
            "_type": "geometa",
            "_id": "AVtt6COkuVuoyTMhX22y",
            "_score": 1.0,
            "_source": {
                "thumbnail": "thumbnail246.png"
            }
        }]
    }
}

Yet when I run a query like this, it returns nothing:

curl -X POST http://localhost:9200/geotiff_test/geometa/_search -d '{
    "query": {
        "term": {
            "thumbnail": "thumbnail1536.png"
        }
    }
}'

This is the result:

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

If this were Solr, all I want to do is run this simple query: thumbnail:"thumbnail1536.png"

Can someone tell me what is going wrong here?

回答1:

Exact term is not matching because your document field thumbnail got analyzed with standard analyzer and stored it as thumbnail1536 and png.

In Elasticsearch there are different queries for text (full-text) and keyword (exact-term). The one you written comes under latter category.

If you run following full-text query you will get desired results:

curl -X POST http://localhost:9200/geotiff_test/geometa/_search -d '{
    "query": {
        "match": {
            "thumbnail": "thumbnail1536.png"
        }
    }
}'

But as you are trying to search for exact-term using term queries are preferred.

curl -X POST http://localhost:9200/geotiff_test/geometa/_search -d '{
    "query": {
        "term": {
            "thumbnail.keyword": "thumbnail1536.png"
        }
    }
}'

Note: Here .keyword is keyword version of thumbnail field.

Although both yield same results, the above query is more efficient than former one.



回答2:

It depends on how your field is mapped/typed. Please refer to this link from the elastic search website.

Note that you could alter the mapping to better suit your needs.