duanqiao9541 2015-06-23 12:25
浏览 8
已采纳

弹性搜索空间

I have a Elasticsearch setup which will allow user to search indexes as wild cards.

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.

I feel that the problem is with the space.

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

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

} }

  • 写回答

3条回答 默认 最新

  • dongluolie3487 2015-06-24 06:43
    关注

    Assuming field full_name is analyzed by elasticsearch.

    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.

    What you can do about this:

    1. Change index mapping, so full_name filed is not analyzed anymore. Note: you will have to search for John Doe to get match, because value wasn't analyzed so john doe won't match.
    2. You can improve first solution, just by leaving full_name analyzed, but with custom analyzer(wildcard, lowercase). It will allow you to search for text john doe or John 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.

      "full_name.raw" => "John Doe"
      

    Hope it will help you handle your use case.

    UPDATE

    Here you can find more information how to control index mapping.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line