java - 使用正则表达式进行Elasticsearch Java API搜索

标签 java regex elasticsearch

我想借助“regexpQuery”从本地Elasticsearch服务器获取一些数据,为此,我做了以下方法:

public void getProductsStartingWithString() throws ParseException {

    Client client = getClient();

    SearchResponse response = null;

    BoolQueryBuilder boolQuery = QueryBuilders.boolQuery()
            .must(QueryBuilders.regexpQuery("ProductCode", "FA.*"));

    if (response == null || response.getHits().hits().length != 0) {

        response = client.prepareSearch("warehouse-550")
                .setTypes("core2")
                .setQuery(boolQuery)
                .setSize(100)
                .execute()
                .actionGet();
    }

    response.getHits();

}

Elasticsearch中的文档如下所示:
{
  "_index": "warehouse-550",
  "_type": "core2",
  "_id": "AVOKD0Pq8h4KkDGZwBom",
  "_score": null,
  "_source": {
    "message": "3,550,162.06,FALK0011927540Y,2016-03-16;08:00:00.000\r",
    "@version": "1",
    "@timestamp": "2016-03-16T07:00:00.000Z",
    "path": "D:/Programs/Logstash/x_testingLocally/processed-stocklevels-550-42190516032016.csv",
    "host": "EVO385",
    "type": "core2",
    "Quantity": 3,
    "Warehouse": "550",
    "Price": 162.06,
    "ProductCode": "FALK0011927540Y",
    "Timestamp": "2016-03-16;08:00:00.000"
  },
  "fields": {
    "@timestamp": [
      1458111600000
    ]
  },
  "sort": [
    1458111600000
  ]
}

但是在回应时,我总是得到0次点击。

输出:curl -XGET "172.22.130.189:9200/warehouse-550/_mapping/core2?pretty":
{
  "warehouse-550" : {
    "mappings" : {
      "core2" : {
        "properties" : {
          "@timestamp" : {
            "type" : "date",
            "format" : "strict_date_optional_time||epoch_millis"
          },
          "@version" : {
            "type" : "string"
          },
          "Price" : {
            "type" : "double"
          },
          "ProductCode" : {
            "type" : "string"
          },
          "Quantity" : {
            "type" : "long"
          },
          "Timestamp" : {
            "type" : "string"
          },
          "Warehouse" : {
            "type" : "string"
          },
          "host" : {
            "type" : "string"
          },
          "message" : {
            "type" : "string"
          },
          "path" : {
            "type" : "string"
          },
          "type" : {
            "type" : "string"
          }
        }
      }
    }
  }
}

我做错了什么?

最佳答案

ProductCode的默认分析器是standard analyzer

如果我们要将标准分析器重新实现为自定义分析器,则其定义如下:

{
    "type":      "custom",
    "tokenizer": "standard",
    "filter":  [ "lowercase", "stop" ]
}

如果您注意到"lowercase"过滤器,它将文本转换为小写。

FALK0011927540Y gets converted to falk0011927540y



因此,当您搜索"FA.*"时,没有匹配项。

解决方案:
  • 通过在客户端使用小写字母进行搜索。例如。 "fa.*"
  • 将您的ProductCode映射为not_analyzed。它将按原样存储。
  • 关于java - 使用正则表达式进行Elasticsearch Java API搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36328167/

    相关文章:

    java - 如何在 XML 属性值中包含 &、<、> 等

    java - 是否可以将资源文件放入 JAR 文件中?

    javascript - JS 正则表达式 : How to capture repeating pattern into different array elements?

    Java修改csv行

    mysql - 如何在elasticsearch中编写分组查询?

    java - 本地主机上的 JBoss 服务器连接被拒绝 :1099

    java - Java中将中文字符串转换为Unicode

    regex - 带有 {} grep 和正则表达式的大括号 : Why does it exceed the maximum value?

    linux - Logstash 文件限制?

    python - Elasticsearch 在对系统进行任何操作时出错?