简体   繁体   English

弹性搜索空间

[英]Elastic search with space

I have a Elasticsearch setup which will allow user to search indexes as wild cards. 我有一个Elasticsearch设置,它将允许用户搜索通配符作为索引。

array:3 [
 "index" => "users"
 "type" => "user"
 "body" => array:4 [
 "from" => 0
 "size" => 25
 "sort" => array:1 [
  1 => array:1 [
    "order" => "asc"
  ]
]
"query" => array:1 [
  "bool" => array:1 [
    "should" => array:1 [
      0 => array:1 [
        0 => array:1 [
          "wildcard" => array:1 [
            "full_name" => "john doe"
          ]
        ]
      ]
    ]
  ]
]
]
]

when I pass this array to search function, it is returning an empty array. 当我将此数组传递给搜索功能时,它将返回一个空数组。 But there is a document related to "John Doe" and when I run "full_name" => "john" search is returning the document. 但是有一个与“ John Doe”有关的文档,当我运行"full_name" => "john"搜索将返回该文档。

I feel that the problem is with the space. 我觉得问题出在空间上。

{
"users": {
"user": {
  "properties": {

    "address": {
      "type": "string"
    },
    "full_name": {
      "type": "string"
    },
    "industry_name": {
      "type": "string"
    }
  }
}

} } }}

Assuming field full_name is analyzed by elasticsearch. 假设字段full_namefull_name分析。

The problem in your case is fact that wildcard query doesn't analyze search string 您遇到的问题是通配符查询无法分析搜索字符串的事实

Matches documents that have fields matching a wildcard expression (not analyzed). 匹配具有与通配符表达式匹配(未分析)的字段的文档。

In you case it means, that elasticsearch stored john and doe tokens in inverted index, but wildcard query is searching for john doe token, and it fails. 在您的情况下,这意味着Elasticsearch将johndoe令牌存储在反向索引中,但是通配符查询正在搜索john doe令牌,并且失败。

What you can do about this: 您可以对此做些什么:

  1. Change index mapping, so full_name filed is not analyzed anymore. 更改索引映射,因此不再分析full_name Note: you will have to search for John Doe to get match, because value wasn't analyzed so john doe won't match. 注意:您将必须搜索John Doe以获得匹配,因为未对值进行分析,因此john doe将不匹配。
  2. You can improve first solution, just by leaving full_name analyzed, but with custom analyzer(wildcard, lowercase). 您可以改进第一个解决方案,只需对full_name进行分析,但使用自定义分析器(通配符,小写)即可。 It will allow you to search for text john doe or John Doe . 它将允许您搜索文本john doeJohn Doe

     { "settings" : { "index" : { "analysis" : { "analyzer" : { "lowercase_analyzer" : { "tokenizer" : "keyword", "filter" : [ "lowercase" ], "type" : "custom" } } } } }, "mappings" : { "user" : { "properties" : { "id" : { "type" : "integer" }, "fullName" : { "analyzer" : "lowercase_analyzer", "type" : "string" } } } } } 
  3. You can take advantage of multi field , and search against raw field. 您可以利用multi field ,并针对原始字段进行搜索。

     "full_name.raw" => "John Doe" 

Hope it will help you handle your use case. 希望它能帮助您处理用例。

UPDATE UPDATE

Here you can find more information how to control index mapping. 在这里,您可以找到更多有关如何控制索引映射的信息。

I think standard tokenizer will be applied by default. 我认为默认情况下将应用标准令牌生成器。

In that case, it will consider the text john doe as phrase. 在这种情况下,它将把文本john doe视为短语。

So try phrase search 因此,请尝试词组搜索

"full_name" => "\"john doe\""

If you want to consider spaces you could do something like: 如果要考虑空间,可以执行以下操作:

{
    "match" : {
         "full_name" : {
            "query" : "john doe",
            "operator" : "and",
            "zero_terms_query": "all"
        }
    }
}

check this: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query.html 检查以下内容: https : //www.elastic.co/guide/zh-CN/elasticsearch/reference/current/query-dsl-match-query.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM