ElasticSearch精确匹配-错误结果

vnzz0bqm  于 2021-06-14  发布在  ElasticSearch
关注(0)|答案(4)|浏览(373)

根据elasticsearch网站的教程和官方文档,我做了以下两次搜索,但我得到的结果更像是一个contains结果,而不是一个完全匹配的结果。我是个新手,所以请原谅我的错误。任何帮助都将不胜感激。
根据我到目前为止看过的教程,他们的结果并非如此。所以我很困惑为什么我会得到这样的结果。
数据结构:

{
    "_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"
        }
    }
}

Map:

{
    "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"
}
bvpmtnay

bvpmtnay1#

这个 term 查询查找包含倒排索引中指定的确切术语的文档。
热释光;博士:
例子:

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 and mode,其中术语查询将与god mode完全匹配。要么你得把它改成 `Keyword` 或使用 `match` 查询
von4xj4u

von4xj4u2#


我在下面给定的url中放置并发布到以下正文,并出现以下错误:

// 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”',这样示例才能正常工作。

axr492tv

axr492tv3#

我建议更新短语属性如下:

"phrase": {
   "type": "text",
   "fields": {
      "keyword": {
         "type": "keyword"
      }
   }
}

在上述情况下,在索引文档时,只需将值传递给 phrase 现场和 phrase.keyword 获取自动索引。
无论何时要进行精确匹配,都可以使用术语查询,如下所示:

{
   "term": {
      "phrase.keyword": "some text"
   }
}

相关问题