关于elasticsearch:Elastic Search-包含空格的搜索字符串

Elastic Search - Search string having spaces in it

我正在寻找ElasticSearch查询,该查询将为其中包含空格的字符串提供完全匹配。

例如,

-我想搜索" XYZ Company Solutions"之类的词。
我尝试使用querystring查询,但是无论搜索结果如何,它都会为我提供所有记录。我也阅读了帖子,发现我们必须为该字段添加一些映射。我在现场尝试了" Not_Analyzed"分析仪,但仍然无法正常工作。

如果有人有完整的示例或步骤,那么您可以和我分享一下吗?

预先感谢。

谢谢,
Sameer


由于您未发布代码,因此很难说出问题所在,但是映射中的"index":"not_analyzed"是处理此问题的正确方法。

这是一个简单的工作示例。首先,我创建一个使用"index":"not_analyzed"

的映射

1
2
3
4
5
6
7
8
9
10
11
12
13
PUT /test_index
{
   "mappings": {
       "doc": {
           "properties": {
               "name":{
                   "type":"string",
                   "index":"not_analyzed"
                }
            }
        }
    }
}

然后添加几个测试文档

1
2
3
4
5
POST /test_index/doc/_bulk
{"index":{"_id":1}}
{"name":"XYZ Company Solutions"}
{"index":{"_id":2}}
{"name":"Another Company"}

现在,我可以通过简单的术语查询来获取所需的文档:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
POST /test_index/doc/_search
{
   "query": {
       "term": {
          "name": {
             "value":"XYZ Company Solutions"
           }
        }
    }
}
...
{
  "took": 1,
  "timed_out": false,
  "_shards": {
     "total": 1,
     "successful": 1,
     "failed": 0
   },
  "hits": {
     "total": 1,
     "max_score": 1,
     "hits": [
         {
           "_index":"test_index",
           "_type":"doc",
           "_id":"1",
           "_score": 1,
           "_source": {
              "name":"XYZ Company Solutions"
            }
         }
      ]
   }
}

在这种情况下,术语过滤器或匹配查询也将起作用。

这是我用来测试的代码:

http://sense.qbox.io/gist/90fcc7f7a88d58f098c50d5aaf0315fdf06e9e9a


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
PUT /index_1
{  
 "settings": {
   "analysis": {
     "normalizer": {
       "lowercase_normalizer": {"type":"custom","char_filter": [],          "filter": ["lowercase"]}
      }
    }
  },
 "mappings": {
    "doc_type": {
           "properties": {
               "name":{"type":"keyword","normalizer":"lowercase_normalizer"}
            }
     }
  }
}

我使用了上面的设置和映射来定义索引。
然后将一些值推入数据库

1
2
3
4
5
6
7
8
9
10
11
12
13
14
POST index_1/doc_type/1
{
 "name" :"a b c"
}

POST index_1/doc_type/1
{
 "name" :"a c"
}

POST index_1/doc_type/1
{
 "name" :"a b"
}

现在,如果我们在上述索引的名称字段中搜索单个字母,则返回的任何内容

1
2
3
4
5
6
7
8
9
10
11
GET index_1/doc_type/_search
{
 "query" :
    {"match": {"name":"A"}}
}

GET index_1/doc_type/_search
{
 "query" :
    {"match": {"name":"b"}}
}

但是如果我们搜索

1
2
3
4
5
GET index_1/doc_type/_search
{
 "query" :
    {"match": {"name":"A B C"}}
}

我们将获得比赛

这有助于搜索完整的关键字,同时避免区分大小写