Elasticsearch query with fields not working - elasticsearch

Hi i am new to elastic search database, When i want to search with fields I am getting following error. Help me to solve this.
Trace: [query_parsing_exception] [filtered] query does not support [fields], with { index=test_database line=1 col=58 }
My Code to search:
client.search({
index: 'test_database',
body: {
query : {
match_all : {},
fields: ["price","brand"]
},
}
})

You can try this. As, match all query doesn't support fields.
client.search({
index: 'test_database',
body: {
fields: ["price","brand"],
query : {
match_all : {}
}
}
})

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!

Elasticsearch match with filter

I need a query that makes partial match on a string and filter outside documents that have a specific value for a field.
I tried this payload for es:
payload = {
search_request: {
_source: [ 'name', 'source','pg_id' ],
query: {
match: { name: query_string }
bool: {
must_not: {
term: { "source.source": source_value }
}
}
},
size: 100
},
query_hint: query,
algorithm: algorithm,
field_mapping: { title: ["_source.name", "_source.source"]}
}
But ES trows this error:
{
:error=> {
:root_cause=> [
{
:type=>"parse_exception",
:reason=>
"failed to parse search source. expected field name but got [
START_OBJECT
] "}],
:type=>" search_phase_execution_exception",
:reason=>"all shards failed",
:phase=>"query",
:grouped=>true,
:failed_shards=> [
{
:shard=>0,
:index=>"articles",
:node=>"3BUP3eN_TB2-zExigd_k2g",
:reason=> {
:type=>"parse_exception",
:reason=>
"failed to parse search source. expected field name but got [
START_OBJECT
] "
}
}
]
},
:status=>400
}
I am using Elasticsearch 2.4
First of all your json format is not valid. Check for a commas and quotes.
Also if you need just to filtrate documents - filters are much faster than queries. Check documentation

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"
}
}
}

elasticsearch full text search part of word

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.

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