Elasticsearch使用like_text中的多个值对此查询进行模糊处理

时间:2015-02-12 07:00:23

标签: elasticsearch

我是Elasticsearch的新手。我们使用的是fuzzy_like_this查询,如下所示

{
                      "query": {
                        "bool": {
                          "should": [
                            {
                              "flt": {
                                "fields": [
                                  "actor.id"
                                ],
                                "like_text": "kar@gmail.com"
                              }
                            }
                          ]
                        }
                      },
                      "size": "100",
                      "sort": [
                        {
                          "published": {
                            "order": "desc",
                            "ignore_unmapped": true
                          }
                        }
                      ]
                    }

like_text目前匹配单个字符串,我们想修改它以搜索逗号分隔值,"like_text": "kar@gmail.com,xyz@yahoo.com"

我尝试了flt中的分析器和匹配选项,但无法获得预期的结果。对此方面的任何帮助表示赞赏。在此先感谢。

1 个答案:

答案 0 :(得分:0)

您确定需要模糊查询吗?您是否尝试过使用match query?您可以配置许多选项,但默认情况下,standard analyzer将用于标记您的查询文本,并在逗号上标记。

作为一个例子,我创建了一个简单的索引并添加了一些文档:

PUT /test_index
{
    "settings": {
        "number_of_shards": 1,
        "number_of_replicas": 0
    }
}

POST /test_index/_bulk
{"index":{"_index":"test_index","_type":"doc"}}
{"email": "kar@gmail.com"}
{"index":{"_index":"test_index","_type":"doc"}}
{"email": "xyz@yahoo.com"}
{"index":{"_index":"test_index","_type":"doc"}}
{"email": "somebody@somewhereelse.com"}

然后运行一个简单的匹配查询,并取回预期的结果:

POST /test_index/_search
{
   "query": {
      "match": {
         "email": "kar@gmail.com,xyz@yahoo.com"
      }
   }
}
...
{
   "took": 2,
   "timed_out": false,
   "_shards": {
      "total": 1,
      "successful": 1,
      "failed": 0
   },
   "hits": {
      "total": 2,
      "max_score": 0.43920785,
      "hits": [
         {
            "_index": "test_index",
            "_type": "doc",
            "_id": "AhjrGCcXSjW26eNDDYENHA",
            "_score": 0.43920785,
            "_source": {
               "email": "kar@gmail.com"
            }
         },
         {
            "_index": "test_index",
            "_type": "doc",
            "_id": "X7Fk6J1TQMWK5ZccsfBrVA",
            "_score": 0.43920785,
            "_source": {
               "email": "xyz@yahoo.com"
            }
         }
      ]
   }
}

以下是我使用的代码(如果您使用的是ES 1.4,则必须启用CORS才能在浏览器中使用代码):

http://sense.qbox.io/gist/633c2d284979b26406040db90815231ba71513bc