elasticsearch full text search part of word - elasticsearch

How can I query on elasticsearch for full text searching by part of word.
For example if I have these documents
{
name: "A1"
desc: "This is first document"
}
{
name: "A2"
desc: "This is second document"
}
When I search like this
{
query: {
query_string: {
query: 'first'
}
}
}
It returns me first document, but when I try to search
{
query: {
query_string: {
query: 'fir'
}
}
}
It doesnt return anything.
How can I solve this without mapping parameters such as ngrams, just with query.
Thank you

You should try with a wildcard instead, like this, it will work.
{
query: {
query_string: {
query: 'fir*'
}
}
}
Otherwise, use ngrams, it's much more performant.

Related

How to create this Elasticsearch query body using elastic-builder?

I'd like to create this exact JSON object using elastic-builder library
{
track_total_hits: true,
query: {
script_score: {
query: {
bool: {
must: {
match_all: {}
}
}
}, script: {
source: "...my script..."
}
}
}
}
So that means script_score inside a query, then query again etc and if possible also how to add that track_total_hits key... I could not find a way to do that using their API.
There is a way using functionScoreQuery() to create a similar looking object, but it was not accepted. It was like below:
esb.functionScoreQuery()
.query(esb.matchAllQuery())
.function(esb.scriptScoreFunction(
esb.script('source', "...my script...")
))
Thanks in advance!

How can I find entries in elastic where a specific value is present in array?

Ok, this is the schema:
id
generated{
status{
myStatuses[]
}
}
So having now these entries:
id=1
generated.status.myStatuses=['busy', 'free']
id=2
generated.status.myStatuses=['busy']
id=3
generated.status.myStatuses=['free']
I want to match all the documents where "generated.status.myStatuses" contains the word "free".
In the example above I would find id=1 and id=3.
There's no dedicated array datatype in ES so you can treat your keyword arrays as keywords. This means either
GET generated/_search
{
"query": {
"match": {
"generated.status.myStatuses": "free"
}
}
}
or, for exact matches,
GET generated/_search
{
"query": {
"term": {
"generated.status.myStatuses.keyword": "free"
}
}
}
{
"query":{
"match":{
"generated.status.myStatuses":"free"
}
}
}
You need to use match or term queries based on your data mapping.

Elasticsearch exeact match on analyzed field of integers

I want to find exact matches on a (analyzed string) field in ES. All values are integers but mapped as strings. I, unfortunately, cannot change the mapping and using
query: {
match: {
fieldName: '1234'
}
}
also gives me 0 hits.
I cannot figure out if it's the standard analyzer working in a bizarre way when the mapping is
index: {
type: {
properties: {
fieldName: {
type: string
}
}
}
}
and data is
{fieldName: '12345'}
or there is something in the match query that I'm missing.
Thanks :)
Change your quotations for the fieldNames value from ticks ' to quotes ". Trying your query will the correct quotes returns the expected results on my end.
{
"query": {
"match": {
"fieldName": "1234"
}
}
}

Why is my very simple ElasticSearch query failing, SearchPhaseExecutionException

Im trying to do a search for "dog chew" in the invention-title field in my PatentGrants type.
query url: POST http://localhost:9200/patents/patentGrants/_search
query body:
{
"query": {
"match_all": {
"invention-title": "dog chew"
}
}
}
Below is a picture of the data in my patents index and below that is a picture of my query and the error message.
Try this:
{
"query": {
"match": {
"inventionTitle": "dog chew"
}
}
}
The field name in the screenshot is inventionTitle not invention-title.
https://www.elastic.co/guide/en/elasticsearch/reference/1.6/query-dsl-match-all-query.html - use match instead of match_all. match_all doesn't accept a search query.

ElasticSearch query failing due to state codes "in" and "or" being reserved words

I'm querying for states using the state code as the query string, and "in" and "or" (Indiana and Oregon) are failing, presumably because they're reserved words.
I can confirm that the data exists in the index correctly, because when I run:
curl -XGET 'localhost:9200/state/_search?size=200&pretty=true' -d '{"query" : {"match_all" : {}}}' > out.txt
I can see the data there for both the working states and the non-working states. Plus, if I change the state code of a non-working state in CouchDB to something like XYZ, I can verify that the change makes it to ES by running the above command and searching for XYZ. So I know I'm looking at the right data and it's indexing fine.
The problem is the query. Right now, here's what my entire query object looks like:
var q = {
size: 0,
query: {
filtered: {
query: { term: { postcode: 'tn' } },
filter: { term: { version: 2 } }
}
},
facets: {
version: { terms: { field: "version" } },
count : { statistical : { field : "latestValues.enroll" } }
}
};
If I run that query, I get no results. If I change the "or" out with "tn" or "tx" or "sc" etc., then it works fine.
I looked for a way to escape reserved words and found this link but it doesn't seem to work for me, when running the following query:
var q = {
size: 0,
query: {
filtered: {
query: { match_all: { } },
filter: { term: { version: 2, postcode: 'or' } }
}
},
facets: {
version: { terms: { field: "version" } },
count : { statistical : { field : "latestValues.enroll" } }
}
};
(Note that that query also works when changing out "or" with a non-reserved-word-state so I know it's not a problem with the query itself).
Any ideas?
This is not about "reserved" words, its about stop words. You are using an analyzer which removes stop words (the default analyzer up to a more recent version of Elasticsearch).
You'll need to change the analyzer for the field, see here: http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/analysis.html
This will change require reindexing, though

Resources