elasticsearch match_phrase_prefix查询不与analyzer_startswith一起使用

时间:2017-10-04 20:42:40

标签: elasticsearch

我是弹性搜索的新手,当我在单个场上使用匹配短语前缀时,如果我在多个场上使用匹配短语pefix它不起作用。  搜索混合,121 如果feild3大于4位,我得到的结果是,我需要得到第1位数的结果。   我的查询如下:

{
  "query": {
    "bool": {
      "must": [
        {
          "match_phrase_prefix": {
            "feild1": "p"
          }
        },
        {
          "bool": {
            "must": [
              {
                "match_phrase_prefix": {
                  "feild2": {
                    "query": "HYBRID "
                  }
                }
              },
              {
                "match_phrase_prefix": {
                  "feild3": {
                    "query": "121"
                  }
                }
              }
            ]
          }
        }
      ]
    }
  },
  "from": "0",
  "size": "10"
}

feild3在数据库中很长,我在加载数据时将long转换为char。

我的映射:

{
  "settings": {
    "index": {
      "analysis": {
        "analyzer": {
          "analyzer_startswith": {
            "tokenizer": "keyword",
            "filter": "lowercase"
          }
        }
      }
    }
  },
  "mappings": {
    "pty": {
      "properties": {
        "feild2": {
          "search_analyzer": "analyzer_startswith",
          "analyzer": "analyzer_startswith",
          "type": "string"
        },
        "feild3": {
          "search_analyzer": "analyzer_startswith",
          "analyzer": "analyzer_startswith",
          "type": "string"
        }
      }
    }
  }
}

任何人都可以帮我解决这个问题。

1 个答案:

答案 0 :(得分:1)

对于您的用例。你的映射应该是这样的:

PUT index_name
{
    "settings": {
        "index": {
            "analysis": {
                "analyzer": {
                    "analyzer_startswith": {
                        "tokenizer": "keyword",
                        "filter": "lowercase"
                    }
                }
            }
        }
    },
    "mappings": {
        "type_name": {
            "properties": {
                "field1": {
                    "analyzer": "analyzer_startswith", 
                    "search_analyzer": "analyzer_startswith",
                    "type": "text"
                },
                "field2": {
                    "analyzer": "analyzer_startswith", 
                    "search_analyzer": "analyzer_startswith",
                    "type": "text"
                },
                "field3": {
                    "analyzer": "analyzer_startswith", 
                    "search_analyzer": "analyzer_startswith",
                    "type": "text"
                }
            }
        }
    }
}

示例文件:

POST index_name/type_name
{
  "field1":"hatim",
  "field2":"stovewala",
  "field3":"india"
}

查询应该是这样的:

GET index_name/type_name/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match_phrase_prefix": {
            "field1": "hat"
          }
        },
        {
          "bool": {
            "must": [
              {
                "match_phrase_prefix": {
                  "field2": "s"
                }
              },
              {
                "match_phrase_prefix": {
                  "field3": "in"
                }
              }
            ]
          }
        }
      ]
    }
  }
}