0

I would like to get some data from my Elasticsearch server locally, with help of "regexpQuery", and for this I made the following method :

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();

}

The documents in Elasticsearch looks like this :

{
  "_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
  ]
}

But on response, I get always 0 hits.

The output of : 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"
          }
        }
      }
    }
  }
}

What I do wrong ?

4
  • Is there any analyzer specified on ProductCode. regexp applies to the terms produced by the tokenizer for that field, and not to the original text of the field. Commented Mar 31, 2016 at 8:56
  • @rahulroc, I'm a beginner in using Elasticsearch and Java API for it. I don't understand your statement/question. Commented Mar 31, 2016 at 9:05
  • Then you need to read elastic.co/guide/en/elasticsearch/reference/current/…. Also, please provide the output of curl -XGET 'localhost:9200/warehouse-550/_mapping/core2' Commented Mar 31, 2016 at 9:21
  • @rahulroc, I have added the output for that command. Commented Mar 31, 2016 at 12:57

1 Answer 1

1

the default analyzer for ProductCode is standard analyzer

If we were to reimplement the standard analyzer as a custom analyzer, it would be defined as follows:

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

If you notice the "lowercase" filter, it converts the text to lowercase.

FALK0011927540Y gets converted to falk0011927540y

Hence, when you are searching for "FA.*", there is no match.

Solution :

  • Search by lowercasing on your client side. For eg. "fa.*"

  • map your ProductCode as not_analyzed. It will store it as it is.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.