Retrieve Search Results Based on the First Letter in a Specific Field

I installed the latest version and now it's working fine.

Really? I don't remember my script was giving back both documents. Are you sure you tested it? Might be me though.

Yes, I did.
The following is the result:

{
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
    "total" : 3,
    "successful" : 3,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "en",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "title" : "Articles"
        }
      },
      {
        "_index" : "en",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 1.0,
        "_source" : {
          "title" : "Foo Articles"
        }
      }
    ]
  }
}

Right. Sorry. Here is the right one:

DELETE en
PUT en
{
  "settings": {
    "analysis": {
      "analyzer": {
        "lowercase": {
          "type":      "custom", 
          "tokenizer": "keyword",
          "filter": [
            "lowercase"
          ]
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "title": {
        "type": "text",
        "analyzer": "lowercase"
      }
    }
  }
}

PUT en/_doc/1
{
  "title": "Articles"
}
PUT en/_doc/2
{
  "title": "Foo Articles"
}

GET /_search
{
  "query": {
    "prefix": {
      "title": "a"
    }
  }
}

Look I noticed that I need to use the keyword type for the field so that the prefix query works.

PUT en2
{
  "mappings": {
"properties": {
  "title": {
    "type": "keyword"
  }
}
  }
}

PUT en2/_doc/1
{
  "title": "Articles"
}
PUT en2/_doc/2
{
  "title": "Foo Articles"
}


GET en2/_search
{
  "query": {
    "prefix": {
      "title": "A"
    }
  }
}

I need the title to have 2 different analyzers and as stated above, it needs to be defined as a keyword. How is that even possible?
Also, I noticed that if the query is 'a' as in lowercase, then no documents will be returned.

Also, I noticed that if the query is 'a' as in lowercase, then no documents will be returned.

Check the code I pasted.

Okay, Thanks for your help.

In case I need to update my code to include this, I need to use the multi fields, is that correct?

PUT en
{
  "mappings": {
"properties": {
  "Id": {
    "type": "text"
  },
  "Title": {
    "type": "text",
    "analyzer": "english_analyzer",
    "fields": {
      "exact": {
        "type": "text",
        "analyzer": "english_standard"
      },
"first_letter": {
"type": "text",
"analyzer": "lowercase"
}
    }
  }
}
  },
  "settings": {
"number_of_shards": 5,
"analysis": {
  "filter": {
    "english_stop": {
      "type": "stop",
      "stopwords": "_english_"
    },
    "english_stemmer": {
      "type": "stemmer",
      "language": "english"
    },
    "english_possessive_stemmer": {
      "type": "stemmer",
      "language": "possessive_english"
    }
  },
  "analyzer": {
    "english_analyzer": {
      "tokenizer": "standard",
      "filter": [
        "english_possessive_stemmer",
        "lowercase",
        "english_stop",
        "english_stemmer"
      ]
    },
"lowercase": {
      "type":      "custom", 
      "tokenizer": "keyword",
      "filter": [
        "lowercase"
      ]
    },
    "english_standard": {
      "tokenizer": "standard",
      "filter": [
        "english_possessive_stemmer",
        "lowercase"
      ]
    }
  }
}
  }
}

but it isn't returning any results.

    PUT en/_doc/1
{
  "title": "Articles"
}
PUT en/_doc/2
{
  "title": "Foo Articles"
}

GET en/_search
{
  "query": {
    "prefix": {
      "Title.first_letter": "a"
    }
  }
}

It works for me if you use the following instead:

PUT en/_doc/1
{
  "Title": "Articles"
}
PUT en/_doc/2
{
  "Title": "Foo Articles"
}

Yes!
Thank you for your time:)

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.