How to define two word in spacing - elasticsearch

My elasticsearch coding as below:
"query": {
"constant_score" : {
"filter" : {
"bool": {
"must": [
{
"terms": {"stkno" : ["Net Asset","asset combine"]}},
{ "range": { "CDate": { "gte": "2015-01-01" , "lte": "2017-06-03T23:59:59" } } } ]
}}}}
this is mapping:
{
"news": {
"mappings": {
"bursa": {
"_timestamp": {},
"properties": {
"CDate": {
"type": "date",
"format": "epoch_millis||dateOptionalTime"
},
"RDate": {
"type": "date",
"format": "epoch_millis||dateOptionalTime"
},
"category": {
"type": "string"
},
"content": {
"type": "string"
},
"exDate": {
"type": "string"
},
"exgcode": {
"type": "string"
},
"htext": {
"type": "string"
},
"paymentDate": {
"type": "string"
},
"stat": {
"type": "string"
},
"stkno": {
"type": "string"
},
"tag": {
"type": "string"
}
}
}
}
}
}
Please kindly advice if my word is : Net Asset and Asset combine, use in Must condition,
if "Asset combine" not in the elasticsearch and "New Asset" in the elasticsearch, using Must condition so all indices can't search?
how to do i want find the word if ones of them inside elasticsearch?
Cause it can't find "Net Asset" exactly word when i use should condition.
Thanks

Since stkno is an analyzed field and you are applying terms query which looks for exact match, you are not able fetch results. Try this instead.
"bool": {
"should": [
{
"match": {
"stkno": "Net Asset",
"operator": "and"
}
},
{
"match": {
"stkno": "asset combine",
"operator": "and"
}
}
],
"must": [
{
"range": {
"CDate": {
"gte": "2015-01-01",
"lte": "2017-06-03T23:59:59"
}
}
}
]
}
Hope it helps!!

Related

using match_phrase twice on ES query filter? No filter registered for [match_phrase]

I have been trying to fetch a document using multiple filters.
Im currently using ES 1.7 Is it possible to use match_phrase twice on a filter?
example: people document
q=aaron&address=scarborough - searching a person by name and address, works fine.
{
"query": {
"match_phrase": {
"name": "aaron"
}
},
"filter": {
"bool": {
"must": {
"nested": {
"path": "addresses",
"query": {
"match_phrase": {
"address": "scarborough"
}
}
}
}
}
},
q=aaron&phone=813-689-6889 - searching a person by name and phone number works fine as well.
{
"query": {
"match_phrase": {
"name": "aaron"
}
},
"filter": {
"bool": {
"must": {
"query": {
"match_phrase": {
"phone": "813-689-6889"
}
}
}
}
}
However, When I try to use both filters, address and phone I get a No filter registered for [match_phrase] error
for example: q=aaron&address=scarborough&phone=813-689-6889
{
"query": {
"match_phrase": {
"name": "aaron"
}
},
"filter": {
"bool": {
"must": {
"nested": {
"path": "addresses",
"query": {
"match_phrase": {
"address": "scarborough"
}
}
},
"query": {
"match_phrase": {
"phone": "813-689-6889"
}
}
}
}
}
the error, when using address and phone filters together:
nested: QueryParsingException[[pl_people] No filter registered for [match_phrase]]; }]","status":400}):
index mapping (person) as requested:
{
"pl_people": {
"mappings": {
"person": {
"properties": {
"ac_name": {
"type": "string",
"analyzer": "autocomplete"
},
"date_of_birth": {
"type": "date",
"format": "dateOptionalTime"
},
"email": {
"type": "string"
},
"first_name": {
"type": "string",
"fields": {
"na_first_name": {
"type": "string",
"index": "not_analyzed"
}
}
},
"last_name": {
"type": "string",
"fields": {
"na_last_name": {
"type": "string",
"index": "not_analyzed"
}
}
},
"middle_name": {
"type": "string",
"fields": {
"na_middle_name": {
"type": "string",
"index": "not_analyzed"
}
}
},
"name": {
"type": "string",
"fields": {
"na_name": {
"type": "string",
"index": "not_analyzed"
},
"ngram_name": {
"type": "string",
"analyzer": "my_start"
},
"ns_name": {
"type": "string",
"analyzer": "no_stopwords"
}
}
},
"phone": {
"type": "string"
},
"time": {
"type": "date",
"format": "dateOptionalTime"
},
"updated_at": {
"type": "date",
"format": "dateOptionalTime"
}
}
}
}
}
}
Maybe you can use term-filter, instead of match_phrase as a filter.
See here.

ElasticSearch missing and term query

I am trying to fetch documents where is missing field "topic.description" and match term "fundedUnder.programme": "ABC".
Mapping:
...
"fundedUnder": {
"properties": {
"programme": {
"type": "string"
},
"subprogramme": {
"type": "string"
}
}
},
"topics": {
"type": "nested",
"include_in_parent": true,
"properties": {
"code": {
"type": "string",
"analyzer": "analyzer_keyword"
},
"description": {
"type": "string",
"analyzer": "analyzer_keyword"
},
"title": {
"type": "string",
"analyzer": "analyzer_keyword"
}
}
},
...
My Query looks like:
{
"query": {
"filtered": {
"filter": {
"bool": {
"must": [
{
"missing": {
"field": "topics.description"
}
},
{
"term": {
"fundedUnder.programme" : "ABC"
}
}
]
}
}
}
}
}
This query found nothing and that is wrong, because I have in indexes a lot of documents with fundedUnder.programme == "ABC" and with missing field topics.description.
Thanks in advance.
ElasticSearch version 1.7.5
I believe this should work:
EDIT: updated to use version 1.7 Query DSL
{
"query": {
"filtered": {
"query": {
"match": { "fundedUnder.programme" : "ABC" }
},
"filter": {
"missing": { "field": "topics.description" }
}
}
}
}

OR query in nested objects ElasticSearch

I use ElasticSearch version 1.7.5 and I am trying to fetch all documents where missing some fields.
My mapping:
...
"participant": {
"properties": {
"id": {
"type": "string"
},
"firstName": {
"type": "string"
},
"lastName": {
"type": "string"
},
"name": {
"type": "string"
}
},
"coordinator": {
"properties": {
"id": {
"type": "string"
},
"firstName": {
"type": "string"
},
"lastName": {
"type": "string"
},
"name": {
"type": "string"
}
}
...
I want to query all documents that don't have assigned coordinator.id or participant.id yet.
My query looks like:
"query": {
"nested": {
"path": "coordinator, participant",
"query": {
"constant_score": {
"filter": {
"or": [
{
"missing": {
"field": "coordinator.id"
}
},
{
"missing": {
"field": "participant.id"
}
},
]
}
}
}
}
}
You do OR queries via the bool query:
https://www.elastic.co/guide/en/elasticsearch/reference/1.7/query-dsl-bool-filter.html
So this query would work:
{
"query": {
"bool": {
"should": [
{
"constant_score": {
"filter": {
"missing": {
"field": "participant.id"
}
}
}
},
{
"constant_score": {
"filter": {
"missing": {
"field": "coordinator.id"
}
}
}
}
]
}
}
}
I noticed that you were using a nested query though the mapping does not state that coordinator and participant are nested field types so that will not work:
https://www.elastic.co/guide/en/elasticsearch/reference/1.7/mapping-nested-type.html
Setting something as a nested type is only useful when you need to group search terms together so I don't think it is necessary for you.

ElasticSearch query doesn't return documents that have an "empty" nested property

I'm running into a weird problem. I have a document mapping for which one of the properties is a nested object.
{
"userLog": {
"properties": {
"userInfo": {
"userId": {
"type": "text"
},
"firstName": {
"type": "text"
},
"lastName": {
"type": "text"
},
"email": {
"type": "text"
}
},
"violations": {
"type": "integer"
},
"malfunctions": {
"type": "integer"
},
"extensionsUsed": {
"type": "integer"
},
"date": {
"type": "date",
"format": "yyyy-MM-dd||yyyy/MM/dd||yyyyMMdd||epoch_millis"
},
"events": {
"type": "nested",
"properties": {
"editorId": {
"type": "text"
},
"editorRole": {
"type": "text"
},
"editedTimestamp": {
"type": "date",
"format": "epoch_millis"
},
"createdTimestamp": {
"type": "date",
"format": "epoch_millis"
},
"userId": {
"type": "text"
},
"timestamp": {
"type": "date",
"format": "epoch_millis"
},
"eventType": {
"type": "text"
}
}
}
}
}
}
Some userLogs have events and some don't. My queries only return userLogs that have events, however, and I'm not sure why. There are definitely userLogs that exist without events in the index. I can see them in Kibana. They just aren't returned in the search. Here's what I'm running for a query:
GET index_name/_search
{
"query": {
"bool": {
"must": [
{
"range": {
"date": {
"gte": "20170913",
"format": "yyyyMMdd"
}
}
}
],
"should": [
{
"match_phrase": {
"userInfo.userId": "Xvo9qblajOVaM3bQQMaV4GKk7S42"
}
}
],
"minimum_number_should_match": 1
}
}
}
based on this discussion
I modified my query to be the following:
GET one20_eld_portal/_search
{
"query": {
"bool": {
"must_not": [
{
"nested": {
"path": "events",
"query": {
"bool": {
"filter": {
"exists": {
"field": "events.userId"
}
}
}
}
}
}
],
"should": [
{
"match_phrase": {
"userInfo.uid": "Xvo9qblajOVaM3bQQMaV4GKk7S42"
}
}
],
"minimum_should_match": 1
}
}
}
but this doesn't return any results. Any help is greatly appreciated!
Turns out the reason the "empty" logs weren't being returned is because the userId wasn't being set properly for empty logs.

Elasticsearch parent-child conditional fetching children

I have 2 type documents mapped using parent-child relations, mappings as follow:
{
"venue": {
"properties": {
"id": {
"type": "long",
"index": "not_analyzed"
},
"address": {
"type": "string",
"index": "analyzed"
},
"capacity": {
"type": "integer",
"index": "not_analyzed"
},
"rate_standard": {
"type": "nested",
"properties": {
"rate": {
"type": "double",
"precision_step": 1,
"index": "not_analyzed"
},
"minimum": {
"type": "integer",
"index": "not_analyzed"
}
}
}
}
},
"calendar": {
"_parent": {
"type": "venue"
},
"properties": {
"day": {
"type": "date",
"format": "yyyy-MM-dd",
"precision_step": 1,
"index": "not_analyzed"
},
"is_available": {
"type": "boolean",
"index": "not_analyzed"
},
"rate": {
"type": "double",
"precision_step": 1,
"index": "not_analyzed"
},
"minimum": {
"type": "integer",
"index": "not_analyzed"
}
}
}
}
I would like to run queries against them as follows:
capacity gte 2
address has all the words of mampang jakarta indonesia
must not have calendar entry (child) with attribute is_available = false, between 2014-01-01 to 2014-12-01
Had tried to build the query but its seems messed up at nesting bool query
{
"query": {
"bool": {
"must": [
{
"range": {
"capacity": {
"gte": 2
}
}
},
{
"match": {
"address": {
"query": "mampang jakarta indonesia",
"operator": "and"
}
}
}
],
"must_not": {
"has_child": {
"type": "calendar",
"query": {
"bool": {
"must": [
{
"term": {
"available": false
}
},
{
"range": {
"day": {
"gte": "2014-01-01",
"lte": "2014-12-01"
}
}
}
],
"must_not": {},
"should": {}
}
}
}
},
"should": {}
}
}
}
This one seems to works
{
"query": {
"bool": {
"must": [
{ "range" : {"capacity": { "gte": 2 }} },
{ "match": {"address": { "query" : "mampang jakarta indonesia", "operator" : "and" }} }
],
"must_not": [
{
"has_child": {
"type": "calendar",
"query": {
"bool": {
"must":[
{"term": {"available" : false }},
{"range": {"day": {"gte": "2014-01-01", "lte": "2014-12-01" }}}
]
}
}
}
}
]
}
}
}
Tho still unsure if this is the appropriate query to achive that

Resources