假设有一个这样的映射:
"keywords": {
"type": "string",
"fields": {
"raw": {
"type": "string",
"index": "not_analyzed"
}
}
},
我不会得到这样的查询:
Retrive all of documents that contain(exactly and phrase) "Blah Mlah" AND "Baw"
为了遵循term filters的elasticsearch文档,我尝试这样:
'query': {
'filtered': {
'filter': {
'bool': {
'must': {
[
{
"terms": {
"keywords": ["Blah Mlah", "Baw"],
"execution": "and"
}
}
]
}
}
},
},
},
},
但是“execution:and”不起作用,它将像
or
一样返回。我也试过这个:
[
{
"term": {
"keywords": "Blah Mlah"
}
},
{
"term": {
"keywords": "Baw"
}
}
]
但是当我不想搜索
"Blah Mlah"
这样的两个单词关键字时,它不适用于关键字字段。怎么办
最佳答案
该查询应针对keywords.raw
,因为这是尚未分析的multi-field
1.x中的示例查询:
put test
put test/test/_mapping
{
"properties": {
"keywords": {
"type": "string",
"fields": {
"raw": {
"type": "string",
"index": "not_analyzed"
}
}
}
}
}
put test/test/1
{
"keywords" : [ "Baw", "Blah Mlah"]
}
put test/test/2
{
"keywords" : ["Baw"]
}
post test/test/_search
{
"query": {
"filtered": {
"filter": {
"bool": {
"must": [
{
"terms": {
"keywords.raw": [
"Blah Mlah",
"Baw"
],
"execution": "and"
}
}
]
}
}
}
}
}
结果:
"hits": {
"total": 1,
"max_score": 1,
"hits": [
{
"_index": "test",
"_type": "test",
"_id": "1",
"_score": 1,
"_source": {
"keywords": [
"Baw",
"Blah Mlah"
]
}
}
]
}
在2.x中的示例查询:
{
"query": {
"bool": {
"filter": [
{
"terms": {
"keywords.raw": [
"Blah Mlah"
]
}
},
{
"terms": {
"keywords.raw": [
"Baw"
]
}
}
]
}
}
}
关于elasticsearch - 字词按not_analysed字符串列表过滤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38038637/