术语过滤not_analysed字符串列表

时间:2016-06-26 12:47:56

标签: elasticsearch

假设有这样的映射:

"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的弹性搜索文档,我试试这样:

    'query': {
        'filtered': {
            'filter': {
                'bool': {
                    'must': {
                          [
                             {
                                "terms": {
                                    "keywords": ["Blah Mlah", "Baw"],
                                    "execution": "and"
                                }
                             }
                          ]
                        }
                     }
                },
            },
        },
    },

但是“执行:和”不起作用,它会像or一样返回。 我也试过这个:

 [
   {
      "term": {
          "keywords": "Blah Mlah"
      }
   },
   {
      "term": {
          "keywords": "Baw"
      }
   }
]

但是当我不想搜索像"Blah Mlah"这样的两个单词关键字时,它不适用于关键字字段。怎么办?

1 个答案:

答案 0 :(得分:1)

查询应该针对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"
                  ]
               }
            }
         ]
      }
   }
}