I am using ES 2.4.6 with Java 8, and i created a document object as following:
#Document(indexName = "airports", type = "airport")
public class Airport {
#Id
private String id;
#Field(type = String)
private String name;
}
And i successfully search several airport objects to ES, with following
names: "San Francisco", "San Mateo", "Santiago", "Palo Alto", "Big San"
The JSON content inside ES looks like following:
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 5,
"max_score": 1,
"hits": [
{
"_index": "airports",
"_type": "airport",
"_id": "SSMlsTWIYefbXHCnYEwEY",
"_score": 1,
"_source": {
"id": "SSMlsTWIYefbXHCnYEwEY",
"name": "Santiago"
}
},
{
"_index": "airports",
"_type": "airport",
"_id": "LlDcKuywPjURNeIISjXLjC",
"_score": 1,
"_source": {
"id": "LlDcKuywPjURNeIISjXLjC",
"name": "San Mateo"
}
},
{
"_index": "airports",
"_type": "airport",
"_id": "CVIjEHYphSmZIjYbHCMwtkqfKWtEHVh",
"_score": 1,
"_source": {
"id": "CVIjEHYphSmZIjYbHCMwtkqfKWtEHVh",
"name": "San Francisco"
}
},
{
"_index": "airports",
"_type": "airport",
"_id": "gbntKR",
"_score": 1,
"_source": {
"id": "gbntKR",
"name": "Palo Alto"
}
},
{
"_index": "airports",
"_type": "airport",
"_id": "bKosUdHeseMMboyaejv",
"_score": 1,
"_source": {
"id": "bKosUdHeseMMboyaejv",
"name": "Big San"
}
}
]
}
}
Then i have following curl command to use regex query to find all airport
names staring with "san" ignoring case, i did:
curl -XGET 'localhost:9200/airports/airport/_search?pretty' -H 'Content-Type: application/json' -d'
{
"query": {
"regexp":{
"name": "^(?i)san"
}
}
}
'
I use the regex "^(?i)san" directly match against those airport names,
it works as expect:
String regex = "^(?i)san";
assertTrue("San Francisco".matches(regex));
assertTrue("San Mateo".matches(regex));
assertTrue("Santiago".matches(regex));
assertTrue(!"Big San".matches(regex));
So does anyone know why ES regex query returns empty result back? Now, if
i use "san" as regex, all 4 names return back, and if i use "San", nothing returns back.
You can use Match Phrase Prefix for the problem mentioned above.
{
"query": {
"match_phrase_prefix": {
"name": "San"
}
}
}
See if it resolves your problem.
Related
I have three documents indexed with title "manage", "manager", and "management".
I am searching by following query:
query: {
query_string: {
"query": "manage*",
"fields": ["title"],
}
}
}
I am getting same score for all three documents. I want document with "title": "manage" first, then manager and management.
There are two ways to achieve what you want. The easiest one to try out is to resort to script-based sorting and return a score that matches the length of the data:
GET test/_search
{
"sort": {
"_script": {
"type": "number",
"script": {
"lang": "painless",
"source": "doc['title.keyword'].value.length()"
},
"order": "asc"
}
},
"query": {
"query_string": {
"query": "manage*",
"fields": [
"title"
]
}
}
}
Note: if you don't have the title.keyword field, you can change your script to work from the source directly:
params._source['title'].length()
You'll get manage (with score of 6), then manager (with score of 7) and then management (with score of 10).
The other way to achieve this is to actually index another integer field (e.g. titleLength) with the actual length of the title field and sort by titleLength.
The query above searches all the documents containing manage, but here since boost is applied to manage, so the document containing manage will have a higher score as compared to other documents.
To know more about Query String Query refer this
Index Data
{ "name":"manage" }
{ "name":"manager"}
{ "name":"management"}
Search Query
{
"query": {
"query_string": {
"fields": [
"name"
],
"query": "manage^2*"
}
}
}
Search Result:
"hits": [
{
"_index": "my_index",
"_type": "_doc",
"_id": "1",
"_score": 3.3263016,
"_source": {
"name": "manage"
}
},
{
"_index": "my_index",
"_type": "_doc",
"_id": "2",
"_score": 1.0,
"_source": {
"name": "manager"
}
},
{
"_index": "my_index",
"_type": "_doc",
"_id": "3",
"_score": 1.0,
"_source": {
"name": "management"
}
}
]
Edit 1:
If 1 more document is indexed:
{ "name":"managers" }
Search Query:
{
"query": {
"query_string": {
"query": "manage~"
}
}
}
Search Result:
"hits": [
{
"_index": "my_index",
"_type": "_doc",
"_id": "1",
"_score": 0.87546873,
"_source": {
"name": "manage"
}
},
{
"_index": "my_index",
"_type": "_doc",
"_id": "2",
"_score": 0.7295572, -->score is different
"_source": {
"name": "manager"
}
},
{
"_index": "my_index",
"_type": "_doc",
"_id": "4",
"_score": 0.58364576,
"_source": {
"name": "managers"
}
}
]
In your case, for management you have more than 2 edit distance i.e. manage -> managem --> manageme --> managemen --> management.
And if the search is made by using a fuzzy query, then their maximum only two edits are allowed .
So, therefore, management will not match here (by the above search query), rest all words will match (which have edit distance<=2), having different scores.
I am trying to debug an ElasticSearch query and I could use some help with figuring out what I am doing wrong here.
The problem I am having is when I add a "terms" query on a field, no hits are found when I expect results back.
This is the query WITHOUT the "terms" part:
GET /activity/doc/_search
{
"query":{
"bool":{
"must": [
{
"range": {
"ageMax": {
"gte": 20
}
}
},
{
"range": {
"ageMin": {
"lte": 28
}
}
},
{
"range":{
"activityDate":{
"gte":"2019-06-12T16:23:12.709Z"
}
}
},
{
"geo_distance":{
"distance":"50.0km",
"location.gps":{
"lon":-122.406417,
"lat":37.785834
}
}
}
]
}
}
}
This is the result I get back:
{
"took": 7,
"timed_out": false,
"_shards": {
"total": 3,
"successful": 3,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 4,
"max_score": 4,
"hits": [
{
"_index": "activity",
"_type": "doc",
"_id": "f35980fe-94cb-4c4a-9ee7-84dbace823b0",
"_score": 4,
"_source": {
"ageMax": 68,
"__typename": "Activity",
"photo": {
"bucket": "vevivo8106a3b4577d41ec943f5ff2d7536d38-develop",
"region": "eu-west-1",
"Key": "Facebook_137538237374224/FA5A7B52-48E6-4816-85FD-06AD04721FBF.jpg",
"url": "https://vevivo8106a3b4577d41ec943f5ff2d7536d38-develop.s3.eu-west-1.amazonaws.com/public/Facebook_137538237374224/FA5A7B52-48E6-4816-85FD-06AD04721FBF.jpg?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=ASIAQUVMMI25HVHW4O5W%2F20190611%2Feu-west-1%2Fs3%2Faws4_request&X-Amz-Date=20190611T202205Z&X-Amz-Expires=900&X-Amz-Security-Token=AgoGb3JpZ2luENj%2F%2F%2F%2F%2F%2F%2F%2F%2F%2FwEaCWV1LXdlc3QtMSKAAoIYw2UKtl9sRiRSxJ6OpnAhQ1GT4WfSzCWQybW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TZJBr%2F1xu3IP7oKBG8AOwjSJJx0fDpRLUMCWH%2BC%2BHNiAqjRpShCvX3OFOFRygMa50C0ocdlGrEtKDZBXa2%2BSW4WR0LEeozMeWjJTPF9iA%2FE8GyCscdnx2bMAhccJupAplL7UhCMTdRCJ2SalnNJR7Q49LvG4ryG8OdxhhYZD4n51wVVWlB7fqsP%2Bki8feH77jokiofq3eOU9jFk3SaxbfkzB8%2FcVhtzCrS%2BSswvZaA6AU%3D&X-Amz-Signature=399ae23fe3f01338d1bf79b918c9ce51ffa91c8f705d62ea3b516b0341b60578&X-Amz-SignedHeaders=host"
},
"dateModified": "2019-06-11T20:21:52.870Z",
"version": 1,
"usersWatching": 0,
"createdAt": "2019-06-11T20:22:43.215Z",
"likesCount": 0,
"textData": "Watch a dance movie?",
"enrolledUsers": 0,
"activityDate": "2019-06-19T20:12:44.000Z",
"ageMin": 18,
"dateCreated": "2019-06-11T20:21:52.870Z",
"peopleRequested": 2,
"commentsCount": 0,
"location": {
"address": {
"zipcode": "94108",
"country": "United States",
"city": "San Francisco",
"street": "Stockton St",
"state": null
},
"gps": {
"lon": -122.406417,
"lat": 37.785834
}
},
"enrollmentRequests": 0,
"id": "f35980fe-94cb-4c4a-9ee7-84dbace823b0",
"activityCreatorId": "Facebook_137538237374224",
"category": "Movies::Ballet",
"updatedAt": "2019-06-11T20:22:43.215Z"
}
},
{
"_index": "activity",
"_type": "doc",
"_id": "ce9ab1ee-8fa8-42dd-aeb8-c1a9f58ab6b3",
"_score": 4,
"_source": {
"ageMax": 68,
"__typename": "Activity",
"photo": {
"bucket": "vevivo8106a3b4577d41ec943f5ff2d7536d38-develop",
"region": "eu-west-1",
"Key": "Facebook_137538237374224/FF8E51D9-279B-4EC2-9461-55E2CBFC637A.jpg",
"url": "https://vevivo8106a3b4577d41ec943f5ff2d7536d38-develop.s3.eu-west-1.amazonaws.com/public/Facebook_137538237374224/FF8E51D9-279B-4EC2-9461-55E2CBFC637A.jpg?X-Amz-Algorithm=AW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f2DjbCYbDs5EZqg0pm8iVCFtAJODIDLktwZUhl%2BCrv9JYv8epzQgIBDBUIPjeudOMLodahv%2BfFwHl9lRLNUZkDPGgmhIby62J5y2pkoJE353mKPs%2BDvqkmqTMy2377DH1IOsrPRmkgWldPpv4uFD5jKN3r9xxQv4LUswzD%2FfPSBG2pgvaUgeE2YcWg3o6CcW4KtUOPLucHLw66lkgcnQ8M9feo3j35z%2B9lSblNTd6rCuVgrxa9zuRzNxpRl37ZSut%2B7VPy%2FcKN05mHsZfvIBVcPXF4SdQwgnH2%2BZz4fNEU75MRLyqud7rKQ47h2pamKL%2F%2BUqPn8tNlAAZNMpQHh1KAyIrE35WBbIMgAZSVRUeXcvHVONTJrMbeVC%2FZ0EYAfnCR8uxBsGWPIcsObXN%2FJAGbLGcMW5QB%2Bu1MTSI0qbLoxtzxK9r9lQwzZ6A6AU%3D&X-Amz-Signature=5bdfec0ae588f234af3f8e6dd75c3c7ed8f85fdb3c333c895d02c6f63bf0a548&X-Amz-SignedHeaders=host"
},
"dateModified": "2019-06-11T20:31:31.646Z",
"version": 1,
"usersWatching": 0,
"createdAt": "2019-06-11T20:32:51.687Z",
"likesCount": 0,
"textData": "Anyone for a dance movie?",
"enrolledUsers": 0,
"activityDate": "2019-06-21T20:31:23.000Z",
"ageMin": 18,
"dateCreated": "2019-06-11T20:31:31.646Z",
"peopleRequested": 2,
"commentsCount": 0,
"location": {
"address": {
"zipcode": "94108",
"country": "United States",
"city": "San Francisco",
"street": "Stockton St",
"state": null
},
"gps": {
"lon": -122.406417,
"lat": 37.785834
}
},
"enrollmentRequests": 0,
"id": "ce9ab1ee-8fa8-42dd-aeb8-c1a9f58ab6b3",
"activityCreatorId": "Facebook_137538237374224",
"category": "Movies::Romance",
"updatedAt": "2019-06-11T20:32:51.687Z"
}
},
{
"_index": "activity",
"_type": "doc",
"_id": "309db646-903c-471e-b045-b1f55ae6cff0",
"_score": 4,
"_source": {
"ageMax": 68,
"__typename": "Activity",
"photo": {
"bucket": "vevivo8106a3b4577d41ec943f5ff2d7536d38-develop",
"region": "eu-west-1",
"Key": "Facebook_137538237374224/18BB874D-F59C-4924-8764-75A25020C61C.jpg",
"url": "https://vevivo8106a3b4577d41ec943f5ff2d7536d38-develop.s3.eu-west-1.amazonaws.com/public/Facebook_137538237374224/18BB874D-F59C-4924-8764-75A25020C61C.jpg?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=ASIAQUVMMI25F4QOD3PE%2F20190611%2Feu-west-1%2Fs3%2Faws4_request&X-Amz-Date=20190611T203026Z&X-Amz-Expires=900&X-Amz-Security-Token=AgoGb3JpZ2luENj%2F%2F%2F%2F%2F%2F%2F%2F%2F%2FwEaCWV1LXdlc3QtMSKAAhy6dUpwqL9847F4NppRj%2FsFwXtJgJQk%2BK6fsSlAzfUwJGEXEDhGrOEs45HiOpHAEN5YoEskPEb6YpfUdLpXanO5TaYp%2F4Q4uiMMTcR9PVPg%2BVFUylTYJQ%2BskQDcqJw%2BPqppZiWvMMEzJNkB335B6gyqLgsJWyl0okgKLknVVKTJAntsGiqfX%2FvVFk94aoMP0Ubv3ymXyxZ9dxqA5Mqe6EbNoxteQMdLQqoZPfXiGQmvDjgfpZph2SCkkOSwp1slGF0vCjOIztj%2B4Rsfq9jfI14Ks6th25SHOZjeB0HEx497KgFyYQFp41ke8u4WsJ91alv8fGpyMhId2b8v%2F%2BwkNsgqrwUIrv%2F%2F%2F%2F%2F%2F%2F%2F%2F%2FARACGgwwNDQzODQ5Mjk0NjYiDOiqDscm7ZoQvO8lWCqDBZB56K6XqJ565o1bnZc%2Bh5x9JHeAU8kOcaDvSfa8syzOCQTSW%2BORfmPJzKDij5DN%2B4QFSvycY%2BuU%2FTQqfB6F3bbfD8rLr1HsVBFaKzwIOA6RSEDolCJb3Q2ZyazfFPuzOof3BlEFKYrUoqO1x5Ur0K3z8DRsst7TyAMBPxiYnN%2BZMzsxjFXRz7ps24suwvO3Urz0zogJWGP%2FxSvX%2F6386bTo8X9oIPAVhwdjEebzYATCfe02IRPUxXGQHyZ3qVF5Ccy7zioLW2iNVUQawCN26lfcoUq79xt%2F9mygFM36Xyr3cC0EH5dV5hIj6f%2F9GDvjb%2FLex0c3XPBtzgDDdJoLPgU4UFdIyhYlHNrUZN40vpLZqJIBKQkjyUcoSkzW0dhCx0DukjimLScwwUPxEtSb%2BKqk94zbsZUJjnSBzcGf2DjbCYbDs5EZqg0pm8iVCFtAJODIDLktwZUhl%2BCrv9JYv8epzQgIBDBUIPjeudOMLodahv%2BfFwHl9lRLNUZkDPGgmhIby62J5y2pkoJE353mKPs%2BDvqkmqTMy2377DH1IOsrPRmkgWldPpv4uFD5jKN3r9xxQv4LUswzD%2FfPSBG2pgvaUgeE2YcWg3o6CcW4KtUOPLucHLw66lkgcnQ8M9feo3j35z%2B9lSblNTd6rCuVgrxa9zuRzNxpRl37ZSut%2B7VPy%2FcKN05mHsZfvIBVcPXF4SdQwgnH2%2BZz4fNEU75MRLyqud7rKQ47h2pamKL%2F%2BUqPn8tNlAAZNMpQHh1KAyIrE35WBbIMgAZSVRUeXcvHVONTJrMbeVC%2FZ0EYAfnCR8uxBsGWPIcsObXN%2FJAGbLGcMW5QB%2Bu1MTSI0qbLoxtzxK9r9lQwzZ6A6AU%3D&X-Amz-Signature=5c5f8e0237e7dc3725617a82db97dfd37c87fe3872a85eabb883100f41aa26e1&X-Amz-SignedHeaders=host"
},
"dateModified": "2019-06-11T20:30:14.449Z",
"version": 1,
"usersWatching": 0,
"createdAt": "2019-06-11T20:31:25.613Z",
"likesCount": 0,
"textData": "Romance Movie Anyone?",
"enrolledUsers": 0,
"activityDate": "2019-06-14T20:31:17.000Z",
"ageMin": 18,
"dateCreated": "2019-06-11T20:30:14.449Z",
"peopleRequested": 1,
"commentsCount": 0,
"location": {
"address": {
"zipcode": "94108",
"country": "United States",
"city": "San Francisco",
"street": "Stockton St",
"state": null
},
"gps": {
"lon": -122.406417,
"lat": 37.785834
}
},
"enrollmentRequests": 0,
"id": "309db646-903c-471e-b045-b1f55ae6cff0",
"activityCreatorId": "Facebook_137538237374224",
"category": "Movies::Romance",
"updatedAt": "2019-06-11T20:31:25.613Z"
}
},
{
"_index": "activity",
"_type": "doc",
"_id": "3a7c629e-803d-4fb9-8b7c-5fac08255649",
"_score": 4,
"_source": {
"ageMax": 62,
"__typename": "Activity",
"photo": {
"bucket": "vevivo8106a3b4577d41ec943f5ff2d7536d38-develop",
"region": "eu-west-1",
"Key": "d70053b2-cbf2-47ea-959e-6b081e00dac9/ECEFB5DB-0F32-4142-A26C-10B7ED120452.jpg",
"url": "https://vevivo8106a3b4577d41ec943f5ff2d7536d38-develop.s3.eu-west-1.amazonaws.com/public/d70053b2-cbf2-47ea-959e-6b081e00dac9/ECEFB5DB-0F32-4142-A26C-10B7ED120452.jpg?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=ASIAQUVMMI25EVY5X32Z%2F20190612%2Feu-west-1%2Fs3%2Faws4_request&X-Amz-Date=20190612T125107Z&X-Amz-Expires=900&X-Amz-Security-Token=AgoGb3JpZ2luEOn%2F%2F%2F%2F%2F%2F%2F%2F%2F%2FwEaCWV1LXdlc3QtMSKAAhT4eq9r4GURlO%2B6hQW0PAMKVmFBUtHaI14%2Fz3i0p3WBjCHnlT49PoOXHaUgZKUVWqDQYKsSWthth%2FfV5k%2BRN76Nce%2BZKNfa9Gzpy%2BCUHVY34koYZSPo%2FFLC%2Be5ox3RBBgrpkB%2BzPHkuc4KNyPKIoSr%2BOHolUhRMZIOmRL84lzzttazBpgwIqxacKo6DQha2k%2FJTh5v%2FqDUXxLwr8Bj0DMRVx7PZg4MxLFSAZ0lShUa7H%2BpHKxkA%2F9wFcTPCK32HuvAub32O1qn1N8zBJxqZhLa5YtBA0vydq%2BIUHPsYePDryc0jTmz4MNVcQsrTPddOPkDoD1qX%2BI1pXEStBPV3EPkqrwUIvv%2F%2F%2F%2F%2F%2F%2F%2F%2F%2FARACGgwwNDQzODQ5Mjk0NjYiDDCSdZ%2BxIQDybtgqASqDBQC%2BvI1sG5B9CqFZNNSl9qiRMnqbvQJ3WU57qsiz4kJftgMBU0nMdhl97p6mDXhJBvqC2vTgnnGiTbrRMr7%2BvNbNzbigkUO0exM%2B7bqxt6ij%2B8gz%2FdLd8T5Faj8j6AzvKGtwHxaGvaG0tCxvJGD%2BNYzOJxS8mae%2Fvf6qgpH%2BzQaf057NaZ9KsNlFZfCDD2CpBT%2FmB05tnzPmLDy5amzyuCyLWZgi7fzB6mqcKBmN0DktWb6RPRyaNFFi6PaJwHIDF%2BL1B3jx%2BmOXMon2Xwki8j9WugGWhQvMAl%2Fug82QoX1bxHegymeesIG052D6e%2F5XieeDwUZZ5IQinTx3eqs3m%2BqFlkxdIRb3JoPRldVm8hNUHG59mEI01r16%2B5SjTJ%2B4yvVR3x2%2BkGV2DY%2FjV5K8PueUHsIlN2hMGepG%2F7Rwg5fMdgg0YX3zEHZuyVdGvcQtXJbpmpYXxsXmRBkhIt3n59%2FtypAKHP1yLzV2Gxp2aYwUGiDVvn%2BsPHlfFL2FM4eil8g6%2FQVmSjZlw3Wa9Ke5HhIq5RV8la4TyOa7ogoAmoiVTlvqNvYgWlWrxl9zij4MgNwZ3S5Z79svhUNS8zHPDCtHru%2FXj81AUaTDsFSU%2BcVH%2BfKZuYVp3xWFAMSWhD3RQ5RT%2B8zCSKfvcI7hnI2i%2BhvAFFdqD8yPKBQ0A17LyYBc%2FAG91JDvw8lOM8fbmHQiMcmnd62V%2FE9RoQ0OAf3mrobMulrwxLKoQd%2F%2F9Hf8G7fVZZFP5jnIDVkIZScY8pZkzjYgftscGjagStFm4UtjM3KKWkBW5kb3zQREEFl9lUNrna4I89rVwxGxHcj0yVso18If2VPv932VzI2b455B2cbLGnswh%2BqD6AU%3D&X-Amz-Signature=72ac408bd3e51780c2ce3a44843365ca70550d07e0d7eb1e8781968ca5bf854e&X-Amz-SignedHeaders=host"
},
"dateModified": "2019-06-12T12:50:51.986Z",
"version": 1,
"usersWatching": 0,
"createdAt": "2019-06-12T12:52:28.461Z",
"likesCount": 0,
"textData": "Movie night?",
"enrolledUsers": 0,
"activityDate": "2019-06-14T12:50:14.000Z",
"ageMin": 18,
"dateCreated": "2019-06-12T12:50:51.986Z",
"peopleRequested": 1,
"commentsCount": 0,
"location": {
"address": {
"zipcode": "94108",
"country": "United States",
"city": "San Francisco",
"street": "Stockton St",
"state": null
},
"gps": {
"lon": -122.406417,
"lat": 37.785834
}
},
"enrollmentRequests": 0,
"id": "3a7c629e-803d-4fb9-8b7c-5fac08255649",
"activityCreatorId": "d70053b2-cbf2-47ea-959e-6b081e00dac9",
"category": "Movies::Ballet",
"updatedAt": "2019-06-12T12:52:28.461Z"
}
}
]
}
}
When I add the "terms" query in the filter like this:
GET /activity/doc/_search
{
"query":{
"bool":{
"must": [
{
"range": {
"ageMax": {
"gte": 20
}
}
},
{
"range": {
"ageMin": {
"lte": 28
}
}
},
{
"range":{
"activityDate":{
"gte":"2019-06-12T16:23:12.709Z"
}
}
},
{
"geo_distance":{
"distance":"50.0km",
"location.gps":{
"lon":-122.406417,
"lat":37.785834
}
}
}
],
"filter": {
"terms":
{
"category": ["Movies::Ballet"]
}
}
}
}
}
I expect to get back only the documents with category 'Movies::Ballet' but I get no hits..
I tried adding the "terms" query to the "must" array but same result.
I appreciate any help with figuring out where I am going wrong
I was finally able to get the query I want to work by changing 2 things:
Changed the category field for my test records to "Movies_Ballet" instead of "Movies::Ballet". I guess the "::" is a special character I was not aware of (someone please correct me if I am wrong here)
Changed the terms query to lowercase like:
"filter": {
"terms": {
"category": [
"movies_romance",
"TWO"
]
}
The expected result is returned.
hope this helps someone save some time
I am using scroll api to get search results using scroll_id and my request is :
https://my-es-domain-5euba7647rpc35m5utkiwweds.eu-west-1.es.amazonaws.com/_search/scroll?scroll_id=123
The weird thing is that as a response I am getting this:
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 3,
"max_score": 1,
"hits": []
}
}
Is there anything that I am missing here? Why I am not getting the rest of my search results?
As you can see hits property is empty.
EDIT: Just to note that when I try to get all results using scroll api then everything is ok:
https://my-es-domain-5euba7647rpc35m5utkiwweds.eu-west-1.es.amazonaws.com/_search?scroll=5m&size=3
{
"_scroll_id": "123",
"took": 4,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 3,
"max_score": 1,
"hits": [
{
"_index": "movies",
"_type": "movie",
"_id": "2",
"_score": 1,
"_source": {
"title": "Lawrence of Arabia",
"director": "David Lean",
"year": 1962,
"genres": [
"Adventure",
"Biography",
"Drama"
]
}
},
{
"_index": "movies",
"_type": "movie",
"_id": "1",
"_score": 1,
"_source": {
"title": "The Assassination of Jesse James by the Coward Robert Ford",
"director": "Andrew Dominik",
"year": 2007,
"genres": [
"Biography",
"Crime",
"Drama"
]
}
},
{
"_index": "movies",
"_type": "movie",
"_id": "3",
"_score": 1,
"_source": {
"title": "To Kill a Mockingbird",
"director": "Robert Mulligan",
"year": 1962,
"genres": [
"Crime",
"Drama",
"Mystery"
]
}
}
]
}
}
EDIT2: My mistake. When I did first request I was already getting all three results and then when I passed scroll_id as a parameter and tried to get rest of the search results the hits array was empty:)
Maybe you have only 3 documents that match this search?
The first scroll returns them, and the second scroll has no more docs to retrieve
Someone have any idea of what tokenizer to use and how to enable rule for the below,
Input : ["test1-data.example.com", "test2-new.example.com", "new1-test.example.com"]
Output (expected ) :
test1-data.example.com test2-new.example.com new1-test.exampl.com
It's not obvious whether it solves your problem or not, but here's one way you can do what it sounds like you're asking:
DELETE /test_index
PUT /test_index
{
"settings": {
"number_of_shards": 1
},
"mappings": {
"doc": {
"_all": {
"enabled": true,
"store": true,
"index": "not_analyzed"
},
"properties": {
"text_field": {
"type": "string",
"include_in_all": true
}
}
}
}
}
PUT /test_index/doc/1
{
"text_field": ["test1-data.example.com", "test2-new.example.com", "new1-test.example.com"]
}
POST /test_index/_search
{
"fields": [
"_all"
]
}
...
{
"took": 3,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 1,
"hits": [
{
"_index": "test_index",
"_type": "doc",
"_id": "1",
"_score": 1,
"fields": {
"_all": "test1-data.example.com test2-new.example.com new1-test.example.com "
}
}
]
}
}
Here's the code in Sense:
http://sense.qbox.io/gist/45200711a41268634439b669e18541e68042ac8a
I have created an Index with field title_auto:
class GameIndex(indexes.SearchIndex, indexes.Indexable):
text = indexes.CharField(document=True, model_attr='title')
title = indexes.CharField(model_attr='title')
title_auto = indexes.NgramField(model_attr='title')
Elastic search settings look like this:
ELASTICSEARCH_INDEX_SETTINGS = {
'settings': {
"analysis": {
"analyzer": {
"ngram_analyzer": {
"type": "custom",
"tokenizer": "lowercase",
"filter": ["haystack_ngram"],
"token_chars": ["letter", "digit"]
},
"edgengram_analyzer": {
"type": "custom",
"tokenizer": "lowercase",
"filter": ["haystack_edgengram"]
}
},
"tokenizer": {
"haystack_ngram_tokenizer": {
"type": "nGram",
"min_gram": 1,
"max_gram": 15,
},
"haystack_edgengram_tokenizer": {
"type": "edgeNGram",
"min_gram": 1,
"max_gram": 15,
"side": "front"
}
},
"filter": {
"haystack_ngram": {
"type": "nGram",
"min_gram": 1,
"max_gram": 15
},
"haystack_edgengram": {
"type": "edgeNGram",
"min_gram": 1,
"max_gram": 15
}
}
}
}
}
I try to do autocomplete search, it works, however returns too many irrelevant results:
qs = SearchQuerySet().models(Game).autocomplete(title_auto=search_phrase)
OR
qs = SearchQuerySet().models(Game).filter(title_auto=search_phrase)
Both of them produce the same output.
If search_phrase is "monopoly", first results contain "Monopoly" in their titles, however, as there are only 2 relevant items, it returns 51. The others have nothing to do with "Monopoly" at all.
So my question is - how can I change relevance of the results?
It's hard to tell for sure since I haven't seen your full mapping, but I suspect the problem is that the analyzer (one of them) is being used for both indexing and searching. So when you index a document, lots of ngram terms get created and indexed. If you search and your search text is also analyzed the same way, lots of search terms get generated. Since your smallest ngram is a single letter, pretty much any query is going to match a lot of documents.
We wrote a blog post about using ngrams for autocomplete that you might find helpful, here: http://blog.qbox.io/multi-field-partial-word-autocomplete-in-elasticsearch-using-ngrams. But I'll give you a simpler example to illustrate what I mean. I'm not super familiar with haystack so I probably can't help you there, but I can explain the issue with ngrams in Elasticsearch.
First I'll set up an index that uses an ngram analyzer for both indexing and searching:
PUT /test_index
{
"settings": {
"number_of_shards": 1,
"analysis": {
"filter": {
"nGram_filter": {
"type": "nGram",
"min_gram": 1,
"max_gram": 15,
"token_chars": [
"letter",
"digit",
"punctuation",
"symbol"
]
}
},
"analyzer": {
"nGram_analyzer": {
"type": "custom",
"tokenizer": "whitespace",
"filter": [
"lowercase",
"asciifolding",
"nGram_filter"
]
}
}
}
},
"mappings": {
"doc": {
"properties": {
"title": {
"type": "string",
"analyzer": "nGram_analyzer"
}
}
}
}
}
and add some docs:
PUT /test_index/_bulk
{"index":{"_index":"test_index","_type":"doc","_id":1}}
{"title":"monopoly"}
{"index":{"_index":"test_index","_type":"doc","_id":2}}
{"title":"oligopoly"}
{"index":{"_index":"test_index","_type":"doc","_id":3}}
{"title":"plutocracy"}
{"index":{"_index":"test_index","_type":"doc","_id":4}}
{"title":"theocracy"}
{"index":{"_index":"test_index","_type":"doc","_id":5}}
{"title":"democracy"}
and run a simple match search for "poly":
POST /test_index/_search
{
"query": {
"match": {
"title": "poly"
}
}
}
it returns all five documents:
{
"took": 3,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"failed": 0
},
"hits": {
"total": 5,
"max_score": 4.729521,
"hits": [
{
"_index": "test_index",
"_type": "doc",
"_id": "2",
"_score": 4.729521,
"_source": {
"title": "oligopoly"
}
},
{
"_index": "test_index",
"_type": "doc",
"_id": "1",
"_score": 4.3608603,
"_source": {
"title": "monopoly"
}
},
{
"_index": "test_index",
"_type": "doc",
"_id": "3",
"_score": 1.0197333,
"_source": {
"title": "plutocracy"
}
},
{
"_index": "test_index",
"_type": "doc",
"_id": "4",
"_score": 0.31496215,
"_source": {
"title": "theocracy"
}
},
{
"_index": "test_index",
"_type": "doc",
"_id": "5",
"_score": 0.31496215,
"_source": {
"title": "democracy"
}
}
]
}
}
This is because the search term "poly" gets tokenized into the terms "p", "o", "l", and "y", which, since the "title" field in each of the documents was tokenized into single-letter terms, matches every document.
If we rebuild the index with this mapping instead (same analyzer and docs):
"mappings": {
"doc": {
"properties": {
"title": {
"type": "string",
"index_analyzer": "nGram_analyzer",
"search_analyzer": "standard"
}
}
}
}
the query will return what we expect:
POST /test_index/_search
{
"query": {
"match": {
"title": "poly"
}
}
}
...
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 1.5108256,
"hits": [
{
"_index": "test_index",
"_type": "doc",
"_id": "1",
"_score": 1.5108256,
"_source": {
"title": "monopoly"
}
},
{
"_index": "test_index",
"_type": "doc",
"_id": "2",
"_score": 1.5108256,
"_source": {
"title": "oligopoly"
}
}
]
}
}
Edge ngrams work similarly, except that only terms that start at the beginning of the words will be used.
Here is the code I used for this example:
http://sense.qbox.io/gist/b24cbc531b483650c085a42963a49d6a23fa5579
Unfortunately at this point in time there seems to be no way (apart from implementing a custom backend) to configure search analyzers and index analyzers through Django-Haystack separately.
In case Django-Haystack autocomplete returns too wide results you can make use of the score value provided with each search result to optimize the output.
if search_query != "":
# Use autocomplete query or filter
# with results_filtered being a SearchQuerySet()
results_filtered = results_filtered.filter(text=search_query)
#Remove objects with a low score
for result in results_filtered:
if result.score < SEARCH_SCORE_THRESHOLD:
results_filtered = results_filtered.exclude(id=result.id)
It worked reasonable well for me without having to define my own backend and scheme building.