我已经根据教程和ElasticSearch网站上的官方文档进行了以下两次搜索,但我得到的结果更像是包含结果,而不是完全匹配。 我对ES非常陌生,因此请原谅任何菜鸟错误。 任何帮助,将不胜感激。
根据我到目前为止所学习的教程,其结果并非如此。所以我很困惑为什么我得到了自己的结果。
数据结构:
{
"_scroll_id": "cXVlcnlBbmRGZXRjaDsxOzI3OlRsWmVmMGh5VENLR0FVclB3eXpIaVE7MDs=",
"took": 7,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"failed": 0
},
"hits": {
"total": 16896,
"max_score": 1,
"hits": [
{
"_index": "cus_index",
"_type": "place",
"_id": "71272349",
"_score": 1,
"_source": {
"id": 34543,
"date1": "1928-09-13 00:00:00",
"date2": "1929-01-01 00:00:00",
"code": "1000",
"phrase": "GOD MODE",
"boolCol": false
}
},
{
"_index": "cus_index",
"_type": "place",
"_id": "71272349",
"_score": 1,
"_source": {
"id": 78635,
"date1": "1928-09-13 00:00:00",
"date2": "1929-01-01 00:00:00",
"code": "3000",
"phrase": "THANK GOD",
"boolCol": false
}
},
{
"_index": "cus_index",
"_type": "place",
"_id": "71272349",
"_score": 1,
"_source": {
"id": 45645,
"date1": "1928-09-13 00:00:00",
"date2": "1929-01-01 00:00:00",
"code": "5000",
"phrase": "SOME OTHER GOD PHRASE",
"boolCol": false
}
},
]
}
}
查询:
// returns all rows
{
"query" : {
"constant_score" : {
"filter" : {
"match" : {
"phrase": "GOD MODE"
}
}
}
}
}
// this returns all rows
{
"query" : {
"query_string": {
"query": "GOD MODE",
"fields": ["phrase"]
}
}
}
// this returns no rows
{
"query" : {
"term": {
"phrase": "GOD MODE"
}
}
}
映射:
{
"cus_index": {
"aliases": {},
"mappings": {
"place": {
"properties": {
"id": {
"type": "int"
},
"date1": {
"type": "date"
},
"date2": {
"type": "date"
},
"code": {
"type": "string"
},
// this is the important one
// i just guessed the others as this is an example, But the col in qu is a string
"phrase": {
"type": "string"
},
"boolCol": {
"type": "boolean"
}
}
}
},
"settings": {
"index": {
"creation_date": "1545321229864",
"number_of_shards": "1",
"number_of_replicas": "0",
"uuid": "4PpzZ49SQZWDDW8sawOIaA",
"version": {
"created": "2030199"
}
}
},
"warmers": {}
}
}
ES版本:
{
"name": "Test Node",
"cluster_name": "firsttestnode",
"version": {
// this was a very old version latest "6.5.4" this is what i
// should have been using for the answers below to work.
"number": "2.3.1",
"build_hash": "bd980929010aef404e7cb0843e61d0665269fc39",
"build_timestamp": "2016-04-04T12:25:05Z",
"build_snapshot": false,
"lucene_version": "5.5.0"
},
"tagline": "Test tag line"
}
----------------------回答尝试----------------------- ------------
我在以下给定的网址中输入并张贴到以下正文,并收到以下错误:
// URL: http://ip_address:9200/test_index1
{
"mappings": {
"demo":{
"properties": {
"phrase": {
"type": "string"
},
"phrase1": {
"type": "keyword"
}
}
}
}
}
// errors
{
"error": {
"root_cause": [
{
"type": "mapper_parsing_exception",
"reason": "No handler for type [keyword] declared on field [phrase1]"
}
],
"type": "mapper_parsing_exception",
"reason": "Failed to parse mapping [demo]: No handler for type [keyword] declared on field [phrase1]",
"caused_by": {
"type": "mapper_parsing_exception",
"reason": "No handler for type [keyword] declared on field [phrase1]"
}
},
"status": 400
}
以下两个答案均按预期工作。 多亏了两者。并不是很确定答案是否正确,但是对于其他阅读者,我会推荐两者。
答案1中只有''type':“ string”'应该为'“ type”:“ text”'的示例。
答案 0 :(得分:0)
term
查询可查找包含在倒排索引中指定的精确术语的文档。
TL; DR:
示例:
PUT test_index1
{
"mappings": {
"demo":{
"properties": {
"phrase": {
"type": "string"
},
"phrase1": {
"type": "keyword"
}
}
}
}
}
POST test_index1/demo/1
{
"phrase":"GOOD GOD",
"phrase1":"GOOD GOD"
}
GET test_index1/_search
{
"query": {
"match": {
"phrase": "GOOD"
}
}
}
返回结果以上
GET test_index1/_search
{
"query": {
"term": {
"phrase1": "GOOD"
}
}
}
以上未返回结果
您甚至可以使用_analyze
GET test_index1/_analyze
{
"field": "phrase",
"text": ["GOOD GOD"]
}
查看文档如何以倒排索引存储。
要回答您的问题:
术语查询查找exact match
。由于您的短语类型为字符串,因此单词GOD MODE将存储为GOD和MODE,其中术语查询将与GOD MODE完全匹配。您必须将其更改为Keyword
或使用match
查询
答案 1 :(得分:0)
我建议如下更新短语属性:
"phrase": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword"
}
}
}
在上述情况下,当为文档编制索引时,您只需将值传递到phrase
字段,phrase.keyword
就可以自动索引。
只要您想进行完全匹配,就可以使用以下术语查询:
{
"term": {
"phrase.keyword": "some text"
}
}